• Uncategorized

About linux : Getting-information-about-descriptor

Question Detail

let’s assume that my process accepted a connection and its descriptor = 1024.
Now, I would like to get information about that socket, for example about client port.

Is it possible to ask Linux about that having only descriptor?

Question Answer

For getting client address and port, you can use getpeername():

struct sockaddr_in peer_addr;
socklen_t len = sizeof(peer_addr);

if (getpeername(descriptor, (struct sockaddr*)&peer_addr, &len) == -1)
     // TODO: error handling

Information of own end address can be aquired by using getsockname().

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.