Page 1 of 1

Socket problem

Posted: Sat Oct 31, 2009 3:56 am
by sboons
Hello,

Earlier I posted about my problem with my socket script. It took up to 100% CPU usage.
Now I found the problem. With another found simple PHP socket script, I saw it has the same problem.

The code of this basic script is as follows:

I placed echo's to see what is happening.
When I execute the script, and connect using for example Windows Telnet to the socket, it connects without a problem. When I just CLOSE the telnetbox while connected, I see a loop of the POS7 message.

Then the CPU usage is exeeding, I think this is my problem.
Does anybody know how to fix this, that when someone just connects and closes the session, there is no loop?

Code: Select all

 
<?
 
set_time_limit(0);
ob_implicit_flush();
$address = '0.0.0.0';
$port = 5010;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
        echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
        echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
echo "pos1\n";
do {
echo "pos2\n";
        if (($msgsock = socket_accept($sock)) === false) {
                echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
                break;
echo "pos3\n";
        }do {
                $out = socket_read($msgsock, 2048);
                if (!empty($out)) {
                        if ($out == 'quit') {
                                break;
                        }elseif ($out == 'shutdown') {
                                socket_write($msgsock, 'plc down', 8);
                                socket_close($msgsock);
                                break 2;
                        }else {
                                switch ($out) {
                                        case "A": $response = "A!";
                                        break;
                                        case "B": $response = "B!";
                                        break;
                                        default: $response = "C!";
echo "pos4\n";
                                }
                                socket_write($msgsock, $response, strlen($response));
                                break;
echo "pos5\n";
                        }
echo "pos6\n";
                }
echo "pos7\n";
        } while (true);
echo "pos8\n";
        socket_close($msgsock);
} while (true);
echo "pos9\n";
socket_close($sock);
 
?>

Re: Socket problem

Posted: Sat Oct 31, 2009 4:22 am
by Weirdan
PHP Manual wrote: socket_read() returns the data as a string on success, or FALSE on error (including if the remote host has closed the connection).
Note: socket_read() returns a zero length string ("") when there is no more data to read.
This means you want to check the result of socket_read for emptiness and break to the outer loop if $out is empty.

Re: Socket problem

Posted: Sat Oct 31, 2009 4:25 am
by sboons
I guess you are right! Can you tell me how to do this?
I have been searching on manuals / examples but can't get it to work.
All examples I have seen have this problem.

Re: Socket problem

Posted: Sat Oct 31, 2009 6:03 am
by Weirdan

Code: Select all

 
     $out = socket_read($msgsock, 2048);
     if (!empty($out)) {
           // .......
     } else {
          break;
     }
 

Re: Socket problem

Posted: Sat Oct 31, 2009 1:09 pm
by sboons
Yes!
That is exactly the solution to my problem.
Many thanks!

Steef