[ACCEPTED]-How to get the ip address of the accepted in-bound socket?-sockets

Accepted answer
Score: 11

Use getsockname(2) on the socket returned from accept(2), not the 1 socket returned from bind(2).

Score: 8

getsockname() gets name of passed socket. in this example, you 5 pass the socket which is created by accept 4 function in server. this socket is in server 3 side, so it's name & address is related 2 to server side.

if you want to know "who was connected to me" you 1 must use getpeername() instead of getsockname.

good luck

Score: 1

if you remove the following comment, that 4 means, if you call two times of getsockname, the 3 result will be correct. res = getsockname 2 (ConnectFD, (struct sockaddr *)&addr, &addr_len);

you 1 must init addr_len.

    addr_len = sizeof(addr);

More Related questions