programmatically get the IP of a client

Posted on 2003-03-25
Medium Priority
Last Modified: 2012-08-13
Hello!I'm writing my first winsock program with Borland Builder C++ 5 (but i use the microsoft's sockets implementation).I have a server that accepts incoming connections from multiple clients.Now I want to make the server able to get the IP address of the clients that connect to it.How can i do it?I looked a bit at gethostbyaddr() and other functions in the windows SDK but i didn't quite understand how those functions work and what they do exactly,so,i don't know if those functions will serve my purposes.
Could somebody guide me to the right way to get the clients'IP?
Thanks in advance!
Question by:classobaniera
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 8206570
The gethostbyaddr functions requires that you know the address, so it's not what you want.

Instead, somewhere in the server you'll have a call to accept (or possibly WSAAccept).  These functions take optional arguments for the address and length of a sockaddr structure.   When the function returns, the sockaddr structure will be filled in with the IP address, among other things.


Author Comment

ID: 8208809
could you please explain to me how this is done using accept()?I tried but i didn't find how to do it.I'm a beginner and i keep getting confused with sockaddr structs and all this!
Thanks a lot!

Accepted Solution

GaryFx earned 140 total points
ID: 8213389
The basic steps for setting up a simple server are:

1.  Create a socket with the socket function, using AF_INET as the protocol
2.  Bind it to a port on the local machine with bind.  Note that the sockaddr structure for this does not need the ip address of the client.  Just create a sockaddr_in structure, set the family to AF_INET, set the port number using WSAHtons (to put the port number into network byte order), and set the local address to INADDR_ANY using WSAHtonl.
3.  Listen for connections with listen
4.  Accept connections with accept, passing a new, empty, sockaddr_in structure.  

accept returns a new socket, which is the one you actually use for reading/writing.  It also writes the information about the client ip into the empty sockaddr_in structure.  You need to use WSANtohl to convert the ip address back to host byte order.

One thing that may be confusing is that sockets are a very generic interface, designed for a variety of networks (internet, Novell, Appletalk, DECnet, etc.).  Each protocol has a different sockaddr structure, while the functions all take the generic sockaddr.  Since I'm assuming you want TCP/IP sockets, you'll just always use sockaddr_in structures, and then cast them to be sockaddr when necessary to pass them to the functions.


Author Comment

ID: 8217067
Hi Gary!
I found what i was looking for.This is how i get the client's ip that connects to my server .The accept() function on the server uses the claddr parameter in the place of ip address, so i use inet_ntoa to get it back as an AnsiString (ip):

ip = inet_ntoa ((struct in_addr )claddr.sin_addr);

 thank you for your help and comments!
LVL 11

Expert Comment

ID: 9558857
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to GaryFx

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question