f# - Why does ZeroMQ's socket.recv call hang in the following code? -
i have encountered problem simple pub-sub example in zeromq. have read plenty of documentation, cannot seem find answer.
i got libzmq
, clrzmq
nuget. both functions below socket address is:
let sktaddr = "tcp://127.0.0.1:3456"
here simple publisher, queues message every second.
// publisher - seems work fine let publisher () : unit = let skt = (new zmq.context()).socket(zmq.sockettype.pub) skt.setsockopt(zmq.socketopt.linger, 0) skt.bind sktaddr skt.sendmore("test_topic", text.encoding.unicode) |> ignore let rec h1 () : unit = let nv = datetime.now.touniversaltime().tostring() printfn "sending value: %s" nv skt.send(text.encoding.unicode.getbytes nv) |> ignore threading.thread.sleep 1000 let swt = new threading.spinwait() swt.spinonce() if console.keyavailable match console.readkey().key | consolekey.q -> () | _ -> h1() else h1() h1()
the following simple subscriber throws no error, hangs @ line indicated below.
// subscriber let subscriber () : unit = let skt = (new zmq.context()).socket(zmq.sockettype.sub) skt.connect sktaddr skt.subscribe("test_topic", text.encoding.unicode) let rec h1 () : unit = let odat = skt.recv() // programme hangs here! let strodat = (new text.unicodeencoding()).getstring odat if odat <> null printfn "received: %s" strodat else printfn "no data received" let swt = new system.threading.spinwait() swt.spinonce() if console.keyavailable match console.readkey().key | consolekey.q -> () | _ -> h1() else h1() h1()
i have read this question, no solution provided. posting new question here.
thanks in advance help.
i believe problem in publisher:
skt.sendmore("test_topic", text.encoding.unicode)
not knowing f#, appears above statement happens outside loop. if subscriber listening on test_topic
, messages originating publisher require topic name precede content each message, publisher must each time sends:
skt.sendmore("test_topic", text.encoding.unicode) skt.send("some data here", text.encoding.unicode)
..try this...
let publisher () : unit = let skt = (new zmq.context()).socket(zmq.sockettype.pub) skt.setsockopt(zmq.socketopt.linger, 0) skt.bind sktaddr let rec h1 () : unit = let nv = datetime.now.touniversaltime().tostring() printfn "sending value: %s" nv skt.sendmore("test_topic", text.encoding.unicode) |> ignore skt.send(text.encoding.unicode.getbytes nv) |> ignore threading.thread.sleep 1000 let swt = new threading.spinwait() swt.spinonce() if console.keyavailable match console.readkey().key | consolekey.q -> () | _ -> h1() else h1() h1()
..and subscriber has receive twice each message:
// subscriber let subscriber () : unit = let skt = (new zmq.context()).socket(zmq.sockettype.sub) skt.connect sktaddr skt.subscribe("test_topic", text.encoding.unicode) let rec h1 () : unit = let topicname = skt.recv() let odat = skt.recv() let strodat = (new text.unicodeencoding()).getstring odat if odat <> null printfn "received: %s" strodat else printfn "no data received" let swt = new system.threading.spinwait() swt.spinonce() if console.keyavailable match console.readkey().key | consolekey.q -> () | _ -> h1() else h1() h1()
Comments
Post a Comment