Python中的套接字编程返回错误10061或使我的服务器未接收数据



在学习套接字编程时,我的两个代码之间突然出现了一个奇怪的问题,这取决于我试图运行它们的IP。

服务器:

import socket
import time
import datetime
import filecmp
HOST = 'localhost'
PORT = 9100
n = 1
x = 0
average_list = []
print('I am ready for any client side request n')
file_comparison = "send.txt"
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
while n <= 100:
data = conn.recv(1024)
file = 'receive1.txt';
print('I am starting receiving file', file,'for the',n,'th time')
a = datetime.datetime.now()
f = open(file, 'wb')
f.write(data)
print('I am finishing receiving file', file,'for the',n,'th time')
b = datetime.datetime.now()
rawtime = b - a
millidelta = rawtime * 1000
average_list.append(millidelta)
real_average = ((sum(average_list, datetime.timedelta(0,0))) / n)
print('The time used in milliseconds to receive',file,'for the',n,'th time','is:',millidelta,'milliseconds')
print('The average time to receive',file,'in milliseconds is:',real_average)
if filecmp.cmp(file,file_comparison,shallow=False):
x = x+1
n=n + 1
f.close()
conn.close()
s.close()
print('I am done n')
print('Total errors: ',x,'out of',n-1 )

客户:

import socket
import datetime
import time
import filecmp
#initializing host, port, filename, total time and number of times to send the file
host = 'localhost'
port = 9100
fileName = "send.txt"
n = 1
average_list = []
file_to_send = open(fileName,'rb')
while n <= 100:
data = file_to_send.read(1024)
s=socket.socket()
s.connect((host,port))
s.sendall(data)
#reading the next 1024 bits
print('I am connecting to server side:',host,'n')
print('I am sending file',fileName,'for the',n,'th time')
a = datetime.datetime.now()
print('I am finishing sending file',fileName,'for the',n,'th time')
b = datetime.datetime.now()
rawtime = b - a
millidelta = rawtime * 1000
average_list.append(millidelta)
real_average = ((sum(average_list, datetime.timedelta(0,0))) / n)
print('The time used in milliseconds to send',fileName,'for the',n,'th time','is:',millidelta,'milliseconds')
print('The average time to send',fileName,'in milliseconds is:',real_average)
n = n + 1
file_to_send.close()
s.close()

print('I am done')

在当前的迭代中,我的客户端代码只是在循环中运行,试图将.txt文件的数据发送到一个没有接收到任何内容的服务器。如果我将"localhost"更改为我的实际IP地址,我会让服务器端代码在while循环中循环,而客户端在2次迭代后放弃:

ConnectionRejectedError:[WinError 10061]由于目标计算机主动拒绝,因此无法建立连接

错误引用行15;s.connect((主机,端口((作为问题的原因。最终,我陷入了困境,因为在我认为主机的两个正确实现之间更换主机会给我带来截然不同的结果,而两者都没有按预期工作。

我认为这个错误试图告诉我们,从我看到的其他时间来看,套接字试图连接的端口仍然连接到另一个套接字。

因此,我对为什么会发生这种情况的诊断是,s.close()不在while循环中,所以它一直在制作一个新的套接字,然后尝试在同一端口上连接。

编辑:我有机会在我这边运行它,如果我像这样把插座的整个制作和绑定从循环中拉出来,它对我有效:

import socket
import datetime
import time
import filecmp
#initializing host, port, filename, total time and number of times to send the file
host = 'localhost'
port = 9100
fileName = "send.txt"
n = 1
average_list = []
file_to_send = open(fileName,'rb')
s=socket.socket()
s.connect((host,port))  
while n <= 100:
data = file_to_send.read(1024)
s.sendall(data)
#reading the next 1024 bits
print('I am connecting to server side:',host,'n')
print('I am sending file',fileName,'for the',n,'th time')
a = datetime.datetime.now()
print('I am finishing sending file',fileName,'for the',n,'th time')
b = datetime.datetime.now()
rawtime = b - a
millidelta = rawtime * 1000
average_list.append(millidelta)
real_average = ((sum(average_list, datetime.timedelta(0,0))) / n)
print('The time used in milliseconds to send',fileName,'for the',n,'th time','is:',millidelta,'milliseconds')
print('The average time to send',fileName,'in milliseconds is:',real_average)
n = n + 1
s.close()
file_to_send.close()

这对我来说绝对有效,发送文件100次,收到100次,但我不知道在你的用例中,你是否需要它是一百个新的套接字,而不是一个套接字发送100个成功收到的文件。

最新更新