【发布时间】:2021-03-31 17:44:50
【问题描述】:
我正在使用带有 c++ 的 Windows 套接字。在接下来的通话中,我试图向刚刚连接的套接字回复一条消息。
我尝试使用 C++ 中的虚拟客户端进行连接。它会连接,但 recv() 不会收到任何东西。
然后我尝试使用 telnet,它立即工作,正如我想要的那样。
SOCKET s = accept(ls, (sockaddr*)&clientSin, &s_len);
if (s == INVALID_SOCKET) {
cerr << "Error in accept call: " << WSAGetLastError();
}
else {
cout << "Connection accepted at , socket no. :" << s << endl;
//adding to list of incoming sockets
inactiveList.push_back(s);
//send message to client requesting credentials
char buff[10];
// the character 'x' is a code to the client to provide the server with the credentials
buff[0] = 'x';
buff[1] = '\0';
//send(s, buff, 2, 0);
if (send(s, "From Vic: ", 10, 0) == INVALID_SOCKET)
{
int errorcode = WSAGetLastError();
cerr << "send to client failed: " << errorcode << endl;
closesocket(s);
continue;
}
Sleep(1000);
if (send(s, "From Vic: ", 10, 0) == INVALID_SOCKET)
{
int errorcode = WSAGetLastError();
cerr << "send to client failed: " << errorcode << endl;
closesocket(s);
continue;
}
}
recv 码是:
tnb = 0;
while ((nb = recv(s, &buff[tnb], LINESZ - tnb, 0)) > 0)
{
tnb += nb;
}
/* If there was an error on the read, report it. */
if (nb < 0)
{
printf("recv failed: %d\n", WSAGetLastError());
return 1;
}
if (tnb == 0)
{
printf("Disconnect on recv");
}
/* Make the response NULL terminated and display it. Using C output */
printf("tnb = %d\n", tnb);
buff[tnb] = '\0';
puts(buff);
【问题讨论】:
-
您的
send代码可能没问题。请出示您的recv代码。 -
与您的问题无关。失败的
send调用不会返回 INVALID_SOCKET,而是返回 SOCKET_ERROR。他们可能都是一样的。 -
你应该在你的 recv 循环中明确地检查
tnb >= LINESZ是否发生了中断。在 Windows 上,长度是有符号的,但在其他任何地方,recv 的 len 参数都是无符号的。 -
另外,在打印之前不要忘记 null 终止
buff。否则,您可能会打印出比该行实际收到的更多的风险。 -
我的精神力量表明 LINESZ 比你实际发送的要大得多。因此,它只是永远停留在循环中,等待更多数据。
标签: c++ windows sockets tcp telnet