Home > database >  WinSock connecting between two machines on different networks goes wrong C
WinSock connecting between two machines on different networks goes wrong C

Time:03-02

I made a server/client program, everything works fine when I make a connection on the same network or on the same machine. This is however my problem: when I disconnect the client machine from the local network and connect it to another network(wifi) it doesn't connect to the server.

This is a part of my server:

        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        hints.ai_flags = AI_PASSIVE;

        res = getaddrinfo(NULL, PORT, &hints, &result);

Client:

        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;

        res = getaddrinfo(SERVER_HOST, PORT, &hints, &result);

The destination port in my client code is the same port as the one in my server. I read somewhere that I have to buy a unique IP address? So my question is: How can I connect two computers on different networks using sockets in C (WinSock)?

CodePudding user response:

Okay, I figured out that I need port forwarding on my router. Just contacted my network provider because I didn't had the option to make a new 'port forward rule'. I made a new rule in my router configuration. I put my private (internal) ip address into the new rule with a port number that I use in my server/client program. This is a nice resource.

  • Related