SDLNet Networking Not Working

て烟熏妆下的殇ゞ 提交于 2019-12-23 01:44:34

问题


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.

来源:https://stackoverflow.com/questions/35527272/sdlnet-networking-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!