UDP "connection" between two executables isn't working

What I’m trying to do is send information between two executables, one compiled using the cUDP::init_server function and the other using cUDP::init_client function.

In the server executable it was supposed to receive information from the client executable and print on the console, but for some reason it is not getting anything from him

Heres my UDP class definition using SDL_net:

void cUDP::init_client(const tString& sIP, const Uint16&& iPort)
{
    if(!cUDP::bConnected) {
        if(SDLNet_Init() == -1)
            throw cLog("\n\n[ERROR] Failed to initialize NET support.\n\nERROR: %s\n\n", SDLNet_GetError());

        cUDP::aSocket = SDLNet_UDP_Open(iPort);

        if(!cUDP::aSocket)
            throw cLog("\n\n[ERROR] Failed to open socket.\n\nError: %s\n\n", SDLNet_GetError());

        if(SDLNet_ResolveHost(&cUDP::oIP, sIP.c_str(), iPort) == -1)
            throw cLog("\n\n[ERROR] Failed to resolve host.\n\nError: %s\n\n", SDLNet_GetError());

        cUDP::bConnected = true;
    }
}

void cUDP::init_server(const Uint16&& iPort)
{
    if(!cUDP::bConnected) {
        if(SDLNet_Init() == -1)
            throw cLog("\n\n[ERROR] Failed to initialize NET support.\n\nERROR: %s\n\n", SDLNet_GetError());

        cUDP::aSocket = SDLNet_UDP_Open(iPort);

        if(!cUDP::aSocket)
            throw cLog("\n\n[ERROR] Failed to open socket.\n\nError: %s\n\n", SDLNet_GetError());

        if(SDLNet_ResolveHost(&cUDP::oIP, nullptr, iPort) == -1)
            throw cLog("\n\n[ERROR] Failed to resolve host.\n\nError: %s\n\n", SDLNet_GetError());

        cUDP::bConnected = true;
    }
}

void cUDP::destroy()
{
    if(cUDP::bConnected) {
        SDLNet_FreePacket(cUDP::aPacket);
        SDLNet_Quit();

        cUDP::bConnected = false;
    }
}

void cUDP::create_packet(const int&& iPacket_Size)
{
    if(cUDP::bConnected) {
        cUDP::aPacket = SDLNet_AllocPacket(iPacket_Size);

        if(!cUDP::aPacket)
            throw cLog("\n\n[ERROR] Failed to alloc packet.\n\nError: %s\n\n", SDLNet_GetError());

        cUDP::aPacket->address.host = cUDP::oIP.host;
        cUDP::aPacket->address.port = cUDP::oIP.port;
    }
}

void cUDP::send_data(const Uint8& iData)
{
    if(cUDP::bConnected) {
        memcpy(cUDP::aPacket->data, &iData, sizeof(Uint8));
        cUDP::aPacket->len = sizeof(Uint8);

        if(!SDLNet_UDP_Send(cUDP::aSocket, -1, cUDP::aPacket)) {
            throw cLog("\n\n[ERROR] Failed to send data.\n\nError: %s\n\n", SDLNet_GetError());
        }
    }
}

const Uint8 cUDP::receive_data()
{
    if(SDLNet_UDP_Recv(cUDP::aSocket, cUDP::aPacket))
        return *(cUDP::aPacket->data);
    else
        return 0;
}

the client version is compiled using:

cTest_Zone::cTest_Zone()
{
    cUDP::init_client("192.168.15.14", 111);
    cUDP::create_packet(512);
}

void cTest_Zone::update(const Uint32& iDelta)
{
    Uint8 iData{55};
    cUDP::send_data(iData);
}

And the server version is compiled using:

cTest_Zone::cTest_Zone()
{
    cUDP::init_server(222);
    cUDP::create_packet(512);
}

void cTest_Zone::update(const Uint32& iDelta)
{
    cLog("%d\n", cUDP::receive_data());
}

Just passing to say that I solved the problem and I dunno how to delete this post

Better idea: Share the solution, maybe it’ll be helpful for others that run into the same problem

Remove the SDLNet_ResolveHost function from cUDP::init_Server and inside the cUDP::init_client you need to pass the client port as the parameter of SDLNet_UDP_Open and the server IP and port as the second and third parameters of SDLNet_ResolveHost

1 Like

And for internet connections (non-local connections) you need to open a port in your router (I recommend use the “Half Life Server” port)