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?



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

Post New Answer

More Unix Socket Programming Interview Questions

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?

1 Answers  


How can I tell when a socket is closed on the other end?

0 Answers  


What is difference between socket () and serversocket () class?

0 Answers  


system choose one for me on the connect() call? Should I bind() a port number in my client program, or let the?

0 Answers  


How do I properly close a socket?

1 Answers  


What is a deep well socket?

0 Answers  


How to find other end of unix socket connection?

0 Answers  


What's better 6pt or 12pt sockets?

0 Answers  


How come select says there is data, but read returns zero?

1 Answers  


Whats the difference between select() and poll()?

0 Answers  


What is the difference between SO_REUSEADDR and SO_REUSEPORT?

0 Answers  


How does unix socket work?

0 Answers  


Categories