我创建了一个小的 Python 套接字服务器代码,当我尝试连接客户端时,我得到:
OSError: [Errno 57] Socket is not connected
我不确定为什么我得到它,即使服务器正在运行。
这是我的代码: server.py
# Imports
import socket
# Variables
ip_address = ''
ip_port = 10000
max_connections = 5
txt = 'utf-8'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Code
s.bind((socket.gethostname(), ip_port))
s.listen(max_connections)
while True:
clientsocket, address = s.accept()
print(f"{address} connected!")
clientsocket.send(bytes("quit", txt))
client.py
# Imports
import socket
# Variables
ip_address = ''
ip_port = 10000
txt = 'utf-8'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
msg = s.recv(1024)
# Code
"""Connect to the server"""
s.connect((ip_address, ip_port))
while True:
var = msg.decode(txt)
print(var)
if var == "quit":
break
隔江千里
相关分类