Answer the question
In order to leave comments, you need to log in
How to send data via socket from another process?
The task costs such:
1. I am connected to the server from application.
2. It is necessary from the server to send data to the device via an open socket by pressing a button in the admin panel.
Perhaps the wrong approach.
There is a simple socket server:
set_time_limit(0);
$NULL = NULL;
$address = "127.0.0.1";
$port = 4545;
$max_clients = 10;
$client_sockets = array();
$master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$res = true;
$res &= @socket_bind($master, $address, $port);
$res &= @socket_listen($master);
if(!$res)
{
die ("Cannot bind and listen on $address: $port\n");
}
$abortion = false;
$read = array($master);
while(!$abort)
{
$num_changed = socket_select($read, $NULL, $NULL, 0, 10);
/* Did anything change? */
if ($num_changed)
{
/* Did the master socket (new connection) change */
if(in_array($master, $read))
{
if(count($client_sockets) < $max_clients)
{
$client_sockets[]= socket_accept ($master);
echo "Connection accepted (" . count($client_sockets) . " of $max clients)\n";
}
}
/* Loop through all clients, checking for changes in each one */
foreach($client_sockets as $key => $client)
{
/* New data in client socket? Read and answer */
if(in_array($client, $read))
{
$input = socket_read($client, 1024);
if($input === false)
{
socket_shutdown($client);
unset($client_sockets[$key]);
}
else
{
$input = trim($input);
if ([email protected]_write($client, "You said: $input\n") )
{
socket_close($client);
unset ( $client_sockets[$key] ) ;
}
}
if($input == 'exit')
{
socket_shutdown($master);
$abortion = true;
}
}// END IF in_array
} // END FOREACH
} // END IF ($num_changed)
$read = $client_sockets;
$read[] = $master;
} // END WHILE
Can I send data from another script to an open socket?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question