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 |
system choose one for me on the connect() call? Should I bind() a port number in my client program, or let the?
What is Mac, and how is it different from PC? Which one is better for professional users, and why
1 Answers Evolving Systems, IBM,
How many socket connections can a server handle?
Why does connect() succeed even before my server did an accept()?
What is the purpose of socket?
How can I write a multi-homed server?
How come I get address already in use from bind()?
What's better 6pt or 12pt sockets?
If bind() fails, what should I do with the socket descriptor?
over the socket? Is there a way to have a dynamic buffer? What does one do when one does not know how much information is coming?
Where is the socket located?
What is a deep well socket?