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_p
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.