I’ve been working with socket programming lately; and I’m currently making a server that listens on a port for incoming connections, then once it gets one, reads in a string and places it in a file.
It all works fine when I’m telneting to the running server from localhost, but when I attempt to access it from anywhere else, it acts as though it doesn’t exist. Nmap doesn’t show that particular open port — from either the localhost or the remote host. It only works through telnet on localhost.
The function’s code is here. (Pastebin)
I know it’s a mess, I’m still pretty unfamiliar with network programming. There really aren’t any good in-depth tutorials that show you everything you need to know. I guess I’ll have to buy a book…
Your code is calling
where
bis based on the result of callinggetaddrinfo. This probably doesn’t contain exactly what you want. It looks likecliaddralready contains the correct data to pass tobind(), so use that instead:I’m not sure what
getaddrinfo()might return for you, but it sounds from your description like it might be providing the address 127.0.0.1 (which is localhost). If youbind()only to the localhost interface, then that’s the only address that will respond to request to connect. If you bind toINADDR_ANY, then all interfaces will respond to requests for connection.For a simple socket listening program, you probably don’t need the call to
getaddrinfo()at all.