3

I created a socket server in C (using nanomsg) which shall communicate with a Python script (using standard 'Socket' implementation) via TCP:

C-Code (without error handling):

#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
...
char buf[23];
...
socket = nn_socket(AF_SP, NN_PAIR);
nn_bind(socket, "tcp://127.0.0.1:xxxxx");
...
nn_recv(socket, buf, sizeof(buf), 0); 
...
nn_shutdown(socket, endpoint_id);

Python-Code:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect(("127.0.0.1", xxxxx))
s.send('Hello C this is Python')
s.close()

There is no error in Python when connecting to the socket (if the C app is running). However, the C script is idling in method nn_recv and doesn't get any data at all. What am I doing wrong?

First I start the C code in a shell (it idles in method nn_recv). Then I start Python in another shell and expect the C application to receive the data. Both scripts execute without error.

linksfate
  • 101
  • 1
  • 8
  • Are you sure the type of sockets match? Shouldn't both be SOCK_STREAM or NN_PAIR. In ZeroMq which is not the same, a socket just waits to connnect until a suitable socket becomes available. – hetepeperfan Feb 11 '15 at 14:01
  • Looking into library's docs, it seems that `nn_socker` must have `NN_PULL` flag for this particular situation. – Matt Feb 11 '15 at 14:01
  • In `"tcp://127.0.0.1:xxxxx"`, is `xxxxx` recognized as a valid port? Should it be something like 10000? – ryyker Feb 11 '15 at 15:07
  • Have you seen this ***[nanomsg blog example](http://tim.dysinger.net/posts/2013-09-16-getting-started-with-nanomsg.html)***? – ryyker Feb 11 '15 at 15:10
  • xxxxx is replaced by a valid port in the actual code. – linksfate Feb 11 '15 at 15:17
  • does it work? I am also looking for a similar case – mojovski Jul 23 '15 at 09:12
  • I was able to establish the connection by using [this](https://github.com/tonysimpson/nanomsg-python) Python wrapper for the nanomsg library. It didn't work for me using the standard sockets in Python. (However, standard sockets in C work well with standard sockets in Python) – linksfate Jul 24 '15 at 10:27

1 Answers1

5

The issue is that nanomsg socket type is not a plain, standard, TCP type. The protocols do not match. You cannot send TCP message to a nanomsg socket and expect that nn_recv will work since the message will not conform to the defined nanomsg SP protocol requirements.

See nanomsg SP protocol header:

0                1               2                3
0 1 2 3 4 5 6 7  8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3  4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      0x00     |      0x53     |      0x50     |    version    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|             type              |           reserved            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

First four bytes of the protocol header are used to make sure that the peer's protocol is compatible with the protocol used by the local endpoint.

If the protocol header received from the peer differs, the TCP connection MUST be closed immediately.

That means that any raw TCP send to the nanomsg socket will kill the connection since it does not confirm to the SP protocol.

For more info consult the sp-tcp-mapping-01.txt document here

user3666197
  • 1
  • 6
  • 50
  • 92
sg7
  • 6,108
  • 2
  • 32
  • 40