How to specify the port that the client side of socket in PHP?

女生的网名这么多〃 提交于 2021-02-11 06:55:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!