Adresse IP : L'identifiant unique de chaque appareil du réseau, utilisé pour localiser l'appareil dans le réseau.
Port : identifie différentes applications réseau sur l'appareil, et un port correspond à un service ou une application spécifique.
Protocole : règles de communication de données, telles que TCP et UDP.
Socket est un handle de communication utilisé pour transmettre des données dans le réseau. La bibliothèque
Python fournit un riche ensemble de fonctions et de classes pour implémenter la programmation réseau. 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"))
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!