Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is a sae socket?

971


What is the difference between SO_REUSEADDR and SO_REUSEPORT?

1842


What is a socket api?

932


What are the pros/cons of select(), non-blocking I/O and SIGIO?

1405


How is a socket created?

968


What is difference between socket and websocket?

957


What is the difference between read() and recv()?

2016


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?

1485


What is a socket address?

962


What's better 6pt or 12pt sockets?

864


What is af_inet in socket?

873


Can a socket have multiple ports?

913


How can I write a multi-homed server?

1689


How can I set the timeout for the connect() system call?

1545


What's the difference between a socket and a port?

898