Getting IP address of connected client

Red Squirrel

No Lifer
May 24, 2003
70,316
13,660
126
www.anyf.ca
For some reason this seems like something hard to do, a google search on this reveils lot of forums where peopled asked this same question but got a useless, or no answer. I found stuff on MS, but that won't help.. this is linux sockets.


I assume it has to do with the structure or something so here's the accept part of the code since some of those vars probably need to be used somehow, not even sure if those are predefined, or if they can change.

Code:
    clientAddressLength = sizeof(clientAddress);
    connectSocket = accept(listenSocket,(struct sockaddr *) &clientAddress,&clientAddressLength);
    if (connectSocket < 0) {
      NSS_logger("cannot accept connection ",2);
      exit(1);
    }
	
cout<<inet_ntoa(sockaddr.clientAddress);//my failed attempt

 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
man 7 ip

It points you in the right direction. Actually, the info you need is there.

Either that, or look up BSD sockets (that's what the API is).
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
accept() should give you the remote address, so should getpeername().

Now I'm not sure what you are trying to do here: "inet_ntoa(sockaddr.clientAddress);"

Is this tcp? If so try something like inet_ntoa(clientAddress.sin_addr);
If clientAddress is of type sockaddr, cast it to sockaddr_in first
 

Red Squirrel

No Lifer
May 24, 2003
70,316
13,660
126
www.anyf.ca
Sweet thanks! inet_ntoa(clientAddress.sin_addr); worked. I could not seem to find much info on this. It said the IP was in some struct, etc but did not say how to retrieve it from the struct. One more thing, how can I convert it to a string now? It apears to be a const char.

And just for learning purposes, what exactly is going on in this part inside the accept() function call?

(struct sockaddr *) &clientAddress
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: RedSquirrel
One more thing, how can I convert it to a string now? It apears to be a const char.

inet_addr should return a char* or a const char*, both of which are strings (c-style strings as opposed to C++ std::strign's). cout can display c-style strings just fine.

And just for learning purposes, what exactly is going on in this part inside the accept() function call?
(struct sockaddr *) &clientAddress

it's taking the address of clientAddress, then casting the address (a pointer of type sockaddr_in*) to type sockaddr*

the struct part is necessary in only C programs for referring to structs, but you are using cout so you must be using C++, thus that part is not necessary.
 

Red Squirrel

No Lifer
May 24, 2003
70,316
13,660
126
www.anyf.ca
I should of clarified I was only using cout to see if it's working, what I really want to do is this:

NSS_logger("Received connection from " + inet_ntoa(clientAddress.sin_addr),0);

That function takes a string, then an int. That call does not work though.

And for my other question, so does that mean it's adding a section in the struct sockaddr called clientAddress, and putting the value in it? So it's not actually declaring a new struct then? the struct keyword kind of confused me since I figured it was only used when creating a new one, and not using one. I think I'll read up more on structs, it's something I never really looked at figuring it was old school since classes basically do the same.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: RedSquirrel
I should of clarified I was only using cout to see if it's working, what I really want to do is this:

NSS_logger("Received connection from " + inet_ntoa(clientAddress.sin_addr),0);

That function takes a string, then an int. That call does not work though.

try doing something like

string temp("Recei...");
temp = temp + inet_ntoa(...);
NSS_logger(temp.c_str());

but make sure you include <string> (no .h) and put a using namespace std; after it if you haven't

there are other ways of doing this but this should be the easiest.

And for my other question, so does that mean it's adding a section in the struct sockaddr called clientAddress, and putting the value in it? So it's not actually declaring a new struct then? the struct keyword kind of confused me since I figured it was only used when creating a new one, and not using one. I think I'll read up more on structs, it's something I never really looked at figuring it was old school since classes basically do the same.

no it means you take the same block of memory, but interpreting its content differently. the struct part is not necessary in c++, it's only used in c when you refer to a struct type.

Finally, I suggest that you read more on arrays, strings and pointers :)
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: dighn
Originally posted by: RedSquirrel
I should of clarified I was only using cout to see if it's working, what I really want to do is this:

NSS_logger("Received connection from " + inet_ntoa(clientAddress.sin_addr),0);

That function takes a string, then an int. That call does not work though.

try doing something like

string temp("Recei...");
temp = temp + inet_ntoa(...);
NSS_logger(temp.c_str());

but make sure you include <string> (no .h) and put a using namespace std; after it if you haven't

there are other ways of doing this but this should be the easiest.

And for my other question, so does that mean it's adding a section in the struct sockaddr called clientAddress, and putting the value in it? So it's not actually declaring a new struct then? the struct keyword kind of confused me since I figured it was only used when creating a new one, and not using one. I think I'll read up more on structs, it's something I never really looked at figuring it was old school since classes basically do the same.

no it means you take the same block of memory, but interpreting its content differently. the struct part is not necessary in c++, it's only used in c when you refer to a struct type.

Finally, I suggest that you read more on arrays, strings and pointers :)

He's under the impression that socket(), accept(), etc. are C++, which they are not. He needs to learn what pure C looks like.
 

Red Squirrel

No Lifer
May 24, 2003
70,316
13,660
126
www.anyf.ca
Those are C? What is the C++ way of doing it then? All the tutorials and stuff seem to be showing the old way I guess. Everything in C++ seems more simple then C, so if there's even a C++ version of sockets, then I'd love to learn it.

And yes I'll have to read up on pointers, structs, etc, just trying to get by for now but eventually I need to learn more about that stuff.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: RedSquirrel
Those are C? What is the C++ way of doing it then? All the tutorials and stuff seem to be showing the old way I guess. Everything in C++ seems more simple then C, so if there's even a C++ version of sockets, then I'd love to learn it.

yes they are C. most system level API functions are C, the same is true under windows. but you can still use them in C++.

there are probably some C++ socket libraries but thsoe would be third party libraries. i would just stick with these socket functions.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: RedSquirrel
Those are C? What is the C++ way of doing it then? All the tutorials and stuff seem to be showing the old way I guess. Everything in C++ seems more simple then C, so if there's even a C++ version of sockets, then I'd love to learn it.

And yes I'll have to read up on pointers, structs, etc, just trying to get by for now but eventually I need to learn more about that stuff.

To me C++ seems to unnecessarily complicate things and slow them down while its at it. The only thing I like about C++ is the string class. But you should be able to get by fine with chars. I think you just should stick with what you're doing now, a "hybrid" if you will.
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: RedSquirrel
Those are C? What is the C++ way of doing it then? All the tutorials and stuff seem to be showing the old way I guess. Everything in C++ seems more simple then C, so if there's even a C++ version of sockets, then I'd love to learn it.

And yes I'll have to read up on pointers, structs, etc, just trying to get by for now but eventually I need to learn more about that stuff.

On big projects where design really matters, mixing the two should happen cleanly; that is, there should a well-defined C++ application space, and a well-defined, classful interface for sockets. There do seem to be a few such general implementations already (some of them cross-platform), and many libraries already have networking interfaces defined, but if you require something specialized, roll your own. Just separate the pure C from the pure C++ from the interface.

But for smaller projects, hybrid is OK.
 

Red Squirrel

No Lifer
May 24, 2003
70,316
13,660
126
www.anyf.ca
Actually it's something I've been thinking of using, some kind of 3rd party socket class, something easy to use. ex: I just include a header file, read a couble of readmes and I'm set. Anyone know of a good one?