问题
I want to bind a specified port to the CLIENT side of socket in PHP. Can I do this? How to do this? Using socket_bind()? but I dont know what should be set for the address
I have googled the solution about this question but no example/solution can be found.
Thanks for helping.
回答1:
Look here: http://php.net/manual/en/function.socket-bind.php
socket_bind ( resource $socket , string $address [, int $port = 0 ] )
So something like:
socket_bind($socket, 'localhost', 5555);
Of course, I don't know what the address of the host is, it might not be localhost.
回答2:
You can do it if you create the stream yourself with stream_context_create
You will need to use bindto:
Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is ip:port. Setting the IP or the port to 0 will let the system choose the IP and/or port.
So:
$ctx = stream_context_create(array('socket'=> array('bindto' => "0:1234")));
Then use the context as you see fit.
来源:https://stackoverflow.com/questions/12479452/how-to-specify-the-port-that-the-client-side-of-socket-in-php