[solved] IP address of socket

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

[solved] IP address of socket

Post 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.
Last edited by Syntac on Mon Oct 20, 2008 6:24 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: IP address of socket

Post 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.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: IP address of socket

Post 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).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: IP address of socket

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: IP address of socket

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: IP address of socket

Post 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 :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: IP address of socket

Post 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.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: IP address of socket

Post 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...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: IP address of socket

Post by Eran »

Sending what over? VM is a piece of software and there are many open-source (RE: free) solutions available.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: IP address of socket

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: IP address of socket

Post 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 .....
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: IP address of socket

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: IP address of socket

Post 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)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: IP address of socket

Post 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.
Post Reply