hi,

I want to know how to get name of connected peer.

I am creating a simple client server application using winsock2. When I use getpeername() function it gives me IP of client machine.

After connecting i did following step

sockaddr_in peeraddr;
	int size = sizeof(peeraddr);
	
	getpeername(New_Socket, (struct sockaddr *)&peeraddr, &size);
	
	struct sockaddr_in *s = (struct sockaddr_in *)&peeraddr;
   
	char* IP = inet_ntoa(peeraddr.sin_addr);

	cout << "Accepted connection from " << Name << "\r\n";

Recommended Answers

All 5 Replies

I don't fully get what you want to know?
You want to get peer name, and right below you post the function to get peer name? :)

I just want to know the HOST name of client machine. But i am only getting IP address of that machine by using getpeername function. I am new in SOCKET programming so please help if i am doing any mistake.

Example:
Suppose my machines host name is "Test". I can retrieve this by calling gethostname function. But what about host name of client machine which is recently get connected to server.

I just want to know the HOST name of client machine. But i am only getting IP address of that machine by using getpeername function. I am new in SOCKET programming so please help if i am doing any mistake.

Example:
Suppose my machines host name is "Test". I can retrieve this by calling gethostname function. But what about host name of client machine which is recently get connected to server.

Aha.

Checking the official MSDN page for Winsock, I found gethostbyaddr which returns the name in a hostent structure.

http://msdn.microsoft.com/en-us/library/ms738521%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms738552%28VS.85%29.aspx

If it does not give you the right name, you might just wanna use getHostName in the client app, and then sent the output back to the server?

hey thanks for help ;)

instead of using gethostbyaddr function i used getnameinfo Function.
It is giving the host name of client which is actually i want. Thanks buddy

Glad you got it working.
You're welcome (:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.