php - Why am I getting this error with socket_read()? -
i have following code: http://pastebin.com/u9qysmet
when try telnet server , port with
telnet localhost 6667 i following error:
php warning: socket_read(): unable read socket [107]: transport endpoint not connected in /var/www/php-irc/proxy.php on line 91
anyone have ideas why i'm getting error?
code:
<?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 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 @ 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 client %d%s', $i, $i, "\n"); } $read[$i] = $clients[$i]['socket']; } } if($debug) { var_dump($read); } // changed sockets? // $write , $except 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 client %d %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 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 client %i%s", $data, $i, "\n"); if($data === null) { // disconnected unset($clients[$i]); } $data = trim($data); if($data == 'exit') { printf('received exit command client%s', "\n"); socket_close($clients[$i]['socket']); } elseif($data) { // strip whitespace printf('received data: %s 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);
you haven't connected yet. need:
socket_connect($socket, $address, $service_port); in if statement see if can connect.
Comments
Post a Comment