我当前正在创建一个网络,每当我尝试将我的两个设备连接到学校网络时,这些设备就会拒绝相互连接。但是,我在家庭网络上尝试了完全相同的程序,并且连接完全正常。我不确定为什么会出现此问题,我已经在互联网上寻找解决方案,但找不到任何解决方法。
我最初以为该错误是由于我的学校网络上的互联网连接速度慢所致,所以我尝试在负责连接客户端和服务器的部分周围合并一个while循环。在尝试尝试在我的学校网络上进行连接后,该程序无法连接。问题不在于我的任何代码,因为我可以在我的家庭网络上完美运行,并且两个设备都可以连接到服务器。
客户端程序:
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = 'Insert IPv4 Address Here'
self.port = 5555
self.addr = (self.server, self.port)
def getServer(self):
return self.server
def make_Connection(self):
try:
self.client.connect(self.addr) # Connect the client to the server
return pickle.loads(self.client.recv(2048))
except socket.error as e:
print(e)
def send_and_receive(self, data):
try:
self.client.send(pickle.dumps(data))
return pickle.loads(self.client.recv(2048))
except socket.error as e:
print(e)
### --- Client --- ###
network = Network()
output = network.Make_Connection()
print(output)
output= network.send_and_receive('Hello World')
print(output)
服务器程序:
from _thread import *
import socket
import pickle
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('', 5555))
except socket.error as e:
print(e)
s.listen(2)
# functions #
def ThreatedClient(conn, player):
conn.send(pickle.dumps('Connection Established'))
while True:
try:
new_data = pickle.loads(conn.recv(2048))
if not new_data:
print('Disconnected...')
break
else:
if player == 0:
reply = pickle.dumps('This is Player 1')
else:
reply = pickle.dumps('This is Player 2')
conn.sendall(reply) # Send reply to clients
except:
break
print('Connection Lost ... ')
conn.close()
### --- Thread Starts Here --- ###
current_player = 0
while True:
c, address = s.accept() # Accepts connection from client
print('Connected to:', address)
start_new_thread(ThreatedClient, (c, current_player))
current_player += 1
在服务器中,预期输出应为:
Connected to: ('IPv4 Address', Port)
在客户端中,预期输出应为:
Connection Established
This is Player 1
玩家1和:
Connection Established
This is Player 2
播放器2。
当我在学校网络上运行相同的程序时,输出为errno 32:管道损坏