Arif
Arif

Reputation: 1399

Send data over TCP/IP with Netcat or Rails

I have an IP of the server and a port on which I'm able to connect via nc on Ubuntu 14.04.

> nc x.x.x.x PORT

In order to communicate with the server, the first step is to send a WAKEUP call and get acknowledgment. The server expects a 3 byte ID in the wakeup call. An example is provided in the documentation that shows the success scenario of sending the ID and receiving the ack using a software. i.e

The client sends:

<sy><sy><eq>111<et>

And the server responds with:

<sy><ak>A<et><cr>

Here is some detail of <sy>

Within <> brackets is a non-printable ASCII character (<sy> = ASCII 22 or Hex 0x16)

I tried to replicate the exact same scenario but failed to do so. The server doesn't respond to the data I send, although the data is received there. I'm not sure about these tags <sy><sy><eq> etc. How to send the ID(111) along with these tags <sy> correctly?

Also tried to send this data using Rails framework and Bindata ruby gem but don't know how to represent the above format.

Upvotes: 0

Views: 1312

Answers (1)

Gil Hamilton
Gil Hamilton

Reputation: 12357

netcat is probably the wrong tool for this. Or at least you will want to use some other program to feed it input.

If I were doing this, I would code up something in python or C that would both connect to the server and feed it whatever data I needed to send it (and receive/interpret the responses) leaving out nc altogether. There are many examples on the web.

You can encode the control characters in a byte string in python with the syntax b'\x16' for your <sy> character. Most other languages have an equivalent capability.

I can't be sure exactly what those characters are. It seems likely they are standard ASCII control characters, but they aren't using the standard abbreviations (see http://www.theasciicode.com.ar/ for example). So presumably the documentation you are looking at has a list of the corresponding values. Assuming for the sake of example that <eq> corresponds to the ASCII ENQ character and <et> to the ASCII EOT (and given you already know that <sy> is equivalent to ASCII SYN), your desired string <sy><sy><eq>111<et> can be encoded in a python byte string: b'\x16\x16\x05111\x04'

(or equivalently b'\x16\x16\x05\x31\x31\x31\x04' if you like regularity: the 1 characters are simply ASCII digits, so you can replace each 1 with its binary equivalent b'\x31')

To return to nc, trying to type in the control characters to the nc input from a terminal window is, while possible in most cases, very difficult and error-prone. You will need to know the equivalent control character mapping (for example, 0x16 is "Ctrl-V") and will need to know how to get the terminal to accept that literal character (coincidentally, in linux, you have to precede most control characters with a Ctrl-V in order to enter them as input and avoid having them interpreted in the usual way: Ctrl-D == EOF, Ctrl-C == Interrupt, Ctrl-W == Delete-Previous-Word, etc).

So if you wanted to enter the data above into nc's input from the command line, you would need to type these characters:

Ctrl-V Ctrl-V              <sy> / SYN
Ctrl-V Ctrl-V              <sy> / SYN
Ctrl-V Ctrl-E              <eq> / ENQ
1
1
1
Ctrl-V Ctrl-D              <et> / EOT

But also important to note is that ordinarily nc will not actually send anything until you enter a newline (i.e. press the Return key). Then that newline character will also get sent to the server which might not be what you want.

Upvotes: 1

Related Questions