Exemple #1
0
 def _handle_all_other_sockets(self, sock, inputs):
     try:
         data = connect_util.receive(sock)
         if data:
             # Send as new client's message
             msg = '\n#[' + self.get_client_name(sock) +\
                 ']>>' + data
             # Send data to all except ourself
             for output in self.outputs:
                 if output != sock:
                     connect_util.send(output, msg)
         else:
             print("Chat server: {} hung up"
                   .format(sock.fineno()))
             self.clients -= 1
             sock.close()
             inputs.remove(sock)
             # Sending client leaving info to others
             msg = "\n(Now hung up: Client from %s)"\
                 .format(self.get_client_name(sock))
             for output in self.outputs:
                 connect_util.send(output, msg)
     except socket.error:
         # Remove
         inputs.remove(sock)
         self.outputs.remove(sock)
Exemple #2
0
 def run(self):
     """ Chat client main loop """
     while self.connected:
         try:
             sys.stdout.write(self.prompt)
             sys.stdout.flush()
             # Wait for input from stdin and socket
             readable, writeable, exceptional = select.select(
                 [0, self.sock], [], [])
             for sock in readable:
                 if sock == 0:
                     data = sys.stdin.readline().strip()
                     if data:
                         connect_util.send(self.sock, data)
                 elif sock == self.sock:
                     data = connect_util.receive(self.sock)
                     if not data:
                         print("Client shutting down.")
                         self.connected = False
                     else:
                         sys.stdout.write(data + '\n')
                         sys.stdout.flush()
         except KeyboardInterrupt:
             print("Client interrupted.")
             self.sock.close()
             break
Exemple #3
0
 def _handle_socket_server(self, inputs):
     client, address = self.server.accept()
     print("Chat server: got connection {} from {}"
           .format(client.fileno(), address))
     # Read the login name
     cname = connect_util.receive(client).split('NAME: ')[1]
     # Compute client name and send back
     self.clients += 1
     connect_util.send(client, 'CLIENT: ' + str(address[0]))
     inputs.append(client)
     self.clientmap[client] = (address, cname)
     # Send joining information to other clients
     msg = "\n(connected: New client ({}) from {}"\
         .format(self.clients, self.get_client_name(client))
     for output in self.outputs:
         connect_util.send(output, msg)
     self.outputs.append(client)
Exemple #4
0
 def __init__(self, name, port, host=connect_util.SERVER_HOST):
     self.name = name
     self.connected = False
     self.host = host
     self.port = port
     # Initial prompt
     self.prompt = '[' + \
         '@'.join((name, socket.gethostname().split('.')[0])) + ']> '
     try:
         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.sock.connect((host, self.port))
         print("Now connected to chat server@port {}".format(self.port))
         self.connected = True
         # send my name ...
         connect_util.send(self.sock, 'NAME: ' + self.name)
         data = connect_util.receive(self.sock)
         # contains client address, set it
         addr = data.split('CLIENT: ')[1]
         self.prompt = '[' + '@'.join(((self.name, addr))) + ']> '
     except socket.error:
         print("Failed to connect to chat server @ port {}"
               .format(self.port))
         sys.exit(1)