2

I have a (zebra) printer that I can telnet to:

jasongoodwin$ telnet 192.168.101.051 9100
Trying 192.168.101.51...
Connected to 192.168.101.051.
Escape character is '^]'.

Then I can send it some data from the console and it will print a label for me.

I can also connect and print from scala no problem:

def printZpl(zpl: String, ip: String, port: Int): Unit = {
  val clientSocket = new Socket(ip, port)
  val outToServer = new DataOutputStream(clientSocket.getOutputStream())
  outToServer.writeBytes(zpl)
  clientSocket.close()
}

But I can't seem to connect via elixir/erlang via gen_tcp:

opts = [:binary, active: false]
{:ok, socket} = :gen_tcp.connect('192.168.101.051', 9100, opts)

Iex just freezes and eventually times out. Works fine connecting to eg redis... I'm assuming there is some option or quality of this connection that causes it to fail from elixir/erlang?

I find the gen_tcp docs to be unhelpful - I tried a bunch of different params.

JasonG
  • 5,794
  • 4
  • 39
  • 67
  • You are using port 9100 with telnet and trying with port 6379 in Elixir. Are you sure you are using the correct port? – Justin Wood Dec 19 '17 at 00:49
  • Ya that's just a typo here, not an issue in the code. I'll fix it - thanks. – JasonG Dec 19 '17 at 01:10
  • maybe it's because i'm typing 051 instead of 51 – JasonG Dec 19 '17 at 01:11
  • no doesn't seem to be an issue. – JasonG Dec 19 '17 at 01:13
  • 1
    You have a mess in IP addresses. Please fix everything to be a correct one (first telnet command has `×.×.×.85`, others `×.×.×.51`.) – Aleksei Matiushkin Dec 19 '17 at 06:42
  • Please post an _actual_, _unaltered_ transcript of what happens. Since we can't see the real transcript it's hard to guess what may be going on. – Onorio Catenacci Dec 19 '17 at 14:08
  • Ya sorry - I figured out the issue. The preceeding 0 in the IP address breaks the connect request. – JasonG Dec 19 '17 at 15:01
  • You may want to take a look at this [Q & A](https://superuser.com/questions/857603/are-ip-addresses-with-and-without-leading-zeroes-the-same) for more reference on this question – Onorio Catenacci Dec 20 '17 at 22:26
  • What point specifically? This seems like it shouldn't be an issue: "Whether you type it as 192.168.0.1 or 192.168.000.001 they are both equal to 11000000.10101000.00000000.00000001" – JasonG Dec 21 '17 at 01:47

1 Answers1

2

The issue is that the IP address had a preceeding 0

{:ok, socket} = :gen_tcp.connect('192.168.101.051', 9100, opts)

If I connect like so this works.

iex(1)> opts = [:binary, active: false]
[:binary, {:active, false}]
iex(2)> {:ok, socket} = :gen_tcp.connect('192.168.101.51', 9100, opts)
{:ok, #Port<0.1243>}
JasonG
  • 5,794
  • 4
  • 39
  • 67