gpt4 book ai didi

java - 从客户端接收空行

转载 作者:行者123 更新时间:2023-11-30 02:02:40 26 4
gpt4 key购买 nike

<分区>

我有一个 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);
}

这是我的输出(Received Something 后有一个空行)

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);

那么为什么我没有收到测试字符串?

谢谢

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com