7

I've opened a TCP socket server (I've omitted a few stuff, it is taken from here

sockfd = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol))

Is it possible to get the IP address of the server from sockfd? If not where should I look?

EDIT: I want to know the address of the server (this is before any client connects).

alk
  • 69,737
  • 10
  • 105
  • 255
michelemarcon
  • 23,277
  • 17
  • 52
  • 68
  • The I.P. address of which end? Yourself, or the place you've connected to? Or both?! – Joe Feb 05 '13 at 15:51
  • 1
    This socket isn't connected, so there's no address to get. After `accept`, `getsockname` will help. – ugoren Feb 05 '13 at 15:57
  • 1
    `accept()` itself (optioally) returns the address of the peer that connected (see `man accept`). – alk Feb 05 '13 at 17:34
  • The value return by a successful call to `socket()` isn't connected to any client nor bound to any interface, so there is no client side ip address nor a server side ip address that could be determined form anywhere. – alk Feb 05 '13 at 17:46
  • 2
    The IP address is ```p->ai_addr, p->ai_addrlen``` immediately after the ```for()``` loop, if ```p``` is not ```NULL```. This is simply because that's what the code ```bind```s the socket to. – Heath Hunnicutt Feb 05 '13 at 18:09

4 Answers4

13

If you want to know who's at the other end of your socket you can use getpeername in Linux. getsockname will tell you who you are. You decide what address you want your server to sit on initially though, at bind time.

You may also find this SO question useful: bind socket to network interface

And the book "Unix Network Programming, vol 1", by W. Richard Stevens.

Community
  • 1
  • 1
Joe
  • 7,378
  • 4
  • 37
  • 54
4

You can't use the socket to get the server's address before a client connects, because it isn't known.

In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.
Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1.

So the listening socket has no information about the IP.
You'll need to find what interfaces the machine has, and what's their IPs.

ugoren
  • 16,023
  • 3
  • 35
  • 65
  • This is only partially, namely in the special case where `bind()` was called using `INADDR_ANY`. See the correct answer given by *Davide Berra*. – alk Feb 05 '13 at 17:40
  • @alk, I assume that if he called `bind()` with a specific address, he wouldn't have asked what address it was. – ugoren Feb 05 '13 at 19:36
1

The address of the server is up to you.

Depends on which parameters are passed to the bind() function.

You can specify a single ip or bind your socket to every address of your host.

Look at the Bind man page

user207421
  • 305,947
  • 44
  • 307
  • 483
Davide Berra
  • 6,387
  • 2
  • 29
  • 50
0

The address of the server is the one that was passed to the successfull call to bind() (as shown in the source you linked).

alk
  • 69,737
  • 10
  • 105
  • 255