How do I get my server to find out the clients address / host- name?



How do I get my server to find out the clients address / host- name?..

Answer / chaitanya

After accept()ing a connection, use getpeername() to get the address of the client. The client's address is of course, also returned on the accept(), but it is essential to initialise the address-length parameter before the accept call for this will work.

int t;

int len;

struct sockaddr_in sin;

struct hostent *host;

len = sizeof sin;

if (getpeername(t, (struct sockaddr *)

&sin, &len) < 0)

perror("getpeername");

else {

if ((host = gethostbyaddr((char *)

&sin.sin_addr,sizeof sin.sin_addr,

AF_INET)) == NULL)

perror("gethostbyaddr");

else printf("remote host is '%s'n",

host->h_name);

}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Unix Socket Programming Interview Questions

How can I be sure that UDP messages are received in order?

1 Answers  


What is a socket set used for?

0 Answers  


What is the difference between SO_REUSEADDR and SO_REUSEPORT?

0 Answers  


How can I put a timeout on connect()?

1 Answers  


Where can a get a library for programming sockets?

1 Answers  






How can I set the timeout for the connect() system call?

0 Answers  


How does a socket work?

0 Answers  


What are the pros/cons of select(), non-blocking I/O and SIGIO?

0 Answers  


Is tcp or unix socket faster?

0 Answers  


What is a socket api?

0 Answers  


What is LILO?

2 Answers  


How does unix socket work?

0 Answers  


Categories