我正在尝试构建一个客户端/服务器应用程序,一侧java server
是 a c++ client
,另一侧是 a 。
客户:
while(true)
{
sock = con.connectToServer(argv[2], port);
if(sock!=NULL)
{
_itoa_s(clientID,sendBuffer,16,10);
send(sock,sendBuffer,strlen(sendBuffer),0);
printf("waiting for response\n");
bytes = recv(sock,recvBuffer,sizeof(recvBuffer)-1,0);
recvBuffer[bytes] = '\0';
printf("%s",recvBuffer);
closesocket(sock);
}
Sleep(3000);
}
服务器线程:
public void run()
{
try{
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new BufferedWriter (new OutputStreamWriter(clientSocket.getOutputStream()));
int signID = Integer.parseInt(in.readLine());
System.out.println("signID: " + signID);
//Precessing DataRequest for specific TrafficSign here
clientSocket.close();
}
catch(Exception ex){
System.out.println("Exception in TrafficSignServerThread -> " + ex.toString());
}
}
in.ReadLine()
问题是,连接成功后,虽然函数返回1,但服务器挂了send()
,表示发送数据成功。
如果我删除recv()
客户端的一部分,一切正常。所以似乎只有send()
和recv()
导致问题的组合。