Socket not working PHP
这是我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php error_reporting(E_ALL); /* Allow the script to hang around waiting for connections. */ set_time_limit(0); /* Turn on implicit output flushing so we see what we're getting * as it comes in. */ ob_implicit_flush(); $address = 'localhost'; $port = 10000; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo"socket_create() failed: reason:" . socket_strerror(socket_last_error()) ."\ "; } if (socket_bind($sock, $address, $port) === false) { echo"socket_bind() failed: reason:" . socket_strerror(socket_last_error($sock)) ."\ "; } if (socket_listen($sock, 5) === false) { echo"socket_listen() failed: reason:" . socket_strerror(socket_last_error($sock)) ."\ "; } do { if (($msgsock = socket_accept($sock)) === false) { echo"socket_accept() failed: reason:" . socket_strerror(socket_last_error($sock)) ."\ "; break; } do { $out = socket_read($msgsock, 2048); if (!empty($out)) { if ($out == 'quit') { break; } elseif ($out == 'shutdown') { socket_write($msgsock, 'plc down', 8); socket_close($msgsock); break 2; } else { switch ($out) { case"KABBE": $response ="Kabbe te!"; break; case"SZOPJ": $response ="Szopjal te!"; break; default: $response ="Ismeretlen parancs"; } socket_write($msgsock, $response, strlen($response)); break; } } } while (true); socket_close($msgsock); } while (true); socket_close($sock); ?> |
现在是错误
1 2 3 4 5 6 | Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\\wamp\\www\\socket\\socket.php on line 18 socket_bind() failed: reason: Only one usage of each socket address (protocol/network address/port) is normally permitted. Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: An invalid argument was supplied. in C:\\wamp\\www\\socket\\socket.php on line 22 socket_listen() failed: reason: An invalid argument was supplied. Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: An invalid argument was supplied. in C:\\wamp\\www\\socket\\socket.php on line 27 socket_accept() failed: reason: An invalid argument was supplied. |
我在Google上进行了搜索,但没有任何用处。
有什么问题吗?
PHP还提供了stream_socket_server和其他stream_socket_ *函数。
我发现它们对开发人员更友好。
来自php.net的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 | $socket = stream_socket_server("tcp://localhost:8000", $errno, $errstr); if (!$socket) { echo"$errstr ($errno)<br />\ "; } else { while ($conn = stream_socket_accept($socket)) { fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') ."\ "); fclose($conn); } fclose($socket); } |
" localhost"不是有效的地址,因为socket_bind不接受DNS名称,请使用等效的IP地址" 127.0.0.1"。
更多信息
在绑定之前使用此代码:
1 2 3 4 | if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { echo socket_strerror(socket_last_error($socket)); exit; } |
以供参考http://www.php.net/manual/zh/function.socket-bind.php
您也可以查看http://www.php.net/manual/en/function.socket-set-option.php了解详细信息
这意味着您在该端口上的计算机上已经有一个开放的插槽。
尝试切换到另一个未使用的端口。
在Windows上(这似乎是您正在使用的),您可以从命令行查看打开的套接字列表:
1 | netstat -an |
如果您想知道哪些进程正在侦听那些端口,请尝试以下操作:
1 | netstat -ban |