UDP客户端到UDP服务器的问题,出现10057错误

时间:2019-06-04 18:55:23

标签: c++ sockets udp winsockets

我有一个连接到UDP服务器的问题。在VS上,它没有显示任何错误,除非我进行错误检查时,它给我一个错误10057。

UDP客户端:

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")

using namespace std;

WSADATA wsadata;
SOCKET  ServerSocket;
SOCKADDR_IN ServerAddress, Client;
unsigned int Port = 5020;
char buf[4096];
int ret;
int len, tolen, fromlen, namelen;

int main()
{

    // Initializing Socket
    ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
        if (ret == SOCKET_ERROR)
        {
            cout << "Server: Failed to initialized socket. Error: " << wsadata.szSystemStatus;
            cout << endl;
        }
        else
        {
            cout << "Server: Successfully initialized socket." << wsadata.szSystemStatus;
            cout << endl;
        }

    // Sever Address
    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_port = htons(Port);
    inet_pton(AF_INET, "127.0.0.1", &ServerAddress.sin_addr);

    // Create Socket
    ret = ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (ret == -1)
    {
        cout << "Server: Failed to create socket. Error: " << WSAGetLastError();
        cout << endl;
    }
    else
    {
        cout << "Server: Socket has been created ";
        cout << endl;
    }

    // Bind Socket
    ret = bind(ServerSocket, (sockaddr*)& ServerAddress, sizeof(ServerAddress));
    if (ret == -1)
    {
        cout << "Server: Failed to bind socket. Error: " << WSAGetLastError();
        cout << endl;
    }
    else
    {
        cout << "Server: Socket is binded to address ";
        cout << endl;
    }

    int ClientLength = sizeof(Client);

    while(true)
    {

        // receivefrom
        ret = recvfrom
        (
            ServerSocket,
            buf,
            len,
            0,
            (sockaddr*)& Client,
            &ClientLength
        );

        if (ret == SOCKET_ERROR)
        {
            cout << "Error receiving from client" << WSAGetLastError();
        }

        // display message
        char ClientIP[256];

        inet_ntop(AF_INET, &Client.sin_addr, ClientIP, 256);

        cout << "message recieve from: " << ClientIP << " : " << buf << endl;
    }

    // Close Socket
    closesocket(ServerSocket);

    // Cleanup Winsocket
    WSACleanup();

    return 0;
}

UDP服务器:

// SendTo()
ret = sendto
(
    Client,
    s.c_str(),
    s.size() + 1,
    0,
    (sockaddr*) & Server,
    sizeof(Server)
);

if (ret == SOCKET_ERROR);
{
    cout << "That did not work! Error Code: " << WSAGetLastError();
    cout << endl;
}

我首先执行了客户端(没有问题),然后执行了服务器,然后执行了显示问题的客户端。

[
    {
        "ID": "1",
        "NAME": "Enterprise 1",
        "DOC": "0000000000001",
        "EST": "1"
    },
    {
        "ID": "2",
        "NAME": "Enterprise 2",
        "DOC": "0000000000003",
        "EST": "3"
    }
]

1 个答案:

答案 0 :(得分:3)

两个错误:

  1. ret = Client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    如果要编写UDP客户端,则需要创建UDP套接字,而不是TCP套接字:

    ret = Client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
  2. ret = bind(Client, (sockaddr*)& Server,sizeof(Server));

    这是在客户端。为什么要尝试bind()到服务器的端点?您使用bind()来设置套接字的 local 地址,而不是 remote 地址。使用connect()作为远程地址(使用sendto()时不需要这样做)。