Socket client can do one thing (loop), but not the other (loop, timeout, loop again)

From: Date: Fri, 14 May 2004 17:06:04 +0000
Subject: Socket client can do one thing (loop), but not the other (loop, timeout, loop again)
Groups: php.general 
Request: Send a blank email to php-general+get-186241@lists.php.net to get a copy of this message
Hello everyone, I really appreciate the suggestions so far with my socket client problem. Somebody suggested I repost the issue with the program flow and code, to clarify the situation (a good idea). So here it is: To sumarrize: My socket client runs fine when all it does is (1) wait for data from the server (2) respond to server message (3) then wait again (ad infinitum). E.g., ---THIS WORKS---------------------- Setup and open TCP socket Send a Password_DS to the Server Send a Resend_DS to the Server Enter infinite loop: Wait for messages from the Server Upon receipt of a Send_DS from the Server Respond with a Send_DS_Ack Upon receipt of a Ping_DS from the Server Respond with a Ping_DS_Ack Upon receipt of a Status_DS from the Server Respond with a Status_DS_Ack ---THIS WORKS---------------------- Again, the client loops fine and works well—UNTIL I I try to add one feature: If the Server doesn't send a message for more than five seconds, the socket_read should send Ping_DS to the Server to see if it's still alive, and then, if it is, continue to the top of the loop and wait for another message from the Server. ---THIS DOESN'T WORK---------------------- Setup and open TCP socket Send a Password_DS to the Server Send a Resend_DS to the Server Enter infinite loop: Wait for messages from the Server Upon receipt of a Send_DS from the Server Respond with a Send_DS_Ack Upon receipt of a Ping_DS from the Server Respond with a Ping_DS_Ack Upon receipt of a Status_DS from the Server Respond with a Status_DS_Ack -> Periodically, send a Ping_DS to the Server -> If the Ping_DS transmit fails -> Clean-up and restart the socket connection -> If a Ping_DS_Ack is not received from the Server -> Clean-up and restart the socket connection ---THIS DOESN'T WORK---------------------- ---PROGRAM OUTPUT---------------------- Socket created. Socket connected to 192.168.0.13:9500. SERVER <- CLIENT: 18 (PASSWORD_DS) SERVER -> CLIENT: 19 (PASSWORD_DS_ACK) SERVER <- CLIENT: 3 (RESEND_DS) MESSAGE RECEIVED SERVER -> CLIENT: 23 (STATUS_DS) SERVER <- CLIENT: 24 (STATUS_DS_ACK) MESSAGE RECEIVED SERVER -> CLIENT: 16 (RESEND_DS_ACK) PHP Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 65 Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 65 PHP Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 65 Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 65 ---PROGRAM OUTPUT---------------------- ---MY CODE (WHICH DOESN'T WORK)---------------------- $msg_recv = 3; $timeout = array('sec' => 5, 'usec' => 0); // ENTER LOOP do { $data = ""; socket_set_block($socket); socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout); while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) { $data .= $buf; if(preg_match("/ENX/",$data)) { $msg_recv = 1; break; } elseif ($error = socket_last_error($socket)) { $msg_recv = 0; echo "\nTIMEOUT: SOCKET ERROR [$error]:" . socket_strerror($error); break; } } if ($msg_recv == 1) { echo "\n\n\nMESSAGE RECEIVED\n"; // RECEIVED MESSAGE $msg_type = get_msg_type($data); recv_msg($msg_type); // RESPONSE switch ($msg_type) { case 1: // SEND_DS -- PROCESS MESSAGE, RESPOND $msg_body = get_msg_body($data); send_msg("",$cn["SEND_DS_ACK"]); break; case 13: // PING_DS -- RESPOND send_msg("",$cn["PING_DS_ACK"]); break; case 23: // STATUS_DS -- UP AND RUNNING -- RESPOND send_msg("",$cn["STATUS_DS_ACK"]); break; case 16: // RESEND_DS_ACK -- DO NOTHING break; case 25: // DISCONNECT_DS -- ACKNOWLEDGE, CLOSE SOCKET, REOPEN socket_close($socket); $connection = false; break; } $msg_recv = 3; } elseif ($msg_recv == 0) { echo "\n\n\nTIMEOUT: MESSAGE NOT RECEIVED\n"; $msg_recv = 3; } } while ($connection == true); ---MY CODE WHICH DOESN'T WORK----------------------

« previous php.general (#186241) next »