1

I have created a TCP client in python, hoping it would listen for the constant stream of data being thrown at it. But it hangs after just reading 10 bytes.

Python:

import socket


TCP_IP = '10.0.0.25'
TCP_PORT = 31031
BUFFER_SIZE = 4096
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

while 1:
    data = s.recv(BUFFER_SIZE)
    print (data)

Output:

b'\xff\x00\x00\x00\x00\x00\x00\x00\x01\x7f'

ZMQ:

class ZmqClient(object):

      def __init__(self):

          try:
               self.host = '10.0.0.25'
               self.port = 31031
               context = zmq.Context()
               self.socket = context.socket(zmq.SUB)
               self.socket.connect("tcp://{0}:{1}".format(self.host, self.port))
               self.socket.setsockopt_string(zmq.SUBSCRIBE, "")

          except Exception as e:
               logger.exception("CAN'T ESTABLISH CONNECTION WITH ZMQ SERVER : %s" % e)

      def startReceiving(self): 

          while 1:
                SocketData = self.socket.recv(4096)
                print(SocketData)

if __name__ == "__main__":
    z = ZmqClient()
    z.startReceiving()

Output:

b'\x04\x00\x00\x00BSE\x00\x00\x00\x00\x00\x00\x00\x00\x01\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
   b'\x04\x00\x00\x00BSE\x00\x00\x00\x00\x00\x00\x00\x00\x01\x1c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

I want to replicate the behavior of ZMQ in Python, actually I was writing a TCP client for tornado and found the same issue with it, so was able to replicate it in simple Python client.

What am I doing wrong?

1 Answer 1

1

You have many mistakes. First of all, ZeroMQ has its own server configuration to interact with SUB clients.

Second, the configuration of your socket server is wrong. You are setting it up as a client.

If you want to work with zmq, i leave you a correct client/server example (PUB/SUB Pattern):

ZMQserver.py

# -*- coding: utf-8 -*-

import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:31031")

while True:
    socket.send_string("Hola mundo!")
    time.sleep(1)

ZMQclient.py

# -*- coding: utf-8 -*-

import zmq

host = 'localhost'
port = 31031

#  Socket to talk to server #
context = zmq.Context()
socket = context.socket(zmq.SUB)
print("Receiving messages...")
socket.connect("tcp://{}:{}".format(host, port))
socket.setsockopt_string(zmq.SUBSCRIBE, u'')

while True:
    msg = socket.recv_string()
    print msg

In this example the server sends messages every second to all clients that subscribe to it.

You can also check the code in the zmq Doc: server | client

For what you want to do, zmq is one of the best options.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.