import socket
import threading
bind_ip = "0.0.0.0"
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
print(f"[*] Listen on {bind_ip}:{bind_port}")
def handle_client(client_socket):
request = client_socket.recv(1024)
print(f"[*] Received: {request}")
client_socket.send("ACK")
client_socket.close()
while True:
client, address = server.accept()
print(f"[*] Accept connection from {address[0]}:{address[1]}")
client_handler = threading.Thread(target = handle_client, args = (client, ))
client_handler.start()
[*] Listen on 0.0.0.0:9999
Traceback (most recent call last):
File "main.py", line 23, in
client, address = server.accept()
File "/usr/lib/python3.8/socket.py", line 292, in accept
fd, addr = self._accept()
OSError: [Errno 22] Invalid argument
在 server.bind((bind_ip, bind_port))
後面加上
server.listen(n)
.listen(n) => 用於伺服器端最多可接受 n 個 socket 串接