我有一个 C++ 客户端和一个 Java 服务器。我只是想从客户端发送一个字符串“Test”。这是我的相关JAVA代码,
Socket clientSocket = serverSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line = "";
while((reader.readLine()) != null) {
System.out.println("Recieved Something. " + line.length());
System.out.println(line);
}
这是我的输出(收到的东西后有一个空行)
Recieved Something. 0
Recieved Something. 0
还有 C++ 代码(不完全确定你们需要什么,因为我是 C++ 新手
SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
printf("Winsock error - Socket creation Failed!\r\n");
WSACleanup();
return 0;
}
struct hostent *host;
if((host=gethostbyname("localhost"))==NULL)
{
printf("Failed to resolve hostname.\r\n");
WSACleanup();
return 0;
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port= htons(2501);
SockAddr.sin_family= AF_INET;
SockAddr.sin_addr.s_addr= *((unsigned long*)host->h_addr);
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
{
printf("Failed to establish connection with server\r\n");
WSACleanup();
return 0;
}
string toSend = "Test\n";
send(Socket,toSend.c_str(), strlen(toSend.c_str()), 0);
那么为什么我没有收到测试字符串?
谢谢