Skip to content

Instantly share code, notes, and snippets.

@kamalhm
Created April 9, 2018 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamalhm/8cbbb99be1de9821f7c0d9cc90934b1b to your computer and use it in GitHub Desktop.
Save kamalhm/8cbbb99be1de9821f7c0d9cc90934b1b to your computer and use it in GitHub Desktop.
import zmq
PORT = '4545'
IP = '10.30.0.25'
context = zmq.Context()
print("Menghubungkan ke server...")
socket = context.socket(zmq.REQ)
socket.connect("tcp://{ip}:{port}".format(ip=IP, port=PORT))
tambah = input(
"Terhubung dengan server, apakah anda ingin mengirim pesan ? y/n \n")
if tambah == 'y':
socket.send(b"1")
pesan = socket.recv()
print("menerima pesan %s" % pesan)
request = 0
while True:
pesanKirim = input("Client : ")
if pesanKirim == "stop":
socket.send(b"stop")
else:
socket.send(b"hello clientQ")
print("mengirim request %s..." % request)
request = request + 1
pesanTerima = socket.recv()
if pesanTerima == b'STOP':
break
print("menerima reply %s [%s]" % (request, pesanTerima))
import time
import zmq
PORT = '4545'
IP = '10.30.0.39'
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://{ip}:{port}".format(ip=IP, port=PORT))
lanjut = True
jumlahClient = 0
print("Server berhasil dibuat.")
while True:
message = socket.recv()
pesan = list(message)
if pesan[0] == 49:
jumlahClient += 1
socket.send(b'Halo clientQ')
print("Client saat ini : %s" % jumlahClient)
message = socket.recv()
print("Menerima request: %s" % message)
if message == b'stop':
lanjut = False
time.sleep(1)
if lanjut:
socket.send(b'Hello client')
else:
socket.send(b'STOP')
jumlahClient = jumlahClient - 1
if jumlahClient == 0:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment