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 can I read ICMP errors from connected UDP sockets?
If bind() fails, what should I do with the socket descriptor?
How can I force a socket to send the data in its buffer?
How do I convert a string into an internet address?
Is tcp or unix socket faster?
Why do we need socket programming?
How can I listen on more than one port at a time?
What is a socket set used for?
How can I tell when a socket is closed on the other end?
What is socket programming in java?
What exactly does SO_REUSEADDR do?
How do I send [this] over a socket?