简体   繁体   中英

C++ server unable to receive data from client / Winsock

I am working on a simple client-server application. However, after client runs, i get the message error 10038 with the recv(), in the server side. The socket number descriptor retains the same value in both client and server, thus i think there is no a socket error. Any help would be appreciated.

client:

#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;
}

server:

#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;
}

I am a beginner at Winsock programming and any help would be appreciated.

Error 10038 is WSAENOTSOCK , which means you do not have a valid socket. On the server side, you are using the server socket ( ConnectSocket ) after you have closed it. To receive and send, you need to use the connected socket ( acceptSocket ) instead. Also, you need to close acceptSocket when you are done with it, do not close ConnectSocket a second time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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