WinSock2 C ++调用accept(...)方法时会发生什么

时间:2018-11-18 14:54:38

标签: c++ server winsock2

我正在遵循Microsoft的《 WinSock入门指南》(here)。

在我的情况下,我有一个充当客户端的Java应用程序和一个用于服务器的C ++应用程序。

这是到目前为止的服务器代码:

//all includes
int main()
{   
    WSADATA wsaData;
    int iResult;

    SOCKET listenSocket = INVALID_SOCKET;
    SOCKET clientSocket = INVALID_SOCKET;

    struct addrinfo *result = NULL;
    struct addrinfo hints; 

    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    //Initialization
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }
    else {
        printf("WSAStartup success !\n");
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    //Resolve the server adress and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (listenSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    //Setup the TCP listening socket
    iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed with error: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = listen(listenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed with error: %d\n", WSAGetLastError());
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    clientSocket = accept(listenSocket, NULL, NULL);
    if (clientSocket == INVALID_SOCKET) {
        printf("accept failed with error: %d\n", WSAGetLastError());
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    //No longer need server socket
    closesocket(listenSocket);

    // Receive until the peer shuts down the connection
    do {

        iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
        printf("result : %d", iResult);

        //Receive data
        if (iResult > 0) {
            printf("Bytes received: %d\n", iResult);
            printf("Message: %s\n Sending response to client... ", recvbuf);
            const char* response = "Hello from key/mouse emulator app server";
            iSendResult = send(clientSocket, response, sizeof(char) * (strlen(response)+1), 0);
            if (iSendResult == SOCKET_ERROR) {
                printf("send failed with error: %d\n", WSAGetLastError());
                closesocket(clientSocket);
                WSACleanup();
                return 1;
            }
            printf("Bytes sent: %d\n", iSendResult);
        }
        else if (iResult == 0)
            printf("Connection closing...\n");
        else {
            printf("recv failed with error: %d\n", WSAGetLastError());
            closesocket(clientSocket);
            WSACleanup();
            return 1;
        }

    } while (iResult > 0);

    // shutdown the connection since we're done
    iResult = shutdown(clientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(clientSocket);
    WSACleanup();
    system("PAUSE");
    return 0;
}

我的理解是,在do...while循环中,它正在侦听来自客户端的所有传入数据。因此,我尝试调试以检查什么都不发送时iResult中的值是什么。

我发现在使用Visual Studio社区2017时,我无法走这条线:

clientSocket = accept(listenSocket, NULL, NULL);

仅当客户端发送一些数据时,它才会进入do...while。 所以我的问题是,当我到达accept(...)行时,程序中会发生什么?

0 个答案:

没有答案