我正在Windows10中的Visual Studio 2019中的树莓派中运行服务器,当我运行调试时,它卡在send()中。从树莓派运行。客户端是Windows10中的PC。
我已经搜索了互联网,并试图找到导致错误的原因,但没有找到答案。 错误代码仅在调试中出现,并指出发送文件的地址不正确。它可以毫无问题地连接并接收来自客户端的消息。
void TCPServer::setup(int port)
{
int b = 0;
int l = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(port);
b = bind(sockfd, (struct sockaddr*) & serverAddress, sizeof(serverAddress));
if (b == -1)
{
cout << "Error Binding - " << b << endl;
}
cout << "> bind: " << strerror(errno) << " / codigo: " << b << endl;
l = listen(sockfd, 5);
if (l == -1)
{
cout << "Error Listening - " << l << endl;
}
cout << "> listen: " << strerror(errno) << " / codigo: " << l << endl;
std::cout << ">>> waiting for client" << endl;
socklen_t sosize = sizeof(clientAddress);
newsockfd = accept(sockfd, (struct sockaddr*) & clientAddress, &sosize);
if (newsockfd == -1)
{
cout << "Error accepting - " << newsockfd << endl;
}
cout << "> accept: " << strerror(errno) << " / codigo: " << newsockfd << endl;
std::cout << ">>> client accepted" << endl;
}
//在这里卡住了:
int TCPServer::Send(string msg)
{
int bytesSend = 0;
bytesSend = send(newsockfd, msg.c_str(), MAXPACKETSIZE, 0);
cout << "> bytes sent " << bytesSend << "(" << strerror(errno) << "/" << inet_ntoa(clientAddress.sin_addr) << ")" << endl;
return bytesSend;
}
调试模式下的输出为:bytes sent -1 (Bad address/192.168.137.1)
send is returning -1
温德尔诺(Wend errno)提供了错误的地址未经调试运行的输出为:Response to be sent: Hello! :) 1
bytes sent 2920(Success/192.168.137.1)