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 |
How many sockets can a cpu have?
What is af_inet in socket?
What is sae socket?
What does af mean in sockets?
What's the difference between impact sockets and regular sockets?
What is socket address with example?
What is the function of socket?
What is a socket address?
How many sockets can a port have?
Where can a get a library for programming sockets?
How is a socket created?
When will my application receive SIGPIPE?