Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Playing with AI
#1
A multi-channel text-based P2P chat program


  1. Import the required libraries:
Quote:import socket
from threading import Thread, Lock


  1. Create server and client sockets:
Quote:HOST, PORT = 'localhost', 12345
# Server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen()
print(f"Server listening on {HOST}:{PORT}")
# Client
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST, PORT = 'localhost', 12345
client_socket.connect((HOST, PORT))


  1. Implement a function to handle each client connection:
Quote:def handle_client(client_socket):
    while True:
        try:
            data = client_socket.recv(1024).decode('utf-8')
            print(data)
            # Send response back to the client
            response = input(f"Enter response for {data}: ")
            client_socket.send(response.encode('utf-8'))
        except Exception as e:
            print(e)
            break
    client_socket.close()


  1. Create a function to manage multiple clients:
Quote:def handle_connections(server_socket):
    connections = []
    lock = Lock()
    while True:
        client_socket, addr = server_socket.accept()
        print(f"Connected by {addr}")
        # Add the new connection to our list of connections
        with lock:
            connections.append(client_socket)
        # Start a new thread for each connected client
        client_thread = Thread(target=handle_client, args=(client_socket,))
        client_thread.start()

  1. Implement the main server loop to handle multiple clients:
Quote:def main():
    server_socket = create_server_socket()
    handle_connections(server_socket)
if __name__ == "__main__":
    main()
Reply
#2
been playing with AI going to put an AI chat bot online that you can communicate. Been experimenting with it, going work on the character background and lore. I might just put a different one on and let it loose.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)