我的 Python 套接字程序有问题 [WinError 10057]
I'm having a problem with my Python socket program [WinError 10057]
我正在编写一个可在两台不同计算机上运行的简单客户端-服务器套接字程序。
服务器是具有静态 ip 地址的台式机,客户端是连接到 Wi-Fi 的笔记本电脑。两者都使用 Windows 10 作为操作系统。
我也开了防火墙端口
这是我的代码。
此代码在一台计算机上运行良好,但当另一台计算机(我的笔记本电脑)尝试连接到服务器时会出现 WinError 10057。
server.py
from socket import *
import sys
HOST = '0.0.0.0'
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
CLIENT_NUM = 5
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(ADDR)
print('bind')
serverSocket.listen(CLIENT_NUM)
print('listen')
while True:
try:
connectionSocket, addr_info = serverSocket.accept()
print('accept')
print('--client information--')
print(connectionSocket)
data = connectionSocket.recv(BUFSIZE)
print('Received data:', data.decode())
connectionSocket.send('OK'.encode())
connectionSocket.close()
except KeyboardInterrupt:
sys.exit(0)
client.py
from socket import *
import sys
HOST = '*.*.*.*' # server's ip address
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
clientSocket = socket(AF_INET, SOCK_STREAM)
try:
clientSocket.connect_ex(ADDR)
clientSocket.send('Hello!'.encode()) # WinError 10057 occurs
except Exception as e:
print(e)
print('%s:%s' % ADDR)
sys.exit(1)
print('connect is success')
receive = clientSocket.recv(BUFSIZE)
print(receive.decode())
clientSocket.close()
我已经修好了。我让单位开放防火墙端口,打开防火墙端口后连接成功
我正在编写一个可在两台不同计算机上运行的简单客户端-服务器套接字程序。 服务器是具有静态 ip 地址的台式机,客户端是连接到 Wi-Fi 的笔记本电脑。两者都使用 Windows 10 作为操作系统。 我也开了防火墙端口
这是我的代码。 此代码在一台计算机上运行良好,但当另一台计算机(我的笔记本电脑)尝试连接到服务器时会出现 WinError 10057。
server.py
from socket import *
import sys
HOST = '0.0.0.0'
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
CLIENT_NUM = 5
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(ADDR)
print('bind')
serverSocket.listen(CLIENT_NUM)
print('listen')
while True:
try:
connectionSocket, addr_info = serverSocket.accept()
print('accept')
print('--client information--')
print(connectionSocket)
data = connectionSocket.recv(BUFSIZE)
print('Received data:', data.decode())
connectionSocket.send('OK'.encode())
connectionSocket.close()
except KeyboardInterrupt:
sys.exit(0)
client.py
from socket import *
import sys
HOST = '*.*.*.*' # server's ip address
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
clientSocket = socket(AF_INET, SOCK_STREAM)
try:
clientSocket.connect_ex(ADDR)
clientSocket.send('Hello!'.encode()) # WinError 10057 occurs
except Exception as e:
print(e)
print('%s:%s' % ADDR)
sys.exit(1)
print('connect is success')
receive = clientSocket.recv(BUFSIZE)
print(receive.decode())
clientSocket.close()
我已经修好了。我让单位开放防火墙端口,打开防火墙端口后连接成功