How do I get my server to find out the clients address / host- name?
Answer Posted / 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 View All Answers
What is socket address with example?
Can a single socket port be used for multiple applications?
What is a sae socket?
What is a socket set used for?
What is socket programming in java?
How do I use TCP_NODELAY?
What pieces of information make up a socket?
How can I tell when a socket is closed on the other end?
Is socket a hardware or software?
Why do we need sockets?
How many sockets can a port have?
How can I force a socket to send the data in its buffer?
How do unix sockets work?
Is a socket a file?
How many socket connections can a server handle?