Sign in to follow this  
Neon2302

Need help in C++ winsock

Recommended Posts

Neon2302    122
I'm just create Socket program using C++/ winsock I just copy the example from MSDN everythings work find but since in the example the ip address is 127.0.0.1 and port is 27015 on both client and server I just change port to "2302" no problem occur. as I know the client need to set the ip to the actual server so I find my ip address and set the new ip for the client the server ip adress setting to 127.0.0.1 as the same. (I try to change it once but error occur) the result is client can't connect to server any one can tell me how to connect to server on the real use (I mean not on the same computer as 127.0.0.1) Thank you the second question is, what is ip address setting on the server use for? I still don't see the point of setting ip address on server in "sockaddr_in" struct Thank you Chet Chetchaiyan

Share this post


Link to post
Share on other sites
Dinsdale    255
Hello.

I'll try to answer your second question as it leads to answering the first one.

In your computer you can have multiple network interfaces: one or more LAN cards, wireless adapter, etc. Each of this interfaces (if it's enabled) has been assigned an IP address, either manually or via DHCP, or other autoconfiguration method. Each interface has a different IP address because it's used to connect to different network segment. So by IP address you can uniquelly identify your network interface. You can list your active (enabled) interfaces in the command line using "ipconfig /all". One of the interfaces is the so called local loopback adapter which you will not see in the list but it's there. The local loopback interface always occupies the IP address 127.0.0.1 (the name 'localhost' also translates to this IP address). The local loopback interface serves for socket communication between applications on the same computer, it can't communicate to the 'outside world'. When you bind your socket, you tie it with particular interface and port - that's why you have to specify not only the port but also IP address in the "sockaddr_in" structure. When you put the socket to listening state, it will receive packets only from the network interface you chose in sockaddr_in. However you can tell the socket to bind to (listen on) all available network interfaces by specifying the value "INADDR_ANY" as the IP address. There's no way to bind a socket to a subset of your network interfaces. Either it's bound to one of them or all of them.

Now we can get to the first question. You probably found your IP address by pinging your computer's name or by clicking the flashy network icon on the taskbar. This way you've got the IP address of the adapter used to communicate with the 'outside world'. Specifying this IP address as the server's socket IP makes the socket listen only on this particular interface. The server socket will thus not answer clients on the same computer trying to connect to it via the local loopback interface (IP 127.0.0.1). Knowing this gives you two options to solve your problem:

- Connect to the server using the correct IP (same as the server is listening on).
- Bind the server socket to all network interfaces, so it will listen also on 127.0.0.1 and you'll need not to change the IP with which the client is connecting.

Hope that makes things a bit clearer ;)

[Edited by - Dinsdale on October 14, 2007 1:54:46 AM]

Share this post


Link to post
Share on other sites
Neon2302    122
Thank you for your help and your explanation
I'm had less background in network programming, so I can't understand all of your explanation, but this still helpful for me.

Share this post


Link to post
Share on other sites
Feralrath    163
let me see if can make it a little clearer for you.

the reason you have to specify the IP address in the server is because it tells the server what IP address to listen to. the reason being is because one computer can have more then one IP addresses. this leads us back to your first question, because you changed the address that the server is listening on and left the client on 127.0.0.1 its not connecting to the server which is listening on your actual IP address.

so ass dinsdale said, either change the client to the same IP address that you put in the server. or just change the IP address of the server to 'INADDR_ANY' that way the server will listen on any IP address in your computer including the local 127.0.0.1.

i hope that makes things a little clearer for you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this