Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with server-client example #27

Open
epifanio opened this issue Jun 7, 2017 · 6 comments
Open

issue with server-client example #27

epifanio opened this issue Jun 7, 2017 · 6 comments

Comments

@epifanio
Copy link

epifanio commented Jun 7, 2017

@ashishrv thanks for this documentation.
I was trying to run the client-server example, but after I run the two scripts pairserver.py and pairclient.py I can't see the messages printed in the shell. Nothing is printed. The scripts are:

  • pairserver.py
import zmq
import random
import sys
import time

port = "5556"

if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.bind("tcp://*:%s" % port)

while True:
    socket.send_string("Server message to client3")
    msg = socket.recv()
    print(msg)
    time.sleep(1)
  • pairclient.py
import zmq
import random
import sys
import time

port = "5556"

if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.connect("tcp://localhost:%s" % port)

while True:
    msg = socket.recv()
    print(msg)
    socket.send_string("client message to server1")
    socket.send_string("client message to server2")
    time.sleep(1)

My python and pyzmq versions are:

Python 3.6.1 (default, Apr  4 2017, 09:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
>>> import zmq
>>> zmq.__version__
'16.0.2'
@MrYsLab
Copy link

MrYsLab commented Jun 7, 2017

@epifanio I just ran the code you posted above and it works both on a Raspberry Pi and on Ubuntu 17.04. I am using the same versions of python and zmq as you on the raspberry pi. On Ubuntu I am using python 3.5.3.

@epifanio
Copy link
Author

epifanio commented Jun 7, 2017

Hi @MrYsLab, thanks for confirming it does for you.
I tried on an other machine:
laptop runnin osx as client and a remote debian as server. I allowed the firewall on the server and used its real IP and it works just fine.

Now I wander way is not working on my other laptop (same osx version) , I'll check if has something to do with the fireall .. any clue on how to debug "a denied connection" ?

@epifanio
Copy link
Author

epifanio commented Jun 7, 2017

I just tried on a different machine here in the office and it works fine! I'll investigate why on my laptop is not working .. Thanks for confirming that it should work. I

@epifanio
Copy link
Author

epifanio commented Jun 7, 2017

Oh! seems I found the problem. This I guess has to do with my lack of knowledge about zmq and tcp /socket connection I guess...

To reproduce the problem:

  • run the server script
  • run the client script
  • it should work, then:
  • stop the client (ctr+c)
  • restart the client -> doesn't work anymore. I need to restart the server and re-run the client to have it working again.

If I stop/restart the server (and leave the client running) I can see it still work fine.
If I stop/restart the client .. it doesn't, the server doesn't receive the pessage anymore.
i need to stop/restart the server to see the new messages coming.

I think the code needs to be changed to handle "N" client to connect/disconnect .. I'll try to go through the documents to find a solution. Thanks for any pointer, a help will be great!

@MrYsLab
Copy link

MrYsLab commented Jun 7, 2017

I don't have much experience with pairs. I use pub/sub with a forwarder . The Python Banyan library makes extensive use of this pattern.

@epifanio
Copy link
Author

epifanio commented Jun 8, 2017

Thanks for your help @MrYsLab.
Of course, it was my fault in implementing the code using the wrong socket type.
I changed the code to use: zmq.REQ and zmq.REP for the client and server.

  • client.py
import zmq
import random
import sys
import time

port = "5556"

if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://0.0.0.0:%s" % port)

messaggio='ls -la'
socket.send_string(messaggio)
msg = socket.recv()
print(msg)
  • server.py
import os
import sys
import subprocess
import zmq
import random
import time

port = "5556"

if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:%s" % port)

while True:
    msg = socket.recv()
    print('client meesage: ', msg)
    gmsg=msg.split()
    try:
        proc = subprocess.Popen(gmsg, stdout=subprocess.PIPE)
        output = proc.stdout.read()
        socket.send_string(output)
    except:
        pass
    time.sleep(0.5)

Now it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants