简体   繁体   中英

Issue in connecting client socket with server socket

I have a server running on a desktop machine with IP address 192.168.1.11, and client code is running on server accessing through OpenVPN connect. When I run the below code client sends the request but server doesn't receives it.

Server.py:

context=zmq.Context()
socket=context.socket(zmq.REP)
socket.bind("tcp://*:8080")
while True:
  message=socket.recv_pyobj()
  print("%s:%s" %(message.get(1)[0],message.get(1)[1]))
  socket.send_pyobj({1:[message.get(1)[0],message.get(1)[1]]})

Client.py

socket=context.socket(zmq.REQ)
socket.connect("tcp://192.168.1.11:8080")
name="Test"
while True:
   message=input("Test Message")
   socket.send_pyobj(({1:[name,message]}))

Thanks help is highly appreciated.

Q : "Issue in connecting client socket with server socket"

Step 0: proof there has been achieved an OSI-ISO-Layer-3 visibility traceroute <targetIP>

Step 1: having achieved a visible route to <targetIP> , repair the code to meet documented REQ/REP properties

Step 2: having achieved a visible route to <targetIP> and REQ/REP , we should improve robustness of the code

context = zmq.Context()
socket  = context.socket( zmq.REP )
socket.bind( "tcp://*:8080" )
#---------------------------------------------- # ROBUSTNESS CONFIGs
socket.setsockopt( zmq.LINGER,     0   )        # .set explicitly
socket.setsockopt( zmq.MAXMSGSIZE, ... )        # .set safety ceiling
socket.setsockopt( ...,            ... )        # .set ...
#---------------------------------------------- # ROBUSTNESS CONFIGs

while True:
    message = socket.recv_pyobj()            # .recv() a request from REQ-side
    print( "%s:%s" % ( message.get(1)[0],    #         shall improve robustness
                       message.get(1)[1]     #         for cases other than this
                       )
           )
    socket.send_pyobj( { 1: [ message.get(1)[0], # REP must "answer" to REQ
                              message.get(1)[1]
                              ]
                         }
                  )

TARGET_IP   = "<targetIP>"                      # <targetIP> from Step 0
PORT_NUMBER = 8080

socket = context.socket( zmq.REQ )
socket.connect( "tcp://{0:}:{1:}".format( TARGET_IP, PORT_NUMBER ) )
#---------------------------------------------- # ROBUSTNESS CONFIGs
socket.setsockopt( zmq.LINGER,     0   )        # .set explicitly
socket.setsockopt( zmq.MAXMSGSIZE, ... )        # .set safety ceiling
socket.setsockopt( ...,            ... )        # .set ...
#---------------------------------------------- # ROBUSTNESS CONFIGs

name = "Test"
while True:
    message = input( "Test Message" )
    socket.send_pyobj( ( { 1: [ name,           # REQ-side sends a request
                                message         #                  here
                                ]               #          bearing a tuple
                           }                    #             with a dict
                         )                      #           having a list
                       )                        #              for a single key
    #------------------------------------------ # REQ-side now MUST also .recv()
    _ = socket.recv()                           #          before it can .send() again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM