Socket PHP read error

I have three buttons in my html page related to a php script and they are sending a command to another application using a socket connection.

1) when the application receiving the command and the xampp server is running on localhost works fine, but when I try to send the command on the network it sometimes works and sometimes it doesn't.

2), which may be the reason for this.

code for button1

<?php
// Fill up array with names
$q=$_GET["q"];



$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = $_GET["vz"];
$port=6100;
$buffer=$q ."\0";
$len= strlen($buffer);

socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);


$buffer='COMMAND HERE';
$len= strlen($buffer);


socket_sendto($sock, $buffer, $len, 0 , $host, $port);

echo( socket_read($sock, 65535) );

socket_close($sock);


?>

      

code for button 2

?php


$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = $_GET["x"];
$port=6100;
$buffer='COMMAND HERE';

$len= strlen($buffer);

socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);



$len= strlen($buffer);


socket_sendto($sock, $buffer, $len, 0 , $host, $port);

echo( socket_read($sock, 65535) );

socket_close($sock);


?>

      

code for button 3

<?php


$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = $_GET["x"];
$port=6100;
$buffer='COMMAND HERE';

$len= strlen($buffer);

socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);



$len= strlen($buffer);


socket_sendto($sock, $buffer, $len, 0 , $host, $port);

echo( socket_read($sock, 65535) );

socket_close($sock);


?>

      

+3


source to share


2 answers


Check for errors in this code, maybe timeout?



if ($socket === false) {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg");
}

      

0


source


The following code worked fine with chrome and the latest version of Internet Explorer



0


source







All Articles