问题
I have a weighing scale connected to a client pc with a serial port. The client PC is installed with SERPROXY. Using telnet xx.xx.xx.xx port 25003, the data is shown successfully. It means SERPROXY is working fine and port forwarding is done properly in router too. I will share the real public ip upon request to see the data using telnet.
How do I about to read and echo it on the web page ?
The following piece of code fails on binding using sockets?
error_reporting(E_ALL);
// Server IP address
$address = "xx.xx.xx.xx"; // here goes the real public ip address
// Port to listen
$port = 25003;
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock,$address, $port) or die('Could not bind to address');
socket_listen($mysock, 5);
$client = socket_accept($mysock);
// read 1024 bytes from client
$input = socket_read($client, 1024);
echo $input ;
socket_close($client);
socket_close($mysock);
Its my first time attempt in socket. Does it have to be compulsorily two way communication, ie, for example, server.php and client.php ? Otherwise the sockets don't work.
Is there any other way we can read the output of the said ip address & port in php web page ?
Spent several hours but to no luck. Help would be greatly appreciated.
NOTE: server is hosted on bluehost. socket_extensions are enabled, verified.
EDIT: Also confirmed with bluehost that the port 25003 is open from their side.
EDIT: Tried following way too, but to no avail.
$fp = fsockopen("tcp://xx.xx.xx.xx", 25003, $errno, $errstr);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
while (!feof($fp)) {
$weight = trim(fgets($fp, 64)," ");
}
}
echo $weight;
fclose($fp);
Where I could be going wrong ? Am I totally on the wrong track ?
来源:https://stackoverflow.com/questions/52531586/unable-to-read-data-from-socket-can-not-bind-to-address-in-php