Page 1 of 1

[solved] IP address of socket

Posted: Mon Oct 20, 2008 4:22 pm
by Syntac
I've written a multi-client PHP server. It uses TCP sockets (socket_create(), socket_write(), etc.). However, some functions of the server require me to get the IP address of a client. I've tried socket_getpeername(), which returns 127.0.0.1.

However, the PHP documentation says this shouldn't be used on sockets that have been created with socket_accept(). I also don't know whether the address being returned is the local address or the client's address, and I don't have access to any other computers on which I could test this.

Re: IP address of socket

Posted: Mon Oct 20, 2008 4:40 pm
by requinix
Syntac wrote:I also don't know whether the address being returned is the local address or the client's address
So I guess you don't recognize 127.0.0.1?

Try socket_getsockname.

Re: IP address of socket

Posted: Mon Oct 20, 2008 4:53 pm
by Syntac
Perhaps I could have worded that better.

Yes, I know what 127.0.0.1 is. However, I'm running the client on the same machine as the server. Therefore, both addresses are 127.0.0.1. I need to know which address is being returned: the server's or the client's. While they are both the same, this would not be the case with a client on a different machine. Using the server's address in this case would screw up all sorts of things.

socket_getsockname() queries the local side of the socket, which returns the server's address (correct me if I'm wrong).

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:02 pm
by VladSun
The documentation says that:
Note: socket_getpeername() should not be used with AF_UNIX sockets created with socket_accept().
I think you use AF_INET or AF_INET6, so it's not affected.

And what's wrong if the client IP is 127.0.0.1 and it's the same as the one server binds to? I couldn't understand it.
127.0.0.1 returned by socket_getpeername() is still the client one.

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:09 pm
by Syntac
Dur, right, I missed the AF_UNIX part.

What's wrong with using the server's IP address? Well, my code stores client information based on IP. This information is accessed using the client's IP. Now, suppose I have someone from 123.12.123.1 connecting to my server and changing their username from "foo" to "bar". The change won't be applied, because the new username is being stored for 127.0.0.1 instead of 123.12.123.1. Thus, the person from 123.12.123.1 will remain "foo" and not "bar", leading them to ask what the hell is wrong with my server.

Anyway, I'll just go ahead and use socket_getpeername() for the time being, and test it when I eventually manage to requisition another machine from the IT department.

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:15 pm
by VladSun
You are NOT using the server IP address ... If you access the server from its machine, obviously your IP (the client one) is the same as the server one...
If you access the server from another machine, the IP will not be 127.0.0.1.

I still can't understand you :)

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:25 pm
by Eran
If you don't have access to a remote server for testing, I suggest setting up a virtual machine for testing the sockets and Ip's you are getting.

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:32 pm
by Syntac
VladSun wrote:You are NOT using the server IP address ... If you access the server from its machine, obviously your IP (the client one) is the same as the server one...
If you access the server from another machine, the IP will not be 127.0.0.1.
Er... Yes, we've established that. Let's say no more about this, shall we?

@pytrin: Well, I just called the IT guys and they're sending one over already. Normally they take a week...

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:40 pm
by Eran
Sending what over? VM is a piece of software and there are many open-source (RE: free) solutions available.

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:55 pm
by Syntac
:roll: Another computer, that is. Which I can use to test my code.

And I do know what a virtual machine is, thank you so very much.

Re: IP address of socket

Posted: Mon Oct 20, 2008 5:56 pm
by VladSun
Syntac wrote:
VladSun wrote:You are NOT using the server IP address ... If you access the server from its machine, obviously your IP (the client one) is the same as the server one...
If you access the server from another machine, the IP will not be 127.0.0.1.
Er... Yes, we've established that. Let's say no more about this, shall we?
Have we?!?
I really don't like your choice of tone ...
You are not able to explain what's your problem and I'm guilty for that?!?

Have a nice day .....

Re: IP address of socket

Posted: Mon Oct 20, 2008 6:14 pm
by Syntac
All right, all right, I'm sorry I came off as a little standoffish. The problem is exactly as follows:

I'm trying to get a client's IP address. However, since I'm testing both the client and the server on the same machine, the client's IP is the same as the server's. This is confusing because I don't know which address is being returned: The client's or the server's. There would be problems if it turns out my server is actually getting its own IP and not the client's. Hence the somewhat obfuscated question: Which address is returned by getpeername()?
Vladsun wrote:You are not able to explain what's your problem and I'm guilty for that?!?
I think you'll find I'm explaining it in no uncertain terms, it's just that you are the one who isn't understanding it. And I never said anything was your fault.

*sigh* If I sound like I'm flaming, do excuse my harsh posting style. I've had a long day and, frankly, all these computer-illiterate people who keep calling me on my cell phone, wasting my money, are driving me insane.

tl;dr version: I'm <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> off (looks like this forum has censoring turned on) and it may reflect in my posting style. I apologize for that. However, I've tried my best to explain the problem, and it's not my fault if that explanation is lost on the reader. Again, no hostility or offense is intended.

Re: IP address of socket

Posted: Mon Oct 20, 2008 6:20 pm
by VladSun
It's clearly written in the manual:

http://bg.php.net/socket_getpeername
Description
bool socket_getpeername ( resource $socket , string &$address [, int &$port ] )

Queries the remote side of the given socket which may either result in host/port
You always get the remote address (i.e. the client one)

Re: IP address of socket

Posted: Mon Oct 20, 2008 6:23 pm
by Syntac
Argh, what do I have to do to make you people stop helping me? :wink:

I should really read the documentation more carefully. Completely missed the "remote side" part. Problem solved. Good night.