[solved] IP address of socket
Moderator: General Moderators
[solved] IP address of socket
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.
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.
Re: IP address of socket
So I guess you don't recognize 127.0.0.1?Syntac wrote:I also don't know whether the address being returned is the local address or the client's address
Try socket_getsockname.
Re: IP address of socket
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).
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
The documentation says that:
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.
I think you use AF_INET or AF_INET6, so it's not affected.Note: socket_getpeername() should not be used with AF_UNIX sockets created with socket_accept().
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
Re: IP address of socket
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.
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
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
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
Re: IP address of socket
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
Er... Yes, we've established that. Let's say no more about this, shall we?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.
@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
Sending what over? VM is a piece of software and there are many open-source (RE: free) solutions available.
Re: IP address of socket
And I do know what a virtual machine is, thank you so very much.
Re: IP address of socket
Have we?!?Syntac wrote:Er... Yes, we've established that. Let's say no more about this, shall we?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.
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
Re: IP address of socket
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()?
*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.
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()?
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.Vladsun wrote:You are not able to explain what's your problem and I'm guilty for that?!?
*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
It's clearly written in the manual:
http://bg.php.net/socket_getpeername
http://bg.php.net/socket_getpeername
You always get the remote address (i.e. the client one)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
There are 10 types of people in this world, those who understand binary and those who don't
Re: IP address of socket
Argh, what do I have to do to make you people stop helping me? 
I should really read the documentation more carefully. Completely missed the "remote side" part. Problem solved. Good night.
I should really read the documentation more carefully. Completely missed the "remote side" part. Problem solved. Good night.