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 the difference between read() and recv()?
What is the difference between a socket and a port?
What is the function of socket?
How do I use TCP_NODELAY?
How to find other end of unix socket connection?
What is the purpose of socket?
What is a socket api?
Whats the difference between select() and poll()?
What is socket programming in java?
Are sockets files?
What's better 6pt or 12pt sockets?
How does a socket work?
What is difference between socket () and serversocket () class?
Can a socket have multiple ports?
How many sockets can a port have?