PHP Socket Connection – Through XR300 Router to Receipt Printer

portport-forwardingroutersocketwindows-subsystem-for-linux

I have a PHP application which is attempting to open a socket connection to a printer through my router.

A couple things to note – my networking skills are lacking so I may be missing something obvious or this may not even be possible in the way I want it to be. That being said, I have done a bunch of searching on the issue and can't seem to find anyone with this exact issue (just lots of people trying to SSH remotely). I also know that the device I am trying to connect to is working as intended because I have a small nodejs application which successfully opens a connection to it on the local network. And finally I am using Docker to run the PHP application locally, which may be relevant to my problem (though I know the connection is reaching the router due to the logs).

The code is simple:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_TCP, SO_DEBUG, 1);

$server_connect = socket_connect($socket, "router_public_ip", 9100);

Within my router I setup that port to be forwarded:
enter image description here

When I attempt to connect PHP throws socket_connect(): unable to connect [111]: Connection refused and the router logs show that a connection failed.

I noticed within the router logs that the start port is random and tried changing my port forwarding configuration to:
enter image description here

This gives me the same result and my router does not give me much info to go on (as to why the connection failed, was it blocked?). This is all that is shown in the logs:

enter image description here

Edit:
The printer config utility menu does not seem to mention anything about a firewall or remote connections.
enter image description here

Best Answer

Then I wonder about the 0.0.0.0 gateway in the printer's network config.

It would mean that your printer can connect to any IP address directly. Under normal circumstances, a system will only be able to communicate directly with other systems in its subnet (see the mask). For wider reach it'll need to go through a gateway.

Related Topic