我已經實現了一個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