/*
connect to server as socket client
*/
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
/*$service_port = getservbyname('www', 'tcp'); */
$service_port = 8888;
/* Get the IP address for the target host. */
/* $address = gethostbyname('www.example.com'); */
$address = gethostbyname('192.168.77.95');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$msgObj = new stdClass(); // create new object
$msgObj->device = "4relay";
$msgObj->unit = "1";
$msgObj->set = "1";
$msgObj->level = "0.0";
$tx = json_encode($msgObj);
$rx = '';
echo "Sending message...";
echo $tx;
echo "\n";
socket_write($socket, $tx, strlen($tx));
echo "OK.\n";
echo "Reading response: ";
while ($rx = socket_read($socket, 2048)) {
echo $rx;
echo "\n";
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>