2012-08-08 67 views
1

我有以下問題: 我有一個Android應用程序,它將圖片發送到C++服務器,計算一些參數並將它們發送回客戶端。但是當文件被髮送並且一切都被計算出來的時候手機不能接收到這個信息,而我的LogCat就會說「Socket Closed」。Android到C++ Socket連接在收到文件後關閉

但是,如果我註釋掉髮送和接收圖像的代碼,客戶機就能夠從服務器接收消息。爲什麼在圖像發送之後這是不可能的?

這裏是我的相關代碼:

安卓

String picturePath = "/sdcard/DCIM/Camera/IMG_0004.jpg"; 
      final File picture = new File(picturePath); 
         if (picture.exists()) { 
          try { 
           //reading file and sending to server 
           FileInputStream fis = new FileInputStream(picture); 
           Log.d(TAG, "trying to connect"); 
           String st = "laber"; 
           Socket s = new Socket("192.168.0.12", port); 

           tv = (TextView) findViewById(R.id.textDesc); 
           tv.setText(st); 
           OutputStream out = s.getOutputStream(); 
           DataInputStream in = new DataInputStream (s.getInputStream()); 
           byte[] buf = new byte[1024]; 
           int read = 0; 
           int totBytes = 0; 
           while ((read = fis.read(buf)) != -1) 
           { 
            totBytes = totBytes + read; 
            out.write(buf, 0, read); 
            Log.d(TAG, "Image - Read: " + read + " - Total " + totBytes + " bytes!"); 

           } 
           out.flush(); 

           out.close(); 
           st = in.readLine(); 
           fis.close(); 


           Log.d(TAG, "Waiting" + " "+ st+ " " +st.compareTo("laber")); 



           tv.setText(st); 


           // Close connection 
           s.close(); 
           Log.d(TAG, "connection closed " + st); 
           dataReceived=true; 
          } catch (UnknownHostException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
         } 

C++

SOCKET TempSock=SOCKET_ERROR; 
while(TempSock==SOCKET_ERROR) 
{ 
    std::cout<<"Waiting for incoming connections...\r\n"; 
    TempSock=accept(Socket,NULL,NULL); 
} 
Socket=TempSock; 
int totalBytes = 0; 
ofstream outFile; 
char buf[1024] = ""; 
int received = 0; 
if (outFile != NULL) 
{ 
    outFile.open("C:\\test\\ReceivedFiles\\test.jpg" , ofstream::binary); 
    cout << "File opened!" << endl; 
} else 
{ 
    cout << "Can't open file!" << endl; 
} 
//receiving file and writing to disk 
while ((received = recv(Socket, buf, sizeof(buf), 0)) > 0) 
{ 
    cout << "R:" << received << " "; 
    if (received > 0) 
    { 
     totalBytes += received; 
     if (outFile.is_open()) 
     { 
      outFile.write(buf, received); 
      cout << " (Total: " << totalBytes << " B)" << endl; 
     } else 
     cout << "Error in recv() function, received bytes = " << received << endl; 
    } else 
     cout << "R:" << received << " "; 
} 

std::cout<<"Client connected!\r\n\r\n"; 

initImageSearch(); 
readFiles(); 
keypointMatching(); 
searchBundleOut(); 
ransacMatrices(); 
getRotationOrientation(); 

//send message back to client 
string message = doubleToString(viewingDirectionArray[0])+" "+doubleToString(viewingDirectionArray[1])+" "+doubleToString(viewingDirectionArray[2])+" "+doubleToString(positionArray[0])+" "+doubleToString(positionArray[1])+" "+doubleToString(positionArray[2])+" 0.5 0.5 0.5 baum"; 
const char *szMessage; 
szMessage = "123 456 789 246 135 178 189";//message.c_str(); 


int msg = send(Socket,szMessage,strlen(szMessage),0); 
std::cout<<msg<<endl; 
// Shutdown socket 
shutdown(Socket,SD_SEND); 

// Close socket entirely 
closesocket(Socket); 

// Cleanup Winsock 
WSACleanup(); 
+0

嘗試將'out.close()'移動到讀完後。 (不記得這是否是一個可能的問題。) – Mat 2012-08-08 10:43:00

+0

我也試過這樣做,但後來我的c + +服務器不知道什麼時候文件完全收到,並沒有繼續與其他功能。 無論如何,這只是OutputSteam,我認爲Socket應該仍然​​是開放的。或者我錯了? – user1584400 2012-08-08 10:51:03

+0

你的socket初始化程序在哪裏? – steveg89 2012-08-08 11:01:31

回答

0

關閉輸出流,當你完成連接,而不是之前。關閉套接字的輸入或輸出流會關閉套接字和其他流。關閉包裝在另一個流中的任何流或讀取器或書寫器會關閉該流。