1

I am working on a game written in C using SDL. Given that it already uses SDL, SDL_image, and SDL_ttf, I decided to add SDL_mixer and SDL_net to my engine. Getting SDL_mixer set up and working was very easy, but I am having a lot of trouble with SDL_net.

To test I created a very simple application with the following rules:

  • Run without arguments act as a TCP server on port 9999
  • Run with an argument try to connect to the server at the given IP address on port 9999

Here are some of the key lines of the program (I'm not going to post my whole event-driven SDL engine because its too long):

char *host = NULL;
if (argc > 1) host = argv[1];

and...

IPaddress ip;
TCPsocket server = NULL;
TCPsocket conn = NULL;
if (host) { /* client mode */
    if (SDLNet_ResolveHost(&ip,host,port) < 0)
        return NULL; //this is actually inside an engine method
    if (!(conn = SDLNet_TCP_Open(&ip))) 
        return NULL; 
} else { /* server mode */
    if (SDLNet_ResolveHost(&ip,NULL,port) < 0)
        return NULL;
    if (!(server = SDLNet_TCP_Open(&ip)))
        return NULL;
}

and... inside the event loop

if (server) {
    if (!conn) 
        conn = SDLNet_TCP_Accept(server);
}
if (conn) {
    void *buf = malloc(size); //server, conn, size are actually members of a weird struct
    while (SDLNet_TCP_Recv(conn,buf,size))
        onReceive(buf); //my engine uses a callback system to handle things
    free(buf);
}

The program seems to start up just fine. However for some reason when I run it in client mode trying to connect to my home computer (which I have on a different IP) from my laptop I find that the call to SDLNet_TCP_Open blocks the program for awhile (5-10 seconds) then returns NULL. Can anybody see what I did wrong? Should I post more of the code? Let me know.

LambdaBeta
  • 21
  • 3
  • 1. is the port open? use [netstat](https://en.wikipedia.org/wiki/Netstat). 2. is it accepts TCP connections? use [telnet client](https://en.wikipedia.org/wiki/Telnet). – keltar Feb 21 '16 at 07:01
  • I'm sorry, I'm not well versed in how computer networks operate. How do I check the port using netstat and telnet? My guess would be: `netstat -a | grep 9999` and `telnet 9999` but I can't say I really understand the documentation. – LambdaBeta Feb 21 '16 at 19:56
  • Yes, maybe even `netstat -na | grep LISTEN | grep 9999` to show only listening ports (if you're on windows and you don't have grep installed, replace it with `findstr`). If port is open, `telnet localhost 9999`. If it connects, you can type message here, and your listening side should get it. If it doesn't - most likely your firewall is blocking it. – keltar Feb 22 '16 at 06:06
  • Ok, so port 9999 is not open. So I tested it on port 80. Once again it listens fine, but now when I try to connect the window goes non-responsive. Either way I want to release this game eventually, so I can't have SDL hanging while trying to connect. Is there any non-blocking way to send data between two computers? – LambdaBeta Feb 27 '16 at 04:27
  • Lower ports (<1024) requires elevated privileges, so it is unlikely you'll be able to open them. I generally dislike SDL_net design, but it shouldn't be a problem here. Networks sockets are non-blocking, but it may not be easy to use them directly, and name resolution is blocking on most platforms, which adds another problem. There are decent solutions like libuv or enet, you can check it out, or just stick with OS sockets directly, it is good to know them anyway. – keltar Feb 27 '16 at 04:58
  • I've never used OS sockets directly. Could you link me to a good tutorial on OS sockets in C? Can they be done in a cross-platform manner (I assume by using preprocessor directives)? – LambdaBeta Feb 27 '16 at 17:00

0 Answers0