Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-08

ZeroMQ Bindings

24 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, I need to use ZeroMQ for inter process communication, but the bindings at https://github.com/bvarga/delphizmq does not seems to work, at least on Linux (XUbuntu 16.04 64bits) and zmq 4.2.

The client is an C++ app and the server is the FPC/Lazarus one. Before trying with Lazarus I did this very simple Python program and it worked:

import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("ipc://localscp")

while True:
# Wait for next request from client
message = socket.recv()
print "Received request: ", message
socket.send("AA")

The port to FPC/Lazarus should be this:

     var
       context: pointer;
       responder: pointer;
       buf: array[0..9] of char;
     begin
       context := zmq_ctx_new;
       responder := zmq_socket(context, ZMQ_REP);
       zmq_bind(responder, 'ipc:///localscp');
       while true do
       begin
         zmq_recv(responder, buf, 10, 0); // here hangs
         writeln(buf);
       end;
     end;

Any hint?

Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Sandro Cumerlato
to Leonardo M. Ramé

Hello Leonardo,
please test attached "client.pas" source code.

Sandro Cumerlato

On 23 August 2016 at 02:56, Leonardo M. Ramé wrote: ...

Show quoted text 10 lines
The port to FPC/Lazarus should be this: var context: pointer; responder: pointer; buf: array[0..9] of char; begin context := zmq_ctx_new; responder := zmq_socket(context, ZMQ_REP); zmq_bind(responder, 'ipc:///localscp');

too many "/" here

while true do begin zmq_recv(responder, buf, 10, 0); // here hangs

you have to pass a pointer to buf[0] I guess

Show quoted text 6 lines
writeln(buf); end; end; Any hint?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: client.pas
Type: application/octet-stream
Size: 389 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/4fca5b74/attachment.obj>

Reply by email

Leonardo M. Ramé
to Sandro Cumerlato
Sandro Cumerlato wrote 7 lines
Hello Leonardo, please test attached "client.pas" source code. Sandro Cumerlato

Thanks Sandro, looks like the only change is this

zmq_recv(responder, @buf[0], 10, 0);

But when I change the code to that I get this error:

unit1.pas(52,42) Error: Can't assign values to an address

Regards,

Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Sandro Cumerlato
to Leonardo M. Ramé

Hello Leonardo,
try to change your binding zmq.pas file like this:

function zmq_recv (s: pointer; buf: pointer; len: size_t; flags:
Integer): Integer; cdecl; external libzmq;

it should be the correct translation of original code:

ZMQ_EXPORT int zmq_recv (void *s, void *buf, size_t len, int flags);

Sandro Cumerlato

On 23 August 2016 at 14:06, Leonardo M. Ramé wrote: ...

Show quoted text 7 lines
zmq_recv(responder, @buf[0], 10, 0); But when I change the code to that I get this error: unit1.pas(52,42) Error: Can't assign values to an address Regards,

Reply by email

Leonardo M. Ramé
to Sandro Cumerlato
Sandro Cumerlato wrote 12 lines
Hello Leonardo, try to change your binding zmq.pas file like this: function zmq_recv (s: pointer; buf: pointer; len: size_t; flags: Integer): Integer; cdecl; external libzmq; it should be the correct translation of original code: ZMQ_EXPORT int zmq_recv (void *s, void *buf, size_t len, int flags);

This time it compiles, but hands on the zmq_recv.

Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé
Leonardo M. Ramé wrote 17 lines
El 23/08/16 a las 09:22, Sandro Cumerlato escribió: > Hello Leonardo, > try to change your binding zmq.pas file like this: > > function zmq_recv (s: pointer; buf: pointer; len: size_t; flags: > Integer): Integer; cdecl; external libzmq; > > > it should be the correct translation of original code: > > ZMQ_EXPORT int zmq_recv (void *s, void *buf, size_t len, int flags); > > This time it compiles, but hands on the zmq_recv.

Sorry "hands" -> "hangs".

Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Sandro Cumerlato
to Leonardo M. Ramé

I never played with ZeroMQ before, can you please provide the corresponding server side piece of source code?

Sandro Cumerlato

On 23 August 2016 at 14:48, Leonardo M. Ramé wrote: ...

Show quoted text 6 lines
> > This time it compiles, but hands on the zmq_recv. > Sorry "hands" -> "hangs".

Reply by email

Leonardo M. Ramé
to Sandro Cumerlato
Sandro Cumerlato wrote 14 lines
I never played with ZeroMQ before, can you please provide the corresponding server side piece of source code? Sandro Cumerlato On 23 August 2016 at 14:48, Leonardo M. Ramé wrote: ... >> >> This time it compiles, but hands on the zmq_recv. >> > > Sorry "hands" -> "hangs". >

Well, this is the server that's waiting (zmq_recv) for messages.

The client is this (written in "C"):

   void *context = zmq_ctx_new();
   void *requester = zmq_socket(context, ZMQ_REQ);
   int conn = zmq_connect(requester, "ipc://localscp");
   zmq_send(requester, "hello world", 11, 0);
   zmq_close(requester);
   zmq_ctx_destroy(context);
Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

silvioprog
to Leonardo M. Ramé
Leonardo M. Ramé wrote
Hi, I need to use ZeroMQ for inter process communication, but the bindings at https://github.com/bvarga/delphizmq does not seems to work, at least on Linux (XUbuntu 16.04 64bits) and zmq 4.2.

[...]

Just curious, why not FPC's SimpleIPCServer/SimpleIPCClient?

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/89b86a83/attachment.html>

Reply by email

Sandro Cumerlato
to silvioprog

Maybe other side is not written in Free Pascal, I guess.

BTW: I got ZeroMQ TCP working (look at the attachments), IPC not yet :-(

Sandro Cumerlato

P.S: I'm playing with ZeroMQ under Windows only, what about you?

On 23 August 2016 at 17:42, silvioprog wrote:
..

Just curious, why not FPC's SimpleIPCServer/SimpleIPCClient?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: tcp_client.pas
Type: application/octet-stream
Size: 549 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/cbfaa193/attachment.obj> -------------- next part --------------
A non-text attachment was scrubbed...
Name: tcp_server.pas
Type: application/octet-stream
Size: 540 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/cbfaa193/attachment-0001.obj>

Reply by email

silvioprog
to Sandro Cumerlato

On Tue, Aug 23, 2016 at 1:07 PM, Sandro Cumerlato <

wrote: Maybe other side is not written in Free Pascal, I guess.

Indeed.

BTW: I got ZeroMQ TCP working (look at the attachments), IPC not yet :-(

I'm going to try it here with TEncoding, just a moment ...

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/d9b3a7e9/attachment.html>

Reply by email

silvioprog
to silvioprog
silvioprog wrote 14 lines
On Tue, Aug 23, 2016 at 1:07 PM, Sandro Cumerlato < > wrote: > Maybe other side is not written in Free Pascal, I guess. > Indeed. > BTW: I got ZeroMQ TCP working (look at the attachments), IPC not yet :-( I'm going to try it here with TEncoding, just a moment ...

Done. Please check the attached file: first, compile and run the myserver, after, compile and run the myclient. When you run the client it will print '0' and server will print a lot of 'hello world' message in the terminal. I noticed it creates a socket called 'localscp'.

I'm not sure about zmq because it is very new for me.

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/3ce3d7c4/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: myzmq.tar.gz Type: application/x-gzip Size: 5215 bytes Desc: not available URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/3ce3d7c4/attachment.bin>

Reply by email

silvioprog
to silvioprog
silvioprog wrote 24 lines
On Tue, Aug 23, 2016 at 1:15 PM, silvioprog wrote: > On Tue, Aug 23, 2016 at 1:07 PM, Sandro Cumerlato < > > wrote: > >> Maybe other side is not written in Free Pascal, I guess. >> > > Indeed. > > >> BTW: I got ZeroMQ TCP working (look at the attachments), IPC not yet :-( > > > I'm going to try it here with TEncoding, just a moment ... > Done. Please check the attached file: first, compile and run the myserver, after, compile and run the myclient. When you run the client it will print '0' and server will print a lot of 'hello world' message in the terminal. I noticed it creates a socket called 'localscp'. I'm not sure about zmq because it is very new for me.

A possible way to print the 'hello world' message for each client.

=== begin code ===

const
  BUF_SIZE = 1024 * 1000;
var
  context: pointer;
  responder: pointer;
  buf: TBytes;
  recv: Integer;
begin
  while True do
  begin
    context := zmq_ctx_new;
    responder := zmq_socket(context, ZMQ_REP);
    zmq_bind(responder, 'ipc://localscp');
    SetLength(buf, BUF_SIZE);
    while True do
    begin
      recv := zmq_recv(responder, buf[0], BUF_SIZE, 0);
      if recv < 1 then
        Break;
      if recv > BUF_SIZE then
        SetLength(buf, recv);
    end;
    WriteLn(TEncoding.UTF8.GetString(buf));
  end;
end

=== end code ===

Sorry for the weird code, it is just a test, I would need time to read and understand zmq.

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/35c42a97/attachment-0001.html>

Reply by email

Leonardo M. Ramé
to Sandro Cumerlato

I'm using the latest (on Linux x64) and I'm wondering if the bindings only work with the 3.x release. I'll download the 3.2.5 legacy release from here http://zeromq.org/intro:get-the-software

Sandro Cumerlato wrote
ZeroMQ IPC client always print "-1" on my system... at this point I have doubts about the libzmq.dll I am playing with... :-\ Sandro
Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé
Leonardo M. Ramé wrote
I'm using the latest (on Linux x64) and I'm wondering if the bindings only work with the 3.x release. I'll download the 3.2.5 legacy release from here http://zeromq.org/intro:get-the-software

I got the same result, even when using Silvio's code.

It keeps
Leonardo M. Ramé
Medical IT - Griensu S.A.
Av. Colón 636 - Piso 8 Of. A
X5000EPT -- Córdoba
Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

silvioprog
to Sandro Cumerlato: ZeroMQ IPC client always print "-1" on my system... at this point I have doubts about the…

On Tue, Aug 23, 2016 at 2:38 PM, Sandro Cumerlato <

wrote: ZeroMQ IPC client always print "-1" on my system... at this point I have doubts about the libzmq.dll I am playing with... :-\

Strange. :-/

my steps was:

$ cd ~
$ mkdir zeromq && cd zeromq
$ wget -c
https://github.com/zeromq/zeromq4-1/releases/download/v4.1.5/zeromq-4.1.5.tar.gz
$ tar -zxvf zeromq-4.1.5.tar.gz && cd zeromq-4.1.5/
$ ./configure && make
$ sudo cp .libs/libzmq.* /usr/lib/

now just open and compile myserver.lpr/myclient.lpr.

I just copied the files libzmq.* to my lib dir because I don't know zmq makefile, so to uninstall I just did `rm /usr/lib/libzmq.*'.

My environment:

Lazarus 1.7 r52835 FPC 3.1.1 x86_64-linux-gtk 2

$ lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/5a48d318/attachment-0001.html>

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé: I got the same result, even when using Silvio's code. It keeps Leonardo M. Ramé Medical…
Leonardo M. Ramé wrote 12 lines
El 23/08/16 a las 14:45, Leonardo M. Ramé escribió: > I'm using the latest (on Linux x64) and I'm wondering if the bindings > only work with the 3.x release. I'll download the 3.2.5 legacy release > from here http://zeromq.org/intro:get-the-software > I got the same result, even when using Silvio's code. It keeps waiting at zmq_recv.

False alarm, with 3.2.5 it did Work!!!!

Regards,

Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

silvioprog
to Leonardo M. Ramé
Leonardo M. Ramé wrote 15 lines
El 23/08/16 a las 14:58, Leonardo M. Ramé escribió: > El 23/08/16 a las 14:45, Leonardo M. Ramé escribió: > >> I'm using the latest (on Linux x64) and I'm wondering if the bindings >> only work with the 3.x release. I'll download the 3.2.5 legacy release >> from here http://zeromq.org/intro:get-the-software >> >> > I got the same result, even when using Silvio's code. > > It keeps waiting at zmq_recv. > > False alarm, with 3.2.5 it did Work!!!!

Awesome. :-)

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/fffcc190/attachment.html>

Reply by email

Sandro Cumerlato
to Leonardo M. Ramé: False alarm, with 3.2.5 it did Work!!!! Leonardo M. Ramé Medical IT - Griensu S.A. Av.…

Good to know Leonardo! :-)

BUT please let me know if my IPC client/server are working too (look at the attachments), thank you!

Sandro Cumerlato

p.s.: maybe using "patched" zmq.pas header binding file.

On 23 August 2016 at 20:13, Leonardo M. Ramé wrote: ...

Show quoted text 5 lines
False alarm, with 3.2.5 it did Work!!!! Regards,

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ipc_client.pas
Type: application/octet-stream
Size: 608 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/19d9391a/attachment.obj> -------------- next part --------------
A non-text attachment was scrubbed...
Name: ipc_server.pas
Type: application/octet-stream
Size: 534 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/19d9391a/attachment-0001.obj>

Reply by email

Leonardo M. Ramé
to Sandro Cumerlato
Sandro Cumerlato wrote 12 lines
Good to know Leonardo! :-) BUT please let me know if my IPC client/server are working too (look at the attachments), thank you! Sandro Cumerlato p.s.: maybe using "patched" zmq.pas header binding file.

I must add in my project I had to rename the pipe from ipc://localscp to ipc:///tmp/localscp (I don't know how can I name them on Windows). I did the same in your examples and worked like a charm.

Leonardo M. Ramé Medical IT - Griensu S.A. Av. Colón 636 - Piso 8 Of. A X5000EPT -- Córdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

silvioprog
to Leonardo M. Ramé
Leonardo M. Ramé wrote 14 lines
El 23/08/16 a las 16:01, Sandro Cumerlato escribió: > Good to know Leonardo! :-) > > BUT please let me know if my IPC client/server are working too (look > at the attachments), thank you! > > Sandro Cumerlato > > p.s.: maybe using "patched" zmq.pas header binding file. I must add in my project I had to rename the pipe from ipc://localscp to ipc:///tmp/localscp (I don't know how can I name them on Windows). I did the same in your examples and worked like a charm.

I did some changes (sources in attachment) to compile on FPC from trunk (3.1.1) and Sandro's code worked like a charm for me too. :-)

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/b36a1289/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: ipc_client.pas Type: text/x-pascal Size: 607 bytes Desc: not available URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/b36a1289/attachment.pas> -------------- next part -------------- A non-text attachment was scrubbed... Name: ipc_server.pas Type: text/x-pascal Size: 576 bytes Desc: not available URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160823/b36a1289/attachment-0001.pas>

Reply by email

Sandro Cumerlato

Thank you all for the prompt reply!

I was getting crazy... LOL

Bye bye,
Sandro

On 23 Aug 2016 23:22, "silvioprog" wrote:

Leonardo M. Ramé wrote 14 lines
El 23/08/16 a las 16:01, Sandro Cumerlato escribió: > Good to know Leonardo! :-) > > BUT please let me know if my IPC client/server are working too (look > at the attachments), thank you! > > Sandro Cumerlato > > p.s.: maybe using "patched" zmq.pas header binding file. I must add in my project I had to rename the pipe from ipc://localscp to ipc:///tmp/localscp (I don't know how can I name them on Windows). I did the same in your examples and worked like a charm.

I did some changes (sources in attachment) to compile on FPC from trunk (3.1.1) and Sandro's code worked like a charm for me too. :-)

Silvio Clécio --

Reply by email

Paulo Costa
to Leonardo M. Ramé: Hi, I need to use ZeroMQ for inter process communication, but the bindings at…
Leonardo M. Ramé wrote
Hi, I need to use ZeroMQ for inter process communication, but the bindings at https://github.com/bvarga/delphizmq does not seems to work, at least on Linux (XUbuntu 16.04 64bits) and zmq 4.2.

The bindings from bvarga work well with the version 3.2.5 and 32 bits.

The binding have some issues with 64bits. There are at least a few places where you must change integer to ptruint and there can be more subtle problems.

Another problem with version 4.x.x is that at some point they changed the zmq_msg_t structure definition.
The array inside grew from 32 bytes to 64 and they added an extra void pointer:

typedef union zmq_msg_t {unsigned char _ [64]; void *p; } zmq_msg_t;

in

https://github.com/zeromq/libzmq/blob/master/include/zmq.h

the bindings (https://github.com/bvarga/delphizmq/blob/master/zmq.pas) still have

type
   zmq_msg_t = record
     _: Array[0..32-1] of Byte;
   end;

which is the correct definition for version 3.2.5.

Paulo Costa

Reply by email