php simple socket_listen

时间:2012-10-19 12:37:23

标签: php sockets

我有以下内容:

$host = "192.168.0.117";
$port = 777;

set_time_limit(0);

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not createsocket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");

变量result给出了一个错误:

  

无法侦听套接字[0]:提供了无效参数

2 个答案:

答案 0 :(得分:1)

首先需要将套接字绑定到(地址,端口)对,然后再调用listen。

答案 1 :(得分:1)

$host = '192.168.0.117';
$port = '777';

set_time_limit(0);

$socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($socket, $adddress, $port) or die("Could not connect");
socket_listen($socket);

$client_connection = socket_accept($socket);
$input = socket_read($client_connection, 1024);

$output = ereg_replace('[\t\n\r]','', $input).chr(0);

//display output
socket_write($client_connection, $output);

socket_close($client_connection);
socket_close($socket);