Winsock2:"listen"提前回归

Winsock2: "listen" returns early

本文关键字:回归 listen Winsock2      更新时间:2023-10-16

我是使用 winsock2 的新手,并且为我尝试使用的服务器整理了以下代码,用于将字符串发送到我在同一台计算机上运行的客户端(使用与服务器设置为侦听的相同端口连接到 127.0.0.1)。

我正在使用MinGW,如果这很重要的话。

我遇到的问题是 listen() 似乎提前完成,但返回成功代码。这是一个问题,因为当调用accept()时,它似乎永远阻塞。无论我是否正在运行客户端程序,都会发生此事件,并且我尝试在之前和之后运行客户端程序,但这似乎不会影响它。

// -1: "Could not initialize WSA."
// -2: "Could not create listener socket."
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <cstdio>
#define port 0x0ABC
UINT64 trStrLen (char* str)
{
    if (str == NULL) return 0;
    UINT64 pos = 0;
    while (*(str + pos) != '') pos++;
    return pos;
};
#include <cstdio>
int main ()
{
    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(2,0),&wsadata)) return -1;
    SOCKET server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    SOCKADDR_IN sin;
    memset(&sin,0,sizeof(SOCKADDR_IN));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = INADDR_ANY;
    int socksize = sizeof(SOCKADDR);
    while (bind(server,(SOCKADDR*)(&sin),socksize) == SOCKET_ERROR) return -2;
    char* TEMP_TO_SEND = "Billy Mays does not approve.";
    UINT64 TEMP_SEND_LEN = trStrLen(TEMP_TO_SEND);
    printf("Server online.n");
    while (true)
    {
        printf("Waiting for connections.n");
        while (listen(server,SOMAXCONN) == SOCKET_ERROR);
        printf("Client requesting connection.n");
        SOCKET client = accept(server,NULL,NULL);
        printf("Accept is no longer blocking.n");
        if (client != INVALID_SOCKET)
        {
            printf("Attempting to send information to the client...n");
            if (send(client,TEMP_TO_SEND,TEMP_SEND_LEN,0) == SOCKET_ERROR) printf("The information wasn't sent properly.n");
            else printf("The client received the information.n");
        }
        else printf("Couldn't establish a connection to the client.n");
    };
};

这可能是显而易见的事情,但我没有看到它,所以任何提示都会有所帮助。

listen()不是阻塞调用。它不会对网络执行任何操作。它只是将套接字置于被动侦听模式,设置积压工作队列,然后返回。阻塞调用accept():它阻塞直到传入连接完成,然后为其返回套接字。

因此,您根本不应该在while循环中调用listen()

这同样适用于bind()。调用一次。