Reputation: 1293
Desire: I want to connect to the socket and have 'running' sent to the client every second.
This of course is a test.
Issue: The output 'running' is only sent when the client sends data. It seems like the loop is pausing waiting on the client to write back.
Code:
$socket = @socket_create_listen("12345");
while (true) {
$client = socket_accept($socket);
$msg = "\nHello"."\r\n".chr(0);
$length = strlen($msg);
socket_write($client, $msg,$length);
usleep(5);
while (true) {
$msg = 'running'."\r\n".chr(0);
$length = strlen($msg);
socket_write($client, $msg, $length);
sleep(1);
}
}
Example: The code above is in a file on my server. If I run this file and then use telnet to connect to my server on port 12345 telnet myip# 12345
I expected to see "running" printed in the console every second. Instead "running" is only wrote once. If I type something in the console(ie write data to the server) running is displayed again. Thus running only being sent to the console when I send something to the server.
What could cause this?
Thanks JT
Upvotes: 1
Views: 3222
Reputation: 1162
The server seems fine. The problem is most likely with your client. I tweaked your server and wrote a client that successfully tests against. My tweak was a check that the socket closed. Otherwise, you will get in to a continuous loop.
Here is socketserver.php
echo "Starting Server" . "\n\r";
$socket = @socket_create_listen("55000");
while (true) {
$client = socket_accept($socket);
$msg = "\nHello"."\r\n".chr(0);
$length = strlen($msg);
socket_write($client, $msg,$length);
usleep(5);
while (true) {
$msg = 'running'."\r\n".chr(0);
$length = strlen($msg);
$test = socket_write($client, $msg, $length);
sleep(1);
if ($test === false) {
echo "socket probably closed\n";
exit;
}
}
}
Here is socketclient.php
$address = '127.0.0.1';
$port = 55000;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit;
}
echo "\nStarting client";
echo "\nAttempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
echo "\n\nReading responses...\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "\nClosing socket...";
socket_close($socket);
Here is a screenshot of the result.
I realize that this is a test, but note that a periodic status server makes far more sense designed using UDP.
Upvotes: 2
Reputation: 19791
Have you tried using any other client? nc (netcat) is great for testing things. Also, put some output in your server to have it log (to screen or file) when it sends data.
I'm guessing your server is working fine but your client is messed up in some way. Just type nc <my_ip> 12345
and see what you get.
Remember, telnet is an actual protocol (https://www.rfc-editor.org/rfc/rfc854), whereas nc is just straight TCP.
Also, NUL terminating the string may be causing weird issues. Lose the chr(0) bit and see if that makes it better.
Upvotes: 1