Subscriber does not receive message, Pyzmq












0















I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub pattern not sure how to incorporate that idea.





This is my codes:



Publisher:



import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)


Subscriber:



context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())


In terms of raw UDP, I wrote a code which works perfectly.



broadcast:



def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)

sock.sendto(msg.encode(), (Host,port))


recv:



def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)









share|improve this question

























  • Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?

    – barny
    Nov 21 '18 at 22:37


















0















I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub pattern not sure how to incorporate that idea.





This is my codes:



Publisher:



import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)


Subscriber:



context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())


In terms of raw UDP, I wrote a code which works perfectly.



broadcast:



def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)

sock.sendto(msg.encode(), (Host,port))


recv:



def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)









share|improve this question

























  • Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?

    – barny
    Nov 21 '18 at 22:37
















0












0








0


0






I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub pattern not sure how to incorporate that idea.





This is my codes:



Publisher:



import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)


Subscriber:



context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())


In terms of raw UDP, I wrote a code which works perfectly.



broadcast:



def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)

sock.sendto(msg.encode(), (Host,port))


recv:



def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)









share|improve this question
















I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub pattern not sure how to incorporate that idea.





This is my codes:



Publisher:



import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)


Subscriber:



context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())


In terms of raw UDP, I wrote a code which works perfectly.



broadcast:



def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)

sock.sendto(msg.encode(), (Host,port))


recv:



def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)






python sockets zeromq pyzmq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 22:24









Benyamin Jafari

2,96832139




2,96832139










asked Nov 20 '18 at 16:23









keerthanakeerthana

378




378













  • Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?

    – barny
    Nov 21 '18 at 22:37





















  • Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?

    – barny
    Nov 21 '18 at 22:37



















Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?

– barny
Nov 21 '18 at 22:37







Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?

– barny
Nov 21 '18 at 22:37














1 Answer
1






active

oldest

votes


















1














You forgot the zmq.SUB in the subscriber side. And you typed sub.setsocketopt() instead of sub.setsockopt().





Try it:



Publisher:



import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.

while True:
socket.send_string('hello')
time.sleep(1)




Subscriber:



context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')

while True:
print(sub.recv())




[NOTE]:




  • You can also change the .bind() and .connect() in subscriber and publisher with your policy. (This post is relevant).

  • Make sure that 5677 is open in the firewall.


  • socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.






share|improve this answer


























  • Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

    – keerthana
    Nov 23 '18 at 16:53













  • @keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

    – Benyamin Jafari
    Nov 23 '18 at 16:55













  • Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

    – keerthana
    Nov 24 '18 at 10:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397299%2fsubscriber-does-not-receive-message-pyzmq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You forgot the zmq.SUB in the subscriber side. And you typed sub.setsocketopt() instead of sub.setsockopt().





Try it:



Publisher:



import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.

while True:
socket.send_string('hello')
time.sleep(1)




Subscriber:



context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')

while True:
print(sub.recv())




[NOTE]:




  • You can also change the .bind() and .connect() in subscriber and publisher with your policy. (This post is relevant).

  • Make sure that 5677 is open in the firewall.


  • socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.






share|improve this answer


























  • Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

    – keerthana
    Nov 23 '18 at 16:53













  • @keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

    – Benyamin Jafari
    Nov 23 '18 at 16:55













  • Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

    – keerthana
    Nov 24 '18 at 10:16
















1














You forgot the zmq.SUB in the subscriber side. And you typed sub.setsocketopt() instead of sub.setsockopt().





Try it:



Publisher:



import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.

while True:
socket.send_string('hello')
time.sleep(1)




Subscriber:



context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')

while True:
print(sub.recv())




[NOTE]:




  • You can also change the .bind() and .connect() in subscriber and publisher with your policy. (This post is relevant).

  • Make sure that 5677 is open in the firewall.


  • socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.






share|improve this answer


























  • Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

    – keerthana
    Nov 23 '18 at 16:53













  • @keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

    – Benyamin Jafari
    Nov 23 '18 at 16:55













  • Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

    – keerthana
    Nov 24 '18 at 10:16














1












1








1







You forgot the zmq.SUB in the subscriber side. And you typed sub.setsocketopt() instead of sub.setsockopt().





Try it:



Publisher:



import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.

while True:
socket.send_string('hello')
time.sleep(1)




Subscriber:



context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')

while True:
print(sub.recv())




[NOTE]:




  • You can also change the .bind() and .connect() in subscriber and publisher with your policy. (This post is relevant).

  • Make sure that 5677 is open in the firewall.


  • socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.






share|improve this answer















You forgot the zmq.SUB in the subscriber side. And you typed sub.setsocketopt() instead of sub.setsockopt().





Try it:



Publisher:



import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.

while True:
socket.send_string('hello')
time.sleep(1)




Subscriber:



context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')

while True:
print(sub.recv())




[NOTE]:




  • You can also change the .bind() and .connect() in subscriber and publisher with your policy. (This post is relevant).

  • Make sure that 5677 is open in the firewall.


  • socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 7:11

























answered Nov 21 '18 at 22:40









Benyamin JafariBenyamin Jafari

2,96832139




2,96832139













  • Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

    – keerthana
    Nov 23 '18 at 16:53













  • @keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

    – Benyamin Jafari
    Nov 23 '18 at 16:55













  • Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

    – keerthana
    Nov 24 '18 at 10:16



















  • Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

    – keerthana
    Nov 23 '18 at 16:53













  • @keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

    – Benyamin Jafari
    Nov 23 '18 at 16:55













  • Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

    – keerthana
    Nov 24 '18 at 10:16

















Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

– keerthana
Nov 23 '18 at 16:53







Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari

– keerthana
Nov 23 '18 at 16:53















@keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

– Benyamin Jafari
Nov 23 '18 at 16:55







@keerthana socket.bind("tcp://*:5677") or socket.bind("tcp://0.0.0.0:5677") is broadcasting trick.

– Benyamin Jafari
Nov 23 '18 at 16:55















Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

– keerthana
Nov 24 '18 at 10:16





Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(

– keerthana
Nov 24 '18 at 10:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397299%2fsubscriber-does-not-receive-message-pyzmq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith