2

これは私が実行しようとしているプログラムです:

$host = "127.0.0.1";
$port = 25003;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);

WAMP で PHP_socket オプションを有効にしましたが、バインドできないなどのエラーが発生し続けます。誰かが私を助けてくれますか?

4

1 に答える 1

0

私のために働きます。

考えられる問題:

1) 実行中のプログラムの別のコピーがあり、ポートをロックしています。

2) クライアントは「127.0.0.1」ではなく「localhost」に接続しようとします。Windows では、必ずしも同じではないことに気付きました。ネットワークのインストールによっては、「localhost」::1は IPv6 にマップされ、 127.0.0.1IPv4 にマップされる場合があります。

テスト

$ php -q test.php &
[1] 2855

# Now we try again a SECOND copy...
$ php -q test.php

PHP Warning:  socket_bind(): unable to bind address [98]: Address already in use in test.php on line 9
Could not bind to socket

$ telnet 127.0.0.1 25003
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HELLO WORLD.
Client Message : HELLO WORLD..DLROW OLLEH
Connection closed by foreign host.
于 2012-09-29T15:25:48.307 に答える