Beispiel #1
0
    def add_new_client(self, UUID):
        new_client_thread = ClientThread(UUID)

        logging.info("Attempting to add new client...")
        self.client_threads[UUID] = new_client_thread
        logging.info("New client added.")

        new_client_thread.start()
        logging.info("Client thread started")
Beispiel #2
0
 def echo_clients(self):
     while self.connected_clients < self.MAX_CLIENTS:
         print("Waiting for client ", self.connected_clients + 1)
         client_socket, addr = self.server_socket.accept()
         print("Client connected from", addr[0], "id", addr[1])
         thread = ClientThread(client_socket, self.queue_messages)
         thread.start()
         self.clients.append(thread)
         self.connected_clients += 1
     self.__server_loop()
Beispiel #3
0
 def accept_client(self, sock_c):
     # create thread to serve client
     new_client_thread = ClientThread(sock_c, None)
     while self.latest_id in self.client_states:
         self.latest_id += 1
     cid = self.latest_id
     new_client_state = ClientState(sock_c)
     self.client_states[cid] = new_client_state
     # assign client to room
     self.assign_client_room(cid, new_client_thread)
     # start client thread
     new_client_thread.start()
Beispiel #4
0
    def run_server(self):
        """
        Starts an FTPServer listening for connections.
        """
        self.server_socket.bind(self.server_address)
        self.server_socket.listen(self.queue_max)
        ip, port = self.server_socket.getsockname()
        created_threads = []
        print("Server has started listening... ip: {}, port: {}".format(
            ip, port))

        while True:
            # wait for connections
            client_socket, (client_ip, port) = self.server_socket.accept()
            print("Connection from {}".format(client_ip))

            # want to verify client user, password before spawing thread
            while True:
                auth_data = client_socket.recv(self.buffer_size)
                print("Recieved: {}".format(auth_data))
                recv_data = auth_data.decode('utf-8').strip().split(",")

                if len(recv_data) != 3:
                    print("The data recieved from client {} is wrong.".format(
                        client_ip))
                    error = "Expected,'rftp','user:user','passwd:passwd'"
                    client_socket.sendall(error.encode('utf-8'))
                    client_socket.shutdown(socket.SHUT_RDWR)
                    client_socket.close()
                    break
                else:
                    user_info = recv_data[1].split(":")
                    pass_info = recv_data[2].split(":")
                    if self.__authenticate_user(user_info[1], pass_info[1]):
                        message = "Success"
                        client_socket.sendall(message.encode('utf-8'))

                        # once we get a connection just spawn a thread and deal with it
                        thread = ClientThread(client_socket, client_ip, port)
                        thread.start()
                        created_threads.append(thread)
                        break
                    else:
                        message = "Unknown"
                        client_socket.sendall(message.encode('utf-8'))
                        client_socket.shutdown(socket.SHUT_RDWR)
                        client_socket.close()
                        break

        for t in created_threads:
            t.join()
Beispiel #5
0
    def start(self):
        self.serversocket.bind((socket.gethostbyname('ipc_server_dns_name'), self.port))
        # print(socket.gethostname())
        self.serversocket.listen(10)

        print(f'Server working on port: {self.port}')

        while self.run_server:
            try:
                (clientsocket, address) = self.serversocket.accept()
            except:
                break
            print(f'Client accepted: {address}')
            ct = ClientThread(self.queryqueue, address, clientsocket)
            ct.start()
Beispiel #6
0
def handle_connection(connection, address):
    ''' Handle clients trying to connect to the server '''

    global PI_CLIENT
    global PI_CLIENT_CONNECTED

    PRINT('Handling connection from ' + ENC_VALUE(address[0]) + '...', INFO)

    # Create a new client thread for the incoming client
    PI_CLIENT = ClientThread()
    PI_CLIENT_CONNECTED = True

    try:
        PRINT('Starting client thread for ' + ENC_VALUE(address[0]) + '...',
              INFO)

        # Create a thread for the client's run method (to send and recieve data)
        client_thread = Thread(target=PI_CLIENT.run,
                               args=(connection, address))
        client_thread.start()  # Starts client_thread
        client_thread.join()  # Wait for the client to disconnect

        PRINT('Disconnected from ' + ENC_VALUE(address[0]) + '.', INFO)

        # Reset client variables
        connection.close()  # Ends connection
        PI_CLIENT = None
        PI_CLIENT_CONNECTED = False

    except Exception as e:  # Prints Error

        PRINT('Could not run client for ' + ENC_VALUE(address[0]) + '.', ERROR)
        PRINT('| ' + str(e), ERROR)
# Create TCP/IP Socket
# socket.AF_INET    --> IPv4
# socket.SOCK_STREAM -> STREAM
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Prevent 'Address already in use'
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind server to port
server_ip = 'localhost'
server_port = 8001
server_address = (server_ip, server_port)
print(f'starting up on {server_ip} port {server_port}')
sock.bind(server_address)

threads = []

while True:
    # Listen for incoming connections
    sock.listen(5)

    # IPs
    client_sock, client_address = sock.accept()
    ip, port = client_address

    # Create thread
    thread = ClientThread(ip, port, client_sock)
    thread.start()

    threads.append(thread)