Hi all.
I need help "binding" to a specific IP address.

My program is able to "bind" when using inaddr_any, but I can't seem to bind to a specific IP address using inet_addr("...."). I am basically trying to "bind" a socket to MY home pc's IP address, and a random port number (but the program simply refuses to bind to my home pc's IP address).

Can someone please help me to bind a socket to a specific IP address? Thanks in advance.

Recommended Answers

All 7 Replies

Could you please post a sample of your code?

Are you binding to your PC address or to your router address? They aren't usually the same and some people have made that mistake before.

I've heard that a PC generally has around two to three IP addresses in total. I'm using the IP address that was obtained off of: http://www.whatismyip.com/ I'm guessing that that is the "router" IP address. I am NOT using 127.0.0.1.

serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = inet_addr("124.22.127.123"); //My ip address
serverInfo.sin_port = htons(80); 
nret = bind(listeningSocket, (PSOCKADDR)&serverInfo, sizeof(struct sockaddr));

Yes, that will be your router address. Which is not equivalent to your PC address.
Other than that, your code is correct.

If you are using Windows XP, open up My Network Places, then under "Network Tasks" on the left click on "View Network Connections". You will then see a list of your connections. You should see something like "Local Area Connection 1", "Local Area Connection 2" etc.

One of these will look like two *blue* computers. The rest will either be grey or have a red X on them. On the blue coloured icon, double click it and it will say Local Area Connection status. Click on the "Support" tab and you will see some Status about your connection. You are interested in the field called "IP Address". That is the local machine address and the one which you need to bind to.

I'm using Windows 7, and I can't figure out for the life of me what the equivalent of My Network Places would be. Is there anyway you could give me a Windows 7 approach to finding the IP I need?

It's called the Network and Sharing Centre. In that window you will see Local Network and a blue "View Status" link. Then just follow the instructions as above.

Alternatively, open the run prompt (Windows Key + R)
type cmd and click OK
type ipconfig /all and hit enter

Typically your IP will start with either 192.168 or 10.

Thank you for your responses. A final question I have is, if I bind to my PC's IP address (and not its remote one) could I have people to attempt to connect to my pc's IP address (via winsock)?

Yes but you need to ask them to connect to your router IP address and set up port forwarding on your router.

If you need help with that I suggest posting on the Networking section of the forum (under the Hardware heading)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.