C ++服务器无法从客户端/ Winsock接收数据

时间:2013-10-04 14:34:00

标签: c++ client-server winsock2 winsockets

我正在开发一个简单的客户端 - 服务器应用程序。但是,在客户端运行后,我在服务器端收到带有recv()的消息错误10038。套接字号描述符在客户端和服务器中保留相同的值,因此我认为没有套接字错误。任何帮助将不胜感激。

客户端:

#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

// Need to link with Ws2_32.lib.
#pragma comment(lib, "ws2_32.lib")

int wmain()
{
    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        printf("WSAStartup() failed with error: %d\n", iResult);
        return 1;
    }

    // Create a socket for connecting to server.
    SOCKET ConnectSocket;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("socket() failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    printf("Socket descriptor: %d\n",ConnectSocket);

    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    sockaddr_in Service;
    memset(&Service, 0, sizeof(Service));
    Service.sin_family = AF_INET;
    Service.sin_addr.s_addr = inet_addr("127.0.0.1");
    Service.sin_port = htons(27015);

    // Connect to server.
    iResult = connect(ConnectSocket, (SOCKADDR *) &Service, sizeof (Service));
    if (iResult == SOCKET_ERROR) {
        printf("connect() failed with error: %ld\n", WSAGetLastError());
        iResult = closesocket(ConnectSocket);
        if (iResult == SOCKET_ERROR)
            printf("closesocket() failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    printf("Connected to server.\n");

    // Message that has to be sent.
    char receiveBuffer[1000];
    char message[1000];

    printf("\nEnter message: ");
    gets_s(message);

    printf("Message you wrote is: %s\n", message);

    // Send a message.
    if (send(ConnectSocket, message, sizeof(message), 0) == SOCKET_ERROR)
    {
        printf("send() failed with error code: %d\n", WSAGetLastError());
    }

        printf("Message successfully sent to server.");

    // Receive a message. 
    if (recv(ConnectSocket, receiveBuffer, 1000, 0) == SOCKET_ERROR)
    {
       printf("recv() failed with error code: %d\n", WSAGetLastError());
       while(1);
    }

    printf("\nServer says:");
    printf(receiveBuffer,sizeof(receiveBuffer));

  while(1);

closesocket(ConnectSocket);
WSACleanup();

return 0;
}

服务器:

#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")

int wmain()
{
    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        printf("WSAStartup() failed with error: %d\n", iResult);
        return 1;
    }

    // Create a socket for connecting to client.
    SOCKET ConnectSocket;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("socket() failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    printf("Socket descriptor: %d\n", ConnectSocket);

    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    sockaddr_in Service;
    memset(&Service, 0, sizeof(Service));
    Service.sin_family = AF_INET;
    Service.sin_addr.s_addr = inet_addr("127.0.0.1");
    Service.sin_port = htons(27015);

    //Bind.
    if (bind(ConnectSocket, (struct sockaddr *)&Service, sizeof(Service)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code: %d\n" , WSAGetLastError());
    }
    printf("Bind done.\n");

    // Listen on the socket for a client.
    if (listen(ConnectSocket, 1) ==  SOCKET_ERROR)
    {
        printf ("listen() failed with error: %ld\n", WSAGetLastError() );
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    printf("listen() run successfully.\n");

    // Accept a connection from a client.
    SOCKET acceptSocket;
    acceptSocket = accept(ConnectSocket, NULL, NULL);
    if (acceptSocket == INVALID_SOCKET) {
        printf("accept() failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    else{
        printf("accept() run successfully.\n");
    }

    // No longer need server socket.
    closesocket(ConnectSocket);

    char receiveBuffer[1000];
    int recv_len;

    printf("\nWaiting for data...\n");
    fflush(stdout);

    // Receive a message.
    if (recv_len = recv(ConnectSocket, receiveBuffer, 1000, 0) == SOCKET_ERROR)
    {
        printf("Socket descriptor, after recv(): %d\n", ConnectSocket);
        printf("recv() failed with error code: %d\n", WSAGetLastError());
        while(1);
    }
    // Send a message.
    if (send(ConnectSocket, receiveBuffer, recv_len, 0) == SOCKET_ERROR)
    {
        printf("sendto() failed with error code: %d\n", WSAGetLastError());
        while(1);
    }
    else
        printf("\nMessage sent back to client.");
    while(1);

closesocket(ConnectSocket);
WSACleanup();

return 0;
}

我是Winsock编程的初学者,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

错误10038是WSAENOTSOCK,这意味着您没有有效的套接字。在服务器端,您关闭它后使用服务器套接字(ConnectSocket)。要接收和发送,您需要使用连接的套接字(acceptSocket)。此外,您需要在完成后关闭acceptSocket,不要再次关闭ConnectSocket