Score:0

PHP socket connection through XR300 router to a receipt printer

cn flag

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

Gerard H. Pille avatar
in flag
Are you trying to connect to 192.168.1.150:9100? You should use the IP address of the router.
EvilZebra avatar
cn flag
@GerardH.Pille - no, I am trying to connect to the router's public IP address. I believe the router automatically directs me to that local address due to the port forward having an internal IP address set?
Gerard H. Pille avatar
in flag
OK. The start port being random (ephemere IIRC) is the way it works. Now, is the printer listening on 9100 and accepting external connections? The "start port" should be 9100 external and internal.
EvilZebra avatar
cn flag
The printer is listening (as I mention in my post I can successfully connect, and print, to the device using a local nodejs script) and I don't think it differentiates between internal/external connections.
Gerard H. Pille avatar
in flag
Can you make sure? Currently I don't see what else it could be. I'll check your PHP code.
EvilZebra avatar
cn flag
I am 100% certain. My original PHP code sends details to nodejs, nodejs connects to printer locally through socket and prints details. New PHP code is trying to connect to socket directly (and remotely). If I use old code - printer works, use new code - errors.
EvilZebra avatar
cn flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/133066/discussion-between-evilzebra-and-gerard-h-pille).
Gerard H. Pille avatar
in flag
That doesn't prove the printer accepts external connections. The PHP you show is OK, I can connect to my printer using the external address, but my printer hasn't got any "firewall" settings. The HP direct is active (listening on port 9100) or not.
EvilZebra avatar
cn flag
@GerardH.Pille - as per our discussion in chat I tested out your latest ideas and it turned out to be the Default Gateway! According to a Google search the device must have the default gateway set to be able to communicate remotely. So I set that to `192.168.1.1` and all is well. Feel free to throw up an answer to this question so I can accept it and get you your karma.
Score:1
in flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.