Hi Guys,
I am trying to implement a PHP socket server,
after reading many tutorials etc i have managed to create the server and read the buffer data, however i cant get it to read more than X bytes, this is a limit for tcp in a single buffer so i will somehow need to piece all the data back together in a single packet.
let me try explain what i need to do:
- Set up a listining server on a specified port
- deamonise the process
- fork the server so i can handle multiple connections
- when the data comes to the server i need to buffer it until i recieve a certain char “##”
- once i have the entire string i need to pass it to a different script for processing
I don’t need to ever pass anything back to the client
Any help or pointer with this will be greatly accepted
Thanks
K-
What mechanism are you using for reading and maintaining the buffer? Do you have some code for us to help you with?
so far i have this hacked from a Flash chat script, but its getting all the buffer messages mixed up
<?php
//---- Start Socket creation for PHP 5 Socket Server -------------------------------------
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed, reason: " . socket_strerror($master) . "\
";
}
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0) {
echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\
";
}
if (($ret = socket_listen($master, SOMAXCONN)) < 0) {
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\
";
}
$read_sockets = array($master);
//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true) {
$changed_sockets = $read_sockets;
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\
";
continue;
}else{
array_push($read_sockets, $client);
print "[".date('Y-m-d H:i:s')."] ".$client." CONNECTED "."(".count($read_sockets)."/".SOMAXCONN.")\\r\
";
}
}else{
$bytes = @socket_recv($socket, $buffer, 4048, 0);
if ($bytes == 0) {
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
}else{
//This is looping looking for "##\\r\
"
$asc = bin2hex(substr($buffer,-8));
if ($asc == '23230d0a'){
$read.=$buffer;
break;
}else{
$read.=$buffer;
}
print $read;
}
}
}
}
?>
K-