yanjchan Posted April 25, 2010 Share Posted April 25, 2010 Hi! I wrote this as an attempt to create a simple client-server socket chat. Unfortunately, I keep getting errors about broken pipes. Any help would be much appreciated. #!/usr/local/bin/php -q <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); $address = '192.168.1.138'; $port = 31415; if (($sktMain = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "Failed to create socket.\n"; echo socket_strerror(socket_last_error()) . "\n"; } socket_set_option($sktMain, SOL_SOCKET,SO_REUSEADDR, 1); if (socket_bind($sktMain, $address, $port) === false) { echo "Failed to bind to port $port.\n"; echo socket_strerror(socket_last_error()) . "\n"; } if (socket_listen($sktMain) === false) { echo "Failed to start listening on socket.\n"; echo socket_strerror(socket_last_error()) . "\n"; } $clients = array(array('sock' => $sktMain)); while (true) { print_r($clients); foreach ($clients as $client) $read[] = $client['sock']; $ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL); for ($i = 0; $i < count($clients); $i++) { if (in_array($clients[$i]['sock'], $read)) { echo "Was in array\n"; $socket = $clients[$i]['sock']; if ($socket == $sktMain) { if (($client = socket_accept($sktMain)) < 0) { echo "Failed to accept socket.\n"; echo socket_strerror(socket_last_error()) . "\n"; continue; } else { array_push($clients, array('sock' => $client)); } } else { $bytes = socket_recv($socket, $buffer, 2048, 0); if ($bytes == null) { unset($clients[$i]); socket_close($socket); } else { foreach ($clients as $client) { if ($client != $sktMain) socket_write($client['sock'], $buffer, strlen($buffer)); } } } } } } ?> Link to comment https://forums.phpfreaks.com/topic/199642-php-socket-server-broken-pipe-error/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.