简体   繁体   English

为什么套接字中的 recv() 函数不返回任何内容?

[英]Why recv() function in sockets does not return anything?

I am beginner to socket programming and I am using C++ in eclipse.我是套接字编程的初学者,我在 Eclipse 中使用 C++。 The program executes successfully but recv() function in server-side does not return anything.程序成功执行,但服务器端的recv()函数不返回任何内容。 It keeps waiting without terminating the program.它一直等待而不终止程序。

recv() function of server program:服务器程序的recv()函数:

void Server2::readSocket(){

    if(recv(newsockfd, buffer, sizeof(buffer), 0) < 0){
        int err = WSAGetLastError();
        cout << "Read failed with error " << err << endl;
        perror("Reading failed");
        exit(EXIT_FAILURE);
    }

    cout << "MESSAGE: " << buffer << endl;
}

client program:客户端程序:

void Client2::clientSocket(){

    #ifdef WIN32
    // Initialize Winsock
    int iResult;
    WSADATA wsaData;
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        std::cout << "WSAStartup failed: " << iResult << std::endl;

    }
    #endif

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if(sockfd < 0){
        int err = WSAGetLastError();
        cout << "Client Socket failed with error " << err << endl;
        exit(EXIT_FAILURE);
    }

}

void Client2::clientBCopy(){

    server = gethostbyname("127.0.0.1");

    bzero((char *)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;

    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);

    serv_addr.sin_port = htons(portno);

}

void Client2::clientConnect(){

    if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
        int err = WSAGetLastError();
        cout << "Client Connect failed with error " << err << endl;
        exit(EXIT_FAILURE);
    }

    cout << "Please enter message: ";
    bzero(buffer, 256);
    fgets(buffer, 255, stdin);

}

void Client2::clientWrite(){

    if(write(sockfd, buffer, strlen(buffer)) < 0){
        int err = WSAGetLastError();
        cout << "Client Write failed with error " << err << endl;
        perror("Write Failed");
        exit(EXIT_FAILURE);
    }

    bzero(buffer, 256);
}

void Client2::clientRead(){

    if(recv(sockfd, buffer, 255, 0) < 0){
        int err = WSAGetLastError();
        cout << "Client Read failed with error " << err << endl;
        exit(EXIT_FAILURE);
    }
}

How can I solve this issue?我该如何解决这个问题? Please ask me to provide the rest of the code if this is not enough and thank you for helping me!如果这还不够,请让我提供其余的代码,并感谢您帮助我!

As per my understanding,按照我的理解,

The recv() function shall return the length of the message written to the buffer pointed to by the buffer argument. recv() 函数应返回写入缓冲区参数所指向的缓冲区的消息的长度。

int fd_set_blocking(int fd, int blocking) {
/* Save the current flags */
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
    return 0;

if (blocking)`enter code here`
    flags &= ~O_NONBLOCK;
else
    flags |= O_NONBLOCK;
return fcntl(fd, F_SETFL, flags) != -1;

} }

If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recv() shall block until a message arrives.如果套接字上没有可用消息,并且套接字的文件描述符上未设置 O_NONBLOCK,则 recv() 将阻塞,直到消息到达。 might be buffer is not copied properly into the file descriptor.可能是缓冲区未正确复制到文件描述符中。

Thanks & Regards, Gunasekaran J感谢和问候, Gunasekaran J

Finally, I found my error.最后,我发现了我的错误。

    cout << "Please enter message: ";
    bzero(buffer, 256);
    fgets(buffer, 255, stdin);

This code line I used in clientConnect() method of client program cause the problem.我在客户端程序的clientConnect()方法中使用的这行代码导致了问题。 Once I removed that part recv function executed successfully printing the the message sent from the client.一旦我删除了那部分recv函数执行成功打印从客户端发送的消息。 I just found this code online and I have no idea what's happening from the above piece of code我刚刚在网上找到了这段代码,我不知道上面这段代码发生了什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM