IP地址:网络中每个设备的唯一标识符,用于在网络中定位设备。
端口:标识设备上的不同网络应用,一个端口对应一个特定的服务或应用。
协议:数据通信的规则,如TCP和UDP。
Socket是一种通信句柄,用于在网络中传输数据。
Python的socket
库提供了丰富的函数和类,用于实现网络编程。
Socket编程主要包括创建socket对象、绑定地址和端口、监听连接、接受和发送数据等操作。
聊天室分为服务器端和客户端。
服务器端负责接收客户端的连接请求,转发客户端的消息。
客户端负责与服务器建立连接,并发送和接收消息。
聊天室需要支持多个客户端同时在线,并实时接收和发送消息。
import socket import threading server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 8080))
server.listen(5)
clients = [] while True: client_socket, client_address = server.accept() clients.append(client_socket) thread = threading.Thread(target=handle_client, args=(client_socket, client_address)) thread.start()
def handle_client(client_socket, client_address): print(f"[INFO] Client {client_address} connected.") while True: try: message = client_socket.recv(1024).decode("utf-8") if not message: break print(f"[{client_address}] {message}") broadcast(client_socket, f"[{client_address}] {message}") except: break print(f"[INFO] Client {client_address} disconnected.") clients.remove(client_socket) client_socket.close() ``
clients.remove(client_socket) client_socket.close()
def broadcast(from_client_socket, message): for client_socket in clients: if client_socket != from_client_socket: client_socket.send(message.encode("utf-8"))
import socket import threading def handle_client(client_socket, client_address): print(f"[INFO] Client {client_address} connected.") while True: try: message = client_socket.recv(1024).decode("utf-8") if not message: break print(f"[{client_address}] {message}") broadcast(client_socket, f"[{client_address}] {message}") except: break print(f"[INFO] Client {client_address} disconnected.") clients.remove(client_socket) client_socket.close() def broadcast(from_client_socket, message): for client_socket in clients: if client_socket != from_client_socket: client_socket.send(message.encode("utf-8")) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("0.0.0.0", 8080)) server.listen(5) print("[INFO] Server is listening...") clients = [] while True: client_socket, client_address = server.accept() clients.append(client_socket) thread = threading.Thread(target=handle_client, args=(client_socket, client_address)) thread.start()
import socket import threading client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 8080))
def receive_message(): while True: message = client.recv(1024).decode("utf-8") print(message) thread = threading.Thread(target=receive_message) thread.start()
while True: message = input() client.send(message.encode("utf-8"))
import socket import threading def receive_message(): while True: message = client.recv(1024).decode("utf-8") print(message) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(("127.0.0.1", 8080)) print("[INFO] Connected to the server.") thread = threading.Thread(target=receive_message) thread.start() while True: message = input() client.send(message.encode("utf-8"))
分别启动聊天室服务器端和多个客户端。在客户端发送消息,观察其他客户端是否能收到消息。尝试断开客户端,观察服务器端和其他客户端的表现。
以上是怎么使用python构建一个自己的聊天室的详细内容。更多信息请关注PHP中文网其他相关文章!