0

So I am pretty new to sockets and C++ in general. I am trying to set a socket up on one computer that takes the IP address of that computer so I can connect to it from another. I am setting the IP address and the Port. However when it come to bind it always fails.

So I set the IP address to that of my computer and I set a port.

#define DEFAULT_PORT "8080"
#define DEFAULT_IP "165.120.216.66"

I then call WSAStartup which all seems to go fine. I then Resolver the server address and port.

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_IP;
hints.ai_flags = AI_PASSIVE;

Followed by getaddrinfo and create the socket. All of which goes fine

status = getaddrinfo(DEFAULT_IP, DEFAULT_PORT, &hints, &data);   
MySocket = socket(data->ai_family, data->ai_socktype, data->ai_protocol);

However when it comes to bind it always fails, and I just don't know why.

status = bind(MySocket, data->ai_addr, (int)data->ai_addrlen);

Also is there any way I can get the IP address of the machine it is running on instead of coding the IP?

BabyGiant
  • 21
  • 2
  • 5
  • 1
    Does the documentation for `bind` specify some way to determine why it failed? – David Schwartz Dec 11 '15 at 11:09
  • It's not uncommon to have something already listening on port 8080: have you tried any other ports (pick a random 5-digit number, and/or look at the output of `netstat -a` and avoid ports listed with a state of `LISTEN[ING]`. (And, as David says above, `bind()` should indicate _why_ it failed (e.g. `errno` or `WSAGetLastError()`). – TripeHound Dec 11 '15 at 11:40
  • It gives error 10049 - Cannot assign requested address. It says the address may not be valid? How so? I changed the port number – BabyGiant Dec 11 '15 at 11:45
  • Does it work (as in `bind()` succeeds) if you use `127.0.0.1`? If so, then it doesn't like the particular address you're using. `165...` isn't the normal range for an internal address: is it _really_ the IP address of your machine, or the public IP address of your connection to the internet (ping-ing it suggests BT). – TripeHound Dec 11 '15 at 11:56
  • ahh ok super cheers. Believe it was the wrong ip – BabyGiant Dec 11 '15 at 12:01

2 Answers2

1

If I understand you correctly, this would be the server side. So you would like to be able to connect to this socket from an other machine? Then you don't need to bind to a specific address and should start to listen afterwards. E.g.:

status = getaddrinfo(NULL, DEFAULT_PORT, &hints, &data); // get data for the local address 
MySocket = socket(data->ai_family, data->ai_socktype, data->ai_protocol);
bind(sockfd, res->ai_addr, res->ai_addrlen);
listen(sockfd, 10)
tnull
  • 728
  • 5
  • 17
  • it always defaults to localhost and then I cant connect to it from my client? – BabyGiant Dec 11 '15 at 11:34
  • Actually, it defaults to a wildcard. Listening on a specific IP address means, that only clients which connect to this IP can connect. Supplying `getaddrinfo` with `NULL` returns host data which has this wildcard set, so that your server will be reachable via all available IP addresses / interfaces. – tnull Dec 11 '15 at 11:39
  • how do you find out which IP address it is set to? – BabyGiant Dec 11 '15 at 11:41
  • 1
    As I told you, it is not set to a specific address. If you *have* to get the local IP address, have a look at this: http://stackoverflow.com/a/1317284/793929 But this is not advised and, as the above link describes, is tricky. Also, when you try to communicate over the internet, you want an IP address which is reachable from outside your local network. This often differs (due to [NAT](https://en.wikipedia.org/wiki/Network_address_translation)) from the addresses you can locally determine. – tnull Dec 11 '15 at 11:45
0

Just set the address to INADDR_ANY: "0.0.0.0".

user207421
  • 305,947
  • 44
  • 307
  • 483