4
votes

I am new to zmq and network programming. I want to use pyzmq library to write a simple function to send/receive data between client and server (using REQ/REP pattern). What I did for sending the data was the following:

def send_data(socket, data):
    try:
        socket.send(data)
    except:
        return False
    return True

If the data is successfully sent to the server, the function will return True, if it is not sent to the server, the function will return False. I know above code is too naive.

I don't know how to test the cases when it fail to send the data to the server. One way I can think of is to setup the timeout for sending the data, and send the data to a wrong port. To be more specific, I bind the client socket to port "5555" and I bind the server socket to port "5556". If I send the data, the above function will still return True, and the client program will stuck there forever, because no server is on that port. How can I set the timeout and catch the error? Thus,if there is no responding from the server during a given time, the above function will return False, and so I can do something else if the function return False.

Update(the rest of my code):

def createsocket(port, flag,ipaddr="127.0.0.1"):
# setup socket and port/address binding
    context = zmq.Context()
    if flag == 0:     # server socket
        socket = context.socket(zmq.REP)
        socket.bind("tcp://*:%s" %port)
    else:             # client socket
        socket = context.socket(zmq.REQ)


        socket.connect("tcp://%s:%s" %(ipaddr,port))


    return (socket,port,context)

def recv_response(socket):
    message = socket.recv()
    return message

My purpose is the client side send a signal string "init" to the server to request data. Then the server will correspond, and send some data back to client. The server is always listening. But, it's also possible that the server send data to client without the request of client as long as the client is in the list machine of the server. Maybe there are better pattern than REQ/REP, I plan to try this pattern first as practice.

I want to make sure that the data is sent correctly. But when I read zmq website. It said zmq can ensure either receive the message in a whole piece or do not receive the message at all. My understanding is I don't need to worry about check the length of received data or use checksum to ensure the data is received correctly, because zmq will do this job for me. I only need to know whether the data is received or not. Is that correct?

1
Would you mind to post the whole ZMQ context setup et al? It is important to get answers to your use-case. The send_data() function source code snippet is simply not enough for what you have asked above. Thanksuser3666197
Not at all! Please see the update. However, above are the only code I have written for networking communication. It's in the very beginning stage.ohmygoddess

1 Answers

0
votes

You should have one another signalling layer from client side, that will verify/acknowledge the server if it has received the data or not.

You could use another REQ /REP layer listening at port 5558 ( let's say ).

Each time you send data from server you set a timer and if client responds within that timer, it means data is sent correctly otherwise means client does not exist.