Exemplo n.º 1
0
 def receive_msg(self):
     while True:
         try:
             byte_arr = self.sock.recv(1024)
         except Exception as e:
             print("Receive Error.")
         finally:
             # unpack the bytes
             msg = Msg.decode(byte_arr)
             # handle the msg
             if msg.mode == Msg.HELLO:
                 pass
             elif msg.mode == Msg.TALK:
                 # print the msg
                 # print("[%s:%s] > %s" % (msg.ip, msg.port, msg.data))
                 # return the str(msg)
                 return ("[%s:%s] > %s" % (msg.ip, msg.port, msg.data))
             elif msg.mode == Msg.FINISH:
                 pass
Exemplo n.º 2
0
 def handle_sock(self, sock, addr):
     # receive the msg and handle it
     while True:
         try:
             byte_arr = sock.recv(1024)
             # decode the socket bytes
             msg = Msg.decode(byte_arr)
             # (1.mode), (2,ip), (3,port), (4,data)
             # update the (2, ip) and (3, port)
             msg.set_ip(addr[0])
             msg.set_port(addr[1])
             # handle the msg
             if msg.mode == Msg.HELLO:
                 pass
             elif msg.mode == Msg.TALK:
                 # send the msg to everyone
                 # for user in conns:
                 #     self.send_msg(user, msg)
                 # just print in the server
                 print("[%s:%s] > %s" % (msg.ip, msg.port, msg.data))
             elif msg.mode == Msg.FINISH:
                 pass
         except Exception as e:
             # client logout
             # del the conns who logout
             for i in range(len(conns)):
                 if conns[i].ip == addr[0] and conns[i].port == addr[1]:
                     del conns[i]
             print('now has %s client.' % (len(conns)))
             # send the link out msg for everyone
             msg = Msg(
                 data='[%s:%s] logout' % (addr[0], addr[1]),
                 mode=Msg.TALK,
                 ip=addr[0],
                 port=addr[1],
             )
             for user in conns:
                 self.send_msg(user, msg)
             return
Exemplo n.º 3
0
 def rdt_rcv(self, newsocket: socket.socket, rcvpkt: list):
     msg = newsocket.recv(65510)
     ## not allow me to use Msg here
     newmsg = Msg.decode(msg)
     rcvpkt.append(newmsg)