我已经实现了一个Android应用程序,该应用程序使用SP相机拍摄照片并通过套接字将其发送到服务器。通过套接字发送的图像流 - 服务器死锁问题
我用下面(JAVA)的代码读取本地存储的图像文件,并在连续的块在插槽发送:
FileInputStream fileInputStream = new FileInputStream("my_image_file_path");
int nRead;
byte[] data = new byte[16384];
try {
while((nRead = fileInputStream.read(data, 0, data.length)) != -1){
networkOutputStream.write(data, 0, nRead);
}
} catch(IOException e){
e.printStackTrace();
}
fileInputStream.close();
而下面的(C/C++),代码读取它并将其存储在服务器上:
char newbuffer[MAX_BUF_SIZE];
int checkOperation;
ofstream outfile("image_file_path".c_str(), ofstream::binary);
do{
checkOperation = read(clientSocketDescriptor, newbuffer, sizeof(newbuffer));
if(checkOperation < 0){
cout << "Error in recv() function, received bytes = " << checkOperation << endl;
exit(1);
}else if (checkOperation != 0){
/*
* some data was read
*/
cout << endl << "READ Bytes: " << checkOperation << endl;
outfile.write(newbuffer, checkOperation);
/*
* emptying buffer for new incoming data
*/
for(int i = 0; i < sizeof(newbuffer); i++){
newbuffer[i] = 0;
}
}
}while(checkOperation =! 0);
outfile.close();
Android客户端应用程序似乎正确地写在插座的所有字节,并成功地从while
循环退出。
但是,服务器代码卡在while
循环的最后一次迭代中,并且无法继续执行。
- 为什么服务器不能读取
EOF
? - 我的代码发送图像或阅读它不正确?
在此先感谢您提供任何帮助,因为我真的被卡住了!
因为你不发送EOF ... – njzk2