c - Recovering IP/Port from Socket Descriptor -
i'm writing a clone of inetd in must run server prints ip , port of client connecting it.
as overwrite stdin
, stdout
socket descriptor, my initial solution recover sockaddr_in
structure, contains needed information. doing getsockname()
, however, returning empty structure, bits set 0.
any idea of wrong approach? there other approaches can use recover ip/port?
thanks
as r.. pointed out, should use getpeername
. both function , getsockname
take file descriptor first argument, not stream pointer (file *
). use fileno(stdin)
file descriptor standard input (or hard-code stdin_fileno
, it's constant).
also, last argument getsockname
, getpeername
should pointer socklen_t
, not constant, , should use sockaddr_in
tcp/ip:
struct sockaddr_in peeraddr; socklen_t peeraddrlen = sizeof(peeraddr); getpeername(stdin_fileno, &peeraddr, &peeraddrlen);
see complete example here.
Comments
Post a Comment