#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中文網其他相關文章!