为什么我用socket_read()得到这个错误?

时间:2011-04-11 18:01:15

标签: php sockets

我有以下代码:http://pastebin.com/U9qYSmET

当我尝试使用

远程登录我的服务器和端口时
telnet localhost 6667

我收到以下错误:

  

PHP警告:socket_read():无法从套接字[107]读取:第91行的/var/www/php-irc/proxy.php中未连接传输端点

任何人都有任何想法我为什么会收到此错误?

代码:

<?php 

if(!defined('SOCKET_ADDRESS')) {
    define('SOCKET_ADDRESS', '127.0.0.1');
}

if(!defined('SOCKET_PORT')) {
    define('SOCKET_PORT', '6667');
}

if(!defined('MAX_CLIENTS')) {
    define('MAX_CLIENTS', '10');
}
set_time_limit(0);

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, SOCKET_ADDRESS, SOCKET_PORT) or die('Could not bind to address ' . SOCKET_ADDRESS . ' on port ' . SOCKET_PORT . "!\n");
socket_listen($socket, MAX_CLIENTS) or die ("Could not setup socket listener!\n");

// setup read socket array
$read = array();

// client array w/ default initial socket
$clients = array('0' => array('socket' => $socket));

// force debug at first run..
$debug = true;
$time = time();
printf('Time: %d%s', $time, "\n");
while(true) {

    if(time() - $time >= 10) {
        $time = time();
        printf('Time: %d%s', $time, "\n");
        $debug = true;
    }
    if($debug === true) {
        printf('Debug: %s%s', $debug, "\n");
    }
    // $read[0] = $socket;
    if($debug) {
        var_dump($read);
    }

    // Handle clients
    for($i = 0; $i < count($clients); $i++) {
        if(isset($clients[$i]['socket'])) {
            if($debug === true) {
                printf('Setting socket %d to client %d%s', $i, $i, "\n");
            }
            $read[$i] = $clients[$i]['socket'];
        }
    }
    if($debug) {
        var_dump($read);
    }
    // Any changed sockets?
    // $write and $except are only placeholders
    $changed_sockets = socket_select($read, $write = NULL, $except = NULL, 0);
    if($debug === true){
        printf('Changed sockets: %d%s', $changed_sockets, "\n");
    }
    // Handle new connections
    if(in_array($socket, $read)) {
        for($i = 0; $i < MAX_CLIENTS; $i++) {
            if(!isset($clients[$i])) {
                $clients[$i]['socket'] = socket_accept($socket);
                socket_getpeername($clients[$i]['socket'],$ip);
                $clients[$i]['ip'] = $ip;
                printf('Accepting connection into client %d from %s%s', $i, $ip, "\n");
                break;
            }
            // } elseif($i == MAX_CLIENTS - 1) {
                // echo 'Too many clients connected!', "\n";
            // }
            if($changed_sockets < 1) {
                continue;                
            }
        }
    }
    if($debug) {
        var_dump($clients);
    }

    for($i = 0; $i < count($clients); $i++) {
        $client = $clients[$i];
        // Has our client socket seen any changes?
        if(in_array($client['socket'], $read)) {
            printf('Client %d has changed! Reading...%s', $i, "\n");
            $data = socket_read($client['socket'], 1024);
            if($data === false) {
                $error = socket_strerror(socket_last_error());
                printf('An error occured...%s%s', $error, "\n");

            }
            printf("Read raw data %s from client %i%s", $data, $i, "\n");
            if($data === null) {
                // disconnected
                unset($clients[$i]);
            }

            $data = trim($data);
            if($data == 'exit') {
                printf('Received exit command from client%s', "\n");
                socket_close($clients[$i]['socket']);
            } elseif($data) {
                // Strip whitespace
                printf('Received data: %s from client %d%s', $data, $i, "\n");
                $output = sprintf('%s%s%s', $data, "\n", chr(0));
                socket_write($client['socket'], $output);
            }
        }
    }

    // reset debug
    $debug = false;
}

socket_close($socket);

2 个答案:

答案 0 :(得分:1)

您尚未连接任何内容。 你需要:

socket_connect($socket, $address, $service_port);

在if语句中查看是否可以连接。

答案 1 :(得分:0)

问题是它试图从$clients[0]读取。哪个不是有效的客户端。我只是更改了最后两个for循环,以1开始$i

到目前为止,在我的测试中,这似乎没有任何不利影响。错误消失了,我所有的客户端实例似乎都正常工作。