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

Are unix sockets faster than tcp?

0 Answers  


Is socket a hardware or software?

0 Answers  


What is socket programming in java?

0 Answers  


How come I get address already in use from bind()?

1 Answers  


Why sockets are used?

0 Answers  






What is a socket file?

0 Answers  


How come only the first part of my datagram is getting through?

1 Answers  


How do Sockets Work?

1 Answers  


Are sockets files?

0 Answers  


What does af mean in sockets?

0 Answers  


Why do we need socket programming?

0 Answers  


Explain the TIME_WAIT state.

1 Answers  


Categories