🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Network problems
I''ve started programming the server part of a startegy game I''m working on, and have run into some problems. (Probably) not programming related, but maybe someone can help me anyway.
I open a socket on port 4242 (with socket(), bind(), listen() and accept() ) and wait for conneciton. When a connection is established, it fork()s and wait for a new connection, with the child process handligh the connected socket.
Anyway, it works fine and I can try it with
chaos@ryush: ~> telnet localhost 4242
and connect (the server prints out some info about the connected socket and stuff). But if I try to connect from another computer on the network (a windows machine with IP 10.1.1.1, the server machine "ryush" has IP 10.1.1.3)
C:\> telnet 10.1.1.3 4242
it doesn''t connect and telnet terminates with the error "Connection to host lost". ''Normal'' telnet works fine
C:\> telnet 10.1.1.3
Connected to .... (and so on)
How do i configure my linux machine to accept incoming connections from other machines on the network on this port? I thought ports > 1024 was free-to-use and you simply had to call bind() on them.
Ah, nevermind. I fooled around with the /etc/hosts , /etc/hosts.allow and /etc/hosts.deny files and it now works. The strange thing is that it accepted normal telnet and ftp connection, but not to the port I opened in my server app.... well, doesn''t matter now.
Just in case anyone makes the same mistake as I did, I now post the reason to why things blew up in my face like that ![](smile.gif)
In the server code, I used gethostbyaddr() to get the hostname of the connected client. The problem is that I forgot to check the returned pointer. I happily used it right away. BUT, since gethostbyaddr() returns a null-pointer if it can''t resolve the ip address to a hostname, the process segfault:ed on me.
So, when fork():ing and then called gethostbyaddr() with the child process, the child process immediately segfault:ed without printing out the info about a client connecting -> I thought it hadn''t got any connection at all... *bangs head repeatedly against the wall*
This is the reason I got "connection to host lost" with the client, since it DID get a connection, but lost it right away when the socket was closed when the server child process crashed and burned.
And when I added the entry to /etc/hosts , gethostbyaddr() was able to get the hostname and therefor return a valid pointer -> all was good.
Note to self: ALWAYS check a returned pointer before using it. It''s as simple as that
![](smile.gif)
In the server code, I used gethostbyaddr() to get the hostname of the connected client. The problem is that I forgot to check the returned pointer. I happily used it right away. BUT, since gethostbyaddr() returns a null-pointer if it can''t resolve the ip address to a hostname, the process segfault:ed on me.
So, when fork():ing and then called gethostbyaddr() with the child process, the child process immediately segfault:ed without printing out the info about a client connecting -> I thought it hadn''t got any connection at all... *bangs head repeatedly against the wall*
This is the reason I got "connection to host lost" with the client, since it DID get a connection, but lost it right away when the socket was closed when the server child process crashed and burned.
And when I added the entry to /etc/hosts , gethostbyaddr() was able to get the hostname and therefor return a valid pointer -> all was good.
Note to self: ALWAYS check a returned pointer before using it. It''s as simple as that
![](smile.gif)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement