Issues with UDP client to UDP server with 10057 error

952 Views Asked by At

I have an issues of connecting to my UDP server. On VS it showed no errors except when I do an error checking it give me a Error 10057.

UDP Client:

#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 Server:

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

I have executed the client first, no issues, executed server then client that's when it showed the issue.

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

There are 1 best solutions below

0
David Schwartz On

Two mistakes:

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

    If you are writing a UDP client, you need to create a UDP socket, not a TCP socket:

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

    This is on the client side. Why are you trying to bind() to the server's endpoint? You use bind() to set the local address of the socket, not the remote address. Use connect() for the remote address (which you don't need to do when using sendto()).