yanjchan Posted February 27, 2010 Share Posted February 27, 2010 I'm trying to make a PHP Socket Chat server and a C# Client, but I keep getting strange errors. This is modified from something that was meant to be used with flash. #!/usr/bin/php -q <?php /* Raymond Fain Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com For any questions or concerns, email me at [email protected] or simply visit the site, www.php.net, to see if you can find an answer. */ error_reporting(E_ALL); set_time_limit(0); // Ensure that the script stays running indefinitely. ob_implicit_flush(); // Turn implicit flushing off. $address = 'icebrg.us'; // The address we are serving from. $port = 43791; // The port we are servinf from. //---- Function to Send out Messages to Everyone Connected ---------------------------------------- function send_Message($allclient, $socket, $buf) { /* This function sends a message to all of the clients. * Parameters: * $allclient = an array of all of the client sockets * $socket = the socket that is sending the message * $buf = the message buffer, contains the message */ foreach($allclient as $client) { socket_write($client, "$buf"); } } function process_Message($allclient, $socket, $buf) { $msgtype = substr($buf, 0, 3); $nicklen = substr($buf, 3, stripos($buf, ";")); $nick = substr($buf, stripos($buf, ";")+1, $nicklen); $msg = substr($buf, strpos($buf, ";")+$nicklen+1); if ($msgtype == "xit") { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket); unset($sockets_ids[$index]); $sockid --; } else if ($msgtype == "whr") { send_Message($allclient, $socket, "!whr"); } else if ($msgtype == "ihr") { send_Message($allclient, $socket, $nick." is here!"); } else { send_message($allclient, $socket, $nick.": ".$msg); } } //---- Start Socket creation for PHP 5 Socket Server ------------------------------------- if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed, reason: " . socket_strerror($master) . "\n"; } socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n"; } if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n"; } $read_sockets = array($master); $sockets_ids = array(0); $sockid = 0; //---- Create Persistent Loop to continuously handle incoming socket messages --------------------- while (true) { $changed_sockets = $read_sockets; $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); foreach($changed_sockets as $socket) { if ($socket == $master) { if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; continue; } else { $sockid ++; array_push($read_sockets, $client); array_push($sockets_ids, $sockid); socket_write($client, "$sockid"); send_Message($read_sockets, $socket, "Someone has joined"); } } else { $bytes = socket_recv($socket, $buffer, 2048, 0); if ($bytes == 0) { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket); unset($sockets_ids[$index]); $sockid --; } else { $allclients = $read_sockets; array_shift($allclients); process_Message($allclients, $socket, $buffer); } } } } ?> The errors I get: Warning: socket_write(): unable to write to socket [32]: Broken pipe in /var/www/gibchat/socketShell.php on line 35 Warning: socket_recv(): unable to read from socket [104]: Connection reset by peer in /var/www/gibchat/socketShell.php on line 107 Thanks in advance for your help. Link to comment https://forums.phpfreaks.com/topic/193542-php-socket-chat-errors/ Share on other sites More sharing options...
yanjchan Posted February 27, 2010 Author Share Posted February 27, 2010 Or, does anyone know of a good tutorial that they would like to recommend? The only reputable one I've found is for PHP 4, and it doesn't work anyways. Link to comment https://forums.phpfreaks.com/topic/193542-php-socket-chat-errors/#findComment-1019094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.