soadlink Posted August 1, 2008 Share Posted August 1, 2008 Hello, If I create a udp socket, and send data on it like so: <?php $socket = fsockopen("udp://" . $ip, $port); fwrite($socket, "hello"); ?> ... is it possible to see the source port that the socket has bound itself to for the sending of data? I know I can packet scan or use something within the OS itself, but I'm looking for a way to store this source port as a string (within the script itself), and use it for whatever I need. I did some searching but wasn't able to find anything; however, it seems like it's probably very simple . Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/ Share on other sites More sharing options...
trq Posted August 1, 2008 Share Posted August 1, 2008 I doubt that information would be available at such a high level where php sits. Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605257 Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 I'm not aware that information like that is readily available in PHP also. Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605258 Share on other sites More sharing options...
bretticus Posted August 1, 2008 Share Posted August 1, 2008 Great question. I was searching through the stream functions for you. Zippo. Alas, you might try a program execution function together with "netstat -ap". I'm not sure if apache is the process owner or what. This is probably unlikely, but perhaps a hint in the right direction. Also, you'd probably have to keep the socket open until netstat returned the information. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605264 Share on other sites More sharing options...
soadlink Posted August 1, 2008 Author Share Posted August 1, 2008 I'm not sure if apache is the process owner or what. This is probably unlikely, but perhaps a hint in the right direction. Also, you'd probably have to keep the socket open until netstat returned the information. Good luck! I'm just using the CLI for this particular script, so no apache is involved. But I think you are correct in referring to using exec to call on netstat or something similar (and then just parse the info). I was hoping there was an easier way though . I did a little more reading, and I see that I can create a socket (socket_create), bind it to an address/port (socket_bind), and then have set it send out the data on that socket. And I know the source port because I can specify it during socket_bind. A little more code, but I think it would work. I'll try this, and also play with the netstat parsing, but if anyone has any other ideas feel free to post them. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605279 Share on other sites More sharing options...
bretticus Posted August 1, 2008 Share Posted August 1, 2008 From manual: The port parameter is only used when connecting to an AF_INET socket, and designates the port on the remote host to which a connection should be made. You sure about that? Anyways, if it works, great. I'd love to know. Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605283 Share on other sites More sharing options...
soadlink Posted August 1, 2008 Author Share Posted August 1, 2008 You sure about that? Anyways, if it works, great. I'd love to know. Yea, I just tested it and it worked. What you're referring to is the socket_connect function (that is where you specify the dest ip/port). The socket_bind function allows you to bind your particular socket to an address/port belonging to your machine before the connection is made. Here is working code: <?php $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_bind($socket, "192.168.1.2", "45456"); socket_connect($socket, $remoteip, $remoteport); socket_write($socket, "Hello World"); ?> So 192.168.1.2 has to be an ip belonging to my machine (or you get the error: Cannot assign requested address) , and I would assume 45456 would have to be unused by any other processes (you would most likely get another error). Going to mark this topic as solved, as socket_create seems to be a working solution (though not as easy as I assumed it would be ). Feel free to keep posting though if you guys wan't to continue the discussion, but I think I can manage to work this into my script from here. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605291 Share on other sites More sharing options...
bretticus Posted August 1, 2008 Share Posted August 1, 2008 Yea, I just tested it and it worked. What you're referring to is the socket_connect function I actually got that from the socket_bind page on the manual. Documentation is a little confusing because it sounds to me that that 3rd optional parameter is the destination port, not the source. Happy it works. Good to know. Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605301 Share on other sites More sharing options...
soadlink Posted August 1, 2008 Author Share Posted August 1, 2008 Interesting, might be a typo. I would agree that it sounds like they are talking about the destination port ???. I'll make a bug report and let them know just in case, but the bugs site seems to be down at the moment. Another good piece of info for socket_bind: use 0 in the source address field if you don't want to look up your machine's IP, and 0 in the port field for a random port. Quote Link to comment https://forums.phpfreaks.com/topic/117666-solved-how-can-i-get-the-port-a-socket-is-using/#findComment-605331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.