Android到C++;套接字连接在收到文件后关闭
我有以下问题: 我有一个Android应用程序,它将图片发送给C++服务器,它计算一些参数并将它们发送回客户端。但是,当文件被发送并且所有内容都被计算出来时,手机无法接收信息,我的日志显示“套接字已关闭” 但是如果我注释掉发送和接收图像的代码,客户端就能够从服务器接收消息。为什么在发送图像后不可能这样做 这是我的相关代码: 安卓:Android到C++;套接字连接在收到文件后关闭,android,c++,sockets,message,file-transfer,Android,C++,Sockets,Message,File Transfer,我有以下问题: 我有一个Android应用程序,它将图片发送给C++服务器,它计算一些参数并将它们发送回客户端。但是,当文件被发送并且所有内容都被计算出来时,手机无法接收信息,我的日志显示“套接字已关闭” 但是如果我注释掉发送和接收图像的代码,客户端就能够从服务器接收消息。为什么在发送图像后不可能这样做 这是我的相关代码: 安卓: String picturePath = "/sdcard/DCIM/Camera/IMG_0004.jpg"; final File pic
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\u错误;
while(TempSock==SOCKET\u错误)
{
std::cout在连接完成时关闭输出流,而不是之前。关闭套接字的输入或输出流将关闭套接字和另一个流。关闭围绕另一个流的任何流或读卡器或写器将关闭该流。在阅读完后尝试将移出。Close()
移到。(不记得这是否是一个可能的问题。)我也尝试过这样做,但是我的C++服务器不知道文件是什么时候被完全接收的,而且它没有继续使用其他函数。总之,这只是OutPuxSun,我认为插座仍然是打开的。或者我错了吗?你的套接字初始化在哪里C++?关于不知道你什么时候收到所有的问题。然而,您可能应该更改“协议”以添加一个不在流中的特殊终止符标记,或者在流添加总长度之前添加。那么,我是否正确地理解了这一点?问题实际上是关闭outputsteam,我必须用另一种方法确定文件的结尾?
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();