2011-05-16 17 views
53

इस कोड को महान काम करता है:ज़ीरोमक लोकहोस्ट पर क्यों काम नहीं करता है?

import zmq, json, time 

def main(): 
    context = zmq.Context() 
    subscriber = context.socket(zmq.SUB) 
    subscriber.bind("ipc://test") 
    subscriber.setsockopt(zmq.SUBSCRIBE, '') 
    while True: 
     print subscriber.recv() 

def main(): 
    context = zmq.Context() 
    publisher = context.socket(zmq.PUB) 
    publisher.connect("ipc://test") 
    while True: 
     publisher.send("hello world") 
     time.sleep(1) 

लेकिन इस कोड नहीं * काम करता है:

import zmq, json, time 

def recv(): 
    context = zmq.Context() 
    subscriber = context.socket(zmq.SUB) 
    subscriber.bind("tcp://localhost:5555") 
    subscriber.setsockopt(zmq.SUBSCRIBE, '') 
    while True: 
     print subscriber.recv() 

def send(): 
    context = zmq.Context() 
    publisher = context.socket(zmq.PUB) 
    publisher.connect("tcp://localhost:5555") 
    while True: 
     publisher.send("hello world") 
     time.sleep(1) 

यह इस त्रुटि को जन्म देती है:

ZMQError: No such device

क्यों, zeromq नहीं कर सकते लोकलहोस्ट इंटरफेस का उपयोग करें?

क्या यह केवल उसी मशीन पर आईपीसी पर काम करता है?

उत्तर

33
समस्या

लाइन पर है: @fdb बताते हैं के रूप में

subscriber.bind("tcp://127.0.0.1:5555") 
+0

मैं 127.0.0.101 की तरह एक उच्च पते का उपयोग करना है और आवेदन प्रति यह अलग-अलग हो: दूसरी ओर यह रूप में zmq_tcp के लिए डॉक्स में बाद में चर्चा की zmq_connect के साथ एक DNS नाम का उपयोग करने के बिल्कुल ठीक है। आईपीसी सॉकेट से क्लीनर। –

+17

@fdb हां, यह समस्या को हल करता है, लेकिन यह क्यों समझाता नहीं है! इसे [अधिक स्पष्टीकरण] की आवश्यकता है (http://stackoverflow.com/a/8958414/462302)। – aculich

126

:

subscriber.bind("tcp://localhost:5555") 

कोशिश करने के लिए बदलने के लिए

subscriber.bind("tcp://localhost:5555") 
:

समस्या लाइन पर है

इन्हें बदलने का प्रयास करें:

subscriber.bind("tcp://127.0.0.1:5555") 

हालांकि यह समझने के लिए और अधिक स्पष्टीकरण का हकदार क्यों है।

zmq_bind के लिए दस्तावेज़ बताते हैं (बोल्ड जोर मेरा):

The endpoint argument is a string consisting of two parts as follows: transport://address . The transport part specifies the underlying transport protocol to use. The meaning of the address part is specific to the underlying transport protocol selected.

के बाद से अपने उदाहरण की खोज के लिए परिवहन प्रोटोकॉल हम zmq_tcp दस्तावेज में देखने के लिए (फिर से, बोल्ड जोर मेरा) के रूप में टीसीपी उपयोग करता है:

When assigning a local address to a socket using zmq_bind() with the tcp transport, the endpoint shall be interpreted as an interface followed by a colon and the TCP port number to use.

An interface may be specified by either of the following:

  • The wild-card *, meaning all available interfaces.
  • The primary IPv4 address assigned to the interface, in its numeric representation.
  • The interface name as defined by the operating system.

तो, यदि आप वाइल्ड-कार्ड या इंटरफ़ेस नाम का उपयोग नहीं कर रहे हैं, तो इसका मतलब है कि आपको संख्यात्मक रूप में एक आईपीवी 4 पता (DNS नाम नहीं) का उपयोग करना होगा।

नोट, यह केवल zmq_bind के उपयोग पर लागू होता है!

When connecting a socket to a peer address using zmq_connect() with the tcp transport, the endpoint shall be interpreted as a peer address followed by a colon and the TCP port number to use.

A peer address may be specified by either of the following:

  • The DNS name of the peer.
  • The IPv4 address of the peer, in its numeric representation.
+3

यह एक अजीब कार्यान्वयन है। – huggie

+0

आह एक गैर ऑर्थोगोनल एपीआई – RichardOD

संबंधित मुद्दे