如何在服务器关闭之前让服务器继续接收消息?

时间:2015-04-17 00:34:02

标签: c client server winsock2

我正在编写一个简单的客户端和服务器。我写了我的客户端,并将其与另一台服务器进行了比较,因此它完美运行。我现在正在尝试复制服务器,由于某种原因,当我的客户端发送消息" GET 2"时,服务器到达recv函数并返回-1。然后它会在屏幕recv failed上打印,然后它会尝试shutdown,但它也会显示shutdown failed。有谁知道为什么?我非常感谢你的帮助。谢谢!

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFFLEN 1024

const unsigned int LISTENING_PORT = 21001;
const char ipAddress[15] = "127.0.0.1";
SOCKADDR_IN ServerAddr;

int main()
{
    /*Declare and initialize variables*/
    int return_Code = 10;
    char recvBuff[DEFAULT_BUFFLEN] = "";
    char storedQuotes[20][DEFAULT_BUFFLEN];

    WSADATA wsaData;
    SOCKET Socket;
    SOCKET AcceptSocket;
    SOCKADDR_IN ServerAddr;

    /*Initialize Winsock*/
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    /*Create New Socket*/
    Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (Socket == INVALID_SOCKET)
    {
        printf("Server: Socket() failed! Error code: %d.\n", WSAGetLastError());
        WSACleanup();

        system("PAUSE");
        return -1;
    }

    /*Specify what address to listen on*/
    ServerAddr.sin_family = AF_INET; //IPv4
    ServerAddr.sin_addr.s_addr = inet_addr(ipAddress); //IP Address
    ServerAddr.sin_port = htons(LISTENING_PORT); //Port no.

    /*Bind the Socket*/
    if (bind(Socket, (SOCKADDR *)& ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
    {
        printf("Server: bind() failed! Error code: %d.\n", WSAGetLastError());
        closesocket(Socket);
        WSACleanup();

        system("PAUSE");
        return -1;
    }

    /*Listen on socket for a client*/
    if (listen(Socket, 1) == SOCKET_ERROR)
    {
        printf("Sever: listen() failed! Error code: %d.\n", WSAGetLastError());
        closesocket(Socket);
        WSACleanup();

        system("PAUSE");
        return -1;
    }
    else
        printf("Server: Listening on port %d.\n\n", LISTENING_PORT);

    /*Accept a connection from a client*/
    AcceptSocket = accept(Socket, NULL, NULL);
    if (AcceptSocket == SOCKET_ERROR)
    {
        printf("Server: accept() failed! Error code: %d.\n", WSAGetLastError());
        closesocket(Socket);
        WSACleanup();

        system("PAUSE");
        return -1;
    }
    else
        printf("Server: accept() OK!\n");

    /*Receive and send data*/
    //return_Code = recv(Socket, recvBuff, DEFAULT_BUFFLEN, 0);
    do {

        return_Code = recv(Socket, recvBuff, DEFAULT_BUFFLEN, 0);//THIS LINE RETURNS -1, SO THE MESSAGE IS NEVER PROCESSED.
    //IT GOES TO RECEIVE FAILED THEN SHUTDOWN FAILED.
        if (return_Code > 0)
        {
            if (recvBuff[0] == 'G' && recvBuff[1] == 'E' && recvBuff[2] == 'T')
            {
                printf("GET MESSAGE RECEIVED");
            }
            else if (recvBuff[0] == 'S' && recvBuff[1] == 'E' && recvBuff[2] == 'T')
            {
                printf("SET MESSAGE RECEIVED");
            }
            else
            {
                printf("Error: GET and SET NOT RECIEVED");
            }
        }
        else if (return_Code == 0)
            printf("Server: Connection closed!");
        else
            printf("Server: recv() failed! Error code: %d.\n", WSAGetLastError());

    } while (return_Code > 0);

    /*Disconnect*/
    if (shutdown(Socket, SD_BOTH) != 0)
        printf("Server: shutdown() failed! Error code: %d.\n", WSAGetLastError());
    else
        printf("Server: shutdown() OK!\n");

    getchar();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

Socket是正在侦听连接的套接字。 AcceptSocket是您的程序与客户端之间的连接。您需要使用recv拨打AcceptSocket,而不是Socket

此外,shutdown不适用于侦听套接字,仅适用于已连接的套接字。同样适用 - 您可以shutdown AcceptSocket但不能Socket