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

Issues with UDP client to UDP server with 10057 error

本文关键字:UDP 出现 10057 错误 客户端 服务器 问题      更新时间:2023-10-16

我在连接到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")
#define ZeroMemory
using namespace std;
WSADATA wsadata;
SOCKET  Client;
SOCKADDR_IN Server;
unsigned int Port = 5020;
int ret;
char buf[4096];
int len, tolen, fromlen, namelen;

int main(int argc, char * argv[])
{
    // Initialize Winsocket
    ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
                // CHECKS THE SOCKET STATUS
                if (ret == SOCKET_ERROR)
                {
                    printf("Client: Winstock Status is: %s ", wsadata.szSystemStatus);
                    WSACleanup();
                    cout << endl;
                }
                else
                {
                    printf("Client: Winstock Status is: %s ", wsadata.szSystemStatus);
                    cout << endl;
                }
                // Client Address
                Server.sin_family = AF_INET;
                Server.sin_port = htons(Port);
                inet_pton(AF_INET, "127.0.0.1", &Server.sin_addr);
    // Create Socket
    ret = Client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    // CHECKS IF SOCKET IS CREATED
                if (Client == INVALID_SOCKET)
                {
                    cout << "Client: Can't Create Socket! ERROR: %s " << WSAGetLastError();
                    WSACleanup();
                    cout << endl;
                }
    // Bind Socket
    ret = bind(Client, (sockaddr*)& Server,sizeof(Server));
                // CHECKS IF SOCKET IS BINDED
                if (ret == INVALID_SOCKET)
                {
                    cout << "Client: Failed to bind socket" << WSAGetLastError(); ;
                    cout << endl;
                }
                string s(argv[1]);

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

    // CloseSocket
    closesocket(Client);
    // CleanUp Winsocket
    WSACleanup();
    return 0;
}

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

我先执行了客户端,没有问题,执行服务器然后客户端,这就是它显示问题的时候。

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

两个错误:

  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() 设置套接字的本地地址,而不是远程地址。对远程地址使用 connect()(使用 sendto() 时不需要这样做(。