Using fsockopen to read public ip & port

I have a weighing scale connected to a client’s pc with a serial port. The client’s PC is installed with SERPROXY. Using my public IP, I can 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.

How do I about to read and echo it on the web page ?

The following piece of code fails saying CONNECTION REFUSED?

$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);

Run code snippet

Expand snippet

I will share my real public ip for anyone interested to telnet and see the data.

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.

Also confirmed with bluehost that the port 25003 is open on my bluehost server account.

What OS is the client PC running? Does it have a firewall that perhaps needs to be configured to accept connections from your server? I don’t think there’s a need to have 25003 at the server, only at the client end and with port forwarding etc. You can PM me the external IP if you want, I’ll have a look from here.

Client PC is running Windows 7. It has firewall disabled for testing purpose. Please note the public IP

It doesn’t let me send you personal message, let me know once you read this off.

Edit: Public IP removed from post and sent in PM to @droopsnoot - ScallioXTX

You have a config error somewhere. I know that’s not much help here, but I can access the scales perfectly well from over here in the UK, either on a telnet client (from a guest OS inside a VMware player on a desktop PC), or from a small PHP script on that same desktop PC running WAMP.

There are one or two issues with the code, however. This is the code I got working here:

<?php

$count = 0;
$weight = "";

echo "Starting";

$fp = fsockopen("tcp://00.00.00.00", 25003, $errno, $errstr);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
   echo "Cannot open socket";
} else {
   $inwt = false;
   while (!feof($fp) && ($count < 10)) {
      $inchar = fgets($fp, 2); // get a single character from the socket
      if ($inwt) { // are we "inside" a weight string?
         $weight .= $inchar;
         if ($inchar == "]") { // received closing bracket
            echo "Weight " . $count . " value " . $weight;
            $weight = "";
            $inwt = false;
            $count++;
            }
         }
      if ($inchar == "[") { 
         $weight = $inchar;
         $inwt = true;
         }
      }
   }
fclose($fp);
?>

A few notes:

  • Your original posted code has no means of ever ending, so it will just sit in a loop for ever, or until the server times out. I added the $count so I could just get a few readings, display them, then exit.
  • If you look at the doc for fgets(), it is waiting for a newline character, or length-1 characters. Your scales don’t seem to send a newline (when I connected, they send a five-digit number surrounded by square-brackets, like [00030]), so fgets() terminated after 64 characters, which takes a time to build up. I coded a loop to loop for the open and close brackets and build the string up when it finds them, so with this it just wants a single characters at a time.

I don’t know what format the weight comes out of your scales in, I presume the five-digit int is just to show something is happening. You will obviously need to change the code to deal with that formatting difference, and it might be that it sends a fixed-length reading.

@droopsnoot Thank you very much, it pointed out the fact that the problem lies either in bluehost shared server or client windows PC or SERPROXY. I am afraid there is any major config change possible other than baud rate, com port etc in the SERPROXY which is set correctly.

Settings from Bluehost:
(1) From bluehost, socket services are enabled as per their representative.
(2) From bluehost, port 25003 is open as per their representative. I had told them to open it.
(3) When I fsockopen google.com port 80, it works it means, should I assume that the configuration is okay in server?

Settings from Client PC:
(1) Tried TinkerProxy, still same.
(2) Enabled IIS, RAS CONNECTION, RIP LISTENER, SIMPLE TCP IP SERVICES from WINDOWS FEATURES under control panel to see if that was the cause but again to no luck.
(3) Restored the PC to 15 days back and tried again but to no luck.
(4) Planning to change the client PC itself if thats the problem, I will update the feedback.
(5) Disabled firewall from intranet to see if that was the cause but to no luck again and yes…enabled too.

Meanwhile, I am still confused whether the config problem lies in SERPROXY, WINDOWS CLIENT or BLUEHOST SHARED SERVER ?

Is there any other alternative to SERPROXY you are aware about ?

With regards to the scale output format, Ill get back with the supplier and am assured it will be resolved.

Thank you again !

If the client PC is hosting the scales, and the SERPROXY, then the fact that I can get in from here suggests that it is not the client PC or SERPROXY.

I don’t think you need to have 25003 open on your server, only on the router (port forward) and the client PC. (ETA I’ve read the Bluehost thing, they have to open the outgoing port on their firewall, so that’s OK). You do have a fixed IP on your server hosting account as well as on the client PC, don’t you? If they’ve opened the port, presumably you do.

I have never heard of SERPROXY, I did a quick search around the web when you first mentioned it and all I find is either Android / Raspberry Pi stuff or a very old project that doesn’t look like it’s been updated for 10 years or so. But the fact I can get in, suggests it is not that end. I used to use a network serial port program, but that was to share from a Windows terminal server to a client PC, so it had software at both ends. I don’t know if it would support a connection like this, even if I could remember what it was called.

When you test your code, are you testing it from the same LAN as the client PC, or elsewhere? I just wondered if something is routing your request so it bypasses the router (i.e. trying to be too clever) so the port forward doesn’t work. Wouldn’t be the case if your code is running on the Bluehost server, of course.

What about if you change the code to try to open a connection to a more “standard” port on another server, just to test? If you can connect to port 80 on something public from the same piece of code on the BH server, that would suggest that they haven’t opened the firewall, as 80 is open by default.

@droopsnoot

(1) Just cross verified with BH, incoming and outgoing port 25003 is open.
(2) You got it correct, yes, I have fixed ip on BH account and on the client PC too.
(3) Yes, I test the connection from same lan & from same client PC serial port is connected to.
(4) And also tried testing it from outside lan, i,e from phone but to no avail. (of course, having wifi turned off)

EDIT: Firewall is already disabled in Client PC and also disabled in router but to no avail.

Now I think I am more confused, will try a complete different PC and update you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.