Socket problem


Posted on 16th Feb 2014 07:03 pm by admin

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?

Line number On/Off | Expand/Contract <? 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 "pos1n";do {echo "pos2n"; if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "n"; break;echo "pos3n"; }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 "pos4n"; } socket_write($msgsock, $response, strlen($response)); break;echo "pos5n"; }echo "pos6n"; }echo "pos7n"; } while (true);echo "pos8n"; socket_close($msgsock);} while (true);echo "pos9n";socket_close($sock); ?>

No comments posted yet

Your Answer:

Login to answer
65 Like 31 Dislike
Previous forums Next forums
Other forums

Do not allow posting of whitespace
Currently the script below works if the user does not type a name/message, but if i create a whitesp

Saas with SAP R/3
hi experts,

Is SaaS offer available in SAP? for SAP R/3? from where i can get the info a

help retrieiving results and doing pagination
Having some trouble trying to get the results to show on more than just one page.

What is ha

Inter Company -- Resources Related Billing (Services Industry)
Hello,

My company has a scenario where:
Company A bills Company C for INTER (cross

Dynamic Data + Sql Server 2005 Enterprise?
Hi! I have just started to learn ASP.NET, and it looks like it is quite a lot to learn. Im not reall

Logging and nologging bulk insert
Hi,

oracle version: 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

Progress bar tracking file downloads
Hey guys, was wondering if there was a way to track how far a file has been downloaded. I know there

socket makes browser hang...
I have a socket server, and I am having a problem at the moment...

A browser sends a http hea

Login script (probably a simple error)
dbConfig.php
Code: <?
// Replace the variable values below
// with your specific dat

Multi Level Array Problem
hi all,

For example I have array like below:

$temp = array(array('north america', 'us'

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash