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 |
Why do we need socket programming?
How should I choose a port number for my server?
What's the difference between a socket and a port?
How can I read only one character at a time?
How often should I re-transmit un-acknowleged messages?
How do I get the port number for a given service?
Whats the difference between select() and poll()?
What is a deep well socket?
How do I convert a string into an internet address?
What are raw sockets?
How to find other end of unix socket connection?
Is there any advantage to handling the signal, rather than just ignoring it and checking for the EPIPE error? Are there any useful parameters passed to the signal catching function?