recieving multiple messages with a php server
I have the following php server coded below:
<?php
function giveMeAnArray($delimiter, $string) {
return explode($delimiter, $string);
}
/* Copy this script to the two boxes you want to use. Uncomment the line saying "read" on the
* server box; and uncomment the line saying "write" on the client box.
*/
$read_write = "read";
//$read_write = "write";
//This is the server half
if ($read_write == "read") {
//Initialize the socket
set_time_limit(0);
$address = '127.0.0.1';
$port = 10119;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $address, $port) or die ('Could not bind to address');
//Start listening on the socket
While(1)
{//There could be a 'while true' or something here to make it infinite
socket_listen($sock);
//Accept an incoming connection
$client = socket_accept($sock);
//Read whatever was just sent, 1024 bytes' worth. Make this however long you need.
$global_string = socket_read($client, 1024);
print ("Read a string!\n");
//Strip whitespace
$global_string = ereg_replace("[ \t\n\r]", "", $global_string).chr(0);
$delim = ',';
$array = giveMeAnArray($delim, $global_string);
print ("Global string before being parsed: $global_string\n");
print ("Global string after being parsed:");
for($i=0; $i < count($array); $i++)
{
print("Coil ID: $array[0]\n");
print("Coil Width: $array[1]\n");
print("Footage Count: $array[1]\n");
print("Coil Length: $array[3]\n");
}
}//Close the while loop here, if you had one
//Close the connection; and the socket itself. Don't leave things like this open...!!
socket_close($client);
socket_close($sock);
print ("Closed socket successfully\n");
print ("Server exiting now...\n");
} //ends if ("read")
I was under the inpression that the while loop that I have would emable my program to recieve new messages every second but I keep getting a not responding application when run. I have a client that is passing four values to the server which are updated every second. Without the while loop I am able to recieve the first set of valuesbut nothing else. Does anyone know some syntax on how to setup this server to recieve messages that are set on a timer to be sent every second?
|