-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
70 lines (65 loc) · 2.08 KB
/
Copy pathclient.py
File metadata and controls
70 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import zmq
import time
from threading import Thread
def out(MB,UB,s):
s=s.decode()
index = s.rindex('|users|')
dialog=s[:index]
users=s[index+7:]
try:
MB.SetValue(dialog)
UB.SetValue(users)
except:
print("Error: Can't find the WindowOutPut")
class Contact(Thread):
def __init__(self):
Thread.__init__(self)
self.done=False
def run(self):
while not self.done:
mes='n '+self.name
try:
self.sock.send(mes.encode())
except:
print("Error: Can't send message to the server, when reconnect")
try:
out(self.MB, self.UB, self.sock.recv())
except:
print("Error: Can't get messege from the server, when reconnect")
time.sleep(0.5)
def options(self, name, socket, MB, UB):
self.name=name
self.sock=socket
self.MB=MB
self.UB=UB
class Client():
def __init__(self):
self.name = ""
self.address = ""
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
self.sendSocket = self.context.socket(zmq.REQ)
self.contact=Contact()
def set(self,MB,UB):
self.MB=MB
self.UB=UB
def start(self,address,port,name):
self.name=name
self.contact.done=False
print("Connecting to hello world server")
self.socket.connect("tcp://"+address+":"+port)
self.sendSocket.connect("tcp://"+self.address+":"+port)
self.contact=Contact()
self.contact.options(self.name,self.socket,self.MB,self.UB)
self.contact.start()
def send(self,s):
letter="l "+self.name+": "+s
self.sendSocket.send(str.encode(letter))
out(self.MB, self.UB, self.sendSocket.recv())
def done(self):
self.contact.done=True
def end(self):
try:
self.sendSocket.send(str.encode('e '+self.name))
except:
pass