title: how to check send data in zeromq is successful or not
body: <p>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:</p>
  
  <pre><code>def send_data(socket, data):
      try:
          socket.send(data)
      except:
          return False
      return True
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Update(the rest of my code):</p>
  
  <pre><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
  </code></pre>
  
  <p>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.  </p>
  
  <p>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?</p>