Winsock简单的Client Server,收到失败

时间:2017-01-09 00:13:06

标签: c++ windows networking winsock

我正在尝试使用winsock运行这个简单的服务器客户端程序。服务器正在接受客户端连接,但无法接收客户端发送的数据。我正在努力找到问题所在。

服务器:

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include<iostream>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib")

#define DEFAULT_PORT 18000

int main(int argc, char *argv[])
{
    WSADATA wsa;
    SOCKET s, new_socket;
    struct sockaddr_in server, client;
    char *message, client_msg[2000];
    int port, c, recv_size;

    if (argc > 2)
    {
        std::cout << "usage : " << argv[1] << " <PORT>\n";
        std::cout << "If no port is given then default port is used.\n";
        std::cout << "DEFAULT PORT : 18000\n";
        return 1;
    }
    if (argc == 2)
    {
        port = atoi(argv[2]);
    }
    else
    {
        port = DEFAULT_PORT;
    }

    std::cout << "Initializing Winsock...";
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        std::cout << "Failed. Error code" << WSAGetLastError();
        return 1;
    }
    std::cout << "Initialized.\n\n";

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
        std::cout << "Could not create Socket : " << WSAGetLastError() << "\n";
        return 1;
    }
    std::cout << "Socket Created.\n";

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(port);

    if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
    {
        std::cout << "Bind failed with error code : " << WSAGetLastError() << "\n";
    }
    std::cout << "Bind done.\n";


    listen(s, 3);

    std::cout << "Waiting for incoming connections...\n";

    while (true)
    {
        c = sizeof(struct sockaddr_in);

        new_socket = accept(s, (struct sockaddr*)&client, &c);
        if (new_socket == INVALID_SOCKET)
        {
            closesocket(new_socket);
            std::cout << "Accept failed with error code : " << WSAGetLastError() << "\n";
        }
        else
        {
            std::cout << "Connection accepted.\n";

            if ((recv_size = recv(s, client_msg, 200, 0)) == SOCKET_ERROR)
            {
                closesocket(new_socket);
                std::cout << "Receive Failed.\n\n";
            }
            else
            {
                if (strcmp(client_msg, "PASSWD"))
                {
                    message = "HELLO!";
                    send(new_socket, message, strlen(message), 0);
                }
                else
                {
                    message = "Authentication failed!";
                    send(new_socket, message, strlen(message), 0);
                    closesocket(new_socket);
                }
            }
        }
    }

    closesocket(s);
    WSACleanup();


    return 0;
}

客户端:

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include<iostream>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib")

#define DEFAULT_SERVER "127.0.0.1"
#define DEFAULT_PORT 18000
#define PASSWORD "PASSWD"

int main(int argc, char *argv[])
{
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;
    char *message, server_reply[2000], *server_addr;
    int recv_size, port = DEFAULT_PORT;

    if (argc > 4)
    {
        std::cout << "usage : " << argv[0] << " <Server IP> <Server Port> [DATA]\n\n";
        std::cout << "If any argument is missing, default values will be used.\n";
        std::cout << "Default Address : localhost (127.0.0.1)\nDefault Port : 18000\nDefault DATA: 'PASSWD'\n\n";
        return 1;
    }
    else if (argc == 4)
    {
        server_addr = argv[1];
        port = atoi(argv[2]);
        message = argv[3];
    }
    else if (argc == 3)
    {
        server_addr = argv[1];
        port = atoi(argv[2]);
        message = PASSWORD;
    }
    else if (argc == 2)
    {
        server_addr = argv[1];
        port = DEFAULT_PORT;
        message = PASSWORD;
    }
    else
    {
        server_addr = DEFAULT_SERVER;
        port = DEFAULT_PORT;
        message = PASSWORD;
        //server_addr = "127.0.0.1";
        //port = 2222;
        //message = "HELLO";
    }

    std::cout << "Initialising Winsock..\n";
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        std::cout << "Failed.. Error Code : " << WSAGetLastError() << "\n";
        return 1;
    }
    std::cout << "Winsock Initialised.\n";

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
        std::cout << "Could not create socket : " << WSAGetLastError() << "\n";
    }
    std::cout << "Socket Created.\n";

    server.sin_addr.s_addr = inet_addr(server_addr);
    server.sin_family = AF_INET;
    server.sin_port = htons(port);

    if (connect(s, (struct sockaddr*)&server, sizeof(server)) < 0)
    {
        std::cout << "Could not connect to " << server_addr << " on port " << port << ".\n";
        WSACleanup();
        return 1;
    }
    std::cout << "Connected to " << server_addr << " on port " << port << ".\n\n";

    if (send(s, message, strlen(message), 0) < 0)
    {
        std::cout << "Sending Data failed.\n";
        WSACleanup();
        return 1;
    }
    std::cout << "Data send.\n\n";

    //Sleep(500);

    if ((recv_size = recv(s, server_reply, 20, 0)) == SOCKET_ERROR )
    {
        std::cout << "Receive Failed.\n\n";
    }
    else
    {


        server_reply[recv_size] = '\0';

        std::cout << server_addr << " : " << server_reply << "\n";

    }
    //Sleep(5000);

    closesocket(s);
    WSACleanup();

    return 0;
}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

  
    

if((recv_size = recv(s,client_msg,200,0))== SOCKET_ERROR)

  

在服务器端代码中将s更改为new_socket。