• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

programmatically get the IP of a client

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!
0
classobaniera
Asked:
classobaniera
  • 2
  • 2
1 Solution
 
GaryFxCommented:
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.

Gary
0
 
classobanieraAuthor Commented:
Gary,
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!
Classobaniera
0
 
GaryFxCommented:
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.

Gary
0
 
classobanieraAuthor Commented:
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!
0
 
bcladdCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now