Playing with AI - admin - 02-26-2026
A multi-channel text-based P2P chat program
- Import the required libraries:
Quote:import socket
from threading import Thread, Lock
- 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))
- 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()
- 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()
- 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()
RE: Playing with AI - admin - 02-27-2026
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.
|