问题描述
我分配了一个作业,该作业需要使用TCP回显应用程序以三步过程计算往返时间和吞吐量:
-
设置阶段:客户端向服务器发送命令以请求设置。该命令应采用以下格式:
<PROTOCOL PHASE><WS><MEASUREMENT TYPE><WS><NUMBER OF PROBES><WS><MESSAGE SIZE><WS><SERVER DELAY>\n
PS:此处Protocol phase
只能是“ s”,表示这是设置阶段。
- 测量阶段:通过发送以下命令来测量值:
<PROTOCOL PHASE><WS><PROBE SEQUENCE NUMBER><WS><PAYLOAD>\n
PROTOCOL PHASE
在这里是“ m”。
- 终止阶段:终止客户端和服务器之间的连接。命令格式:
<PROTOCOL PHASE>\n
PROTOCOL PHASE
在此处应为“ t”。
因此,例如,如果我要测量10次实验的平均往返时间,并且有效载荷为100字节,那么我的命令应类似于:
python server.py # activate the server
python client.py "s rtt 10 100 0\n"
python client.py "m 1 100\n"
python client.py "t\n"
该错误发生在测量阶段,也就是说,在我发送“ m 1 100 \ n”之后。据说我的client.py中有一个ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
,而我的server.py中有一个{
Connected by ('127.0.0.1',64145)
full_data b''
full_data b's rtt 10 100 0'
full_data length 14
Connected by ('127.0.0.1',64146)
full_data b''
full_data b'm 1 100'
full_data length 7
因此,请帮我检查一下我的代码,因为我是新手,可以在一段时间内找到任何解决方案。
client.py:
import socket
import sys
import time
import matplotlib.pyplot as plt
# HOST = 'csa3.bu.edu' # The server's hostname or IP address
HOST = '127.0.0.1'
PORT = 58444 # The port used by the server
BUFFER_SIZE = 1024 # The max size of one package that the socket can receive at a time
with socket.socket(socket.AF_INET,socket.soCK_STREAM) as s:
s.connect((HOST,PORT))
while True:
# make sure the command ends with '\n'
if sys.argv[1][-2:] == '\\n':
if sys.argv[1][0] == 's':
s.sendall(sys.argv[1][:-2].encode('utf-8'))
print('Setup Command is sent!')
response = s.recv(BUFFER_SIZE)
print(response.decode())
break
elif sys.argv[1][0] == 'm':
s.sendall(sys.argv[1][:-2].encode('utf-8'))
signal = s.recv(BUFFER_SIZE).decode()
# retrieve info that has been defined from the server
try:
probes = int(signal.split(' ')[0])
calculation = signal.split(' ')[1]
delay = int(signal.split(' ')[2])
except:
print(signal)
break
# store the round trip time
results = []
print('Start to measure!')
elements = sys.argv[1][:-2].split(' ')
payload = int(elements[2])
time.sleep(5)
for i in range(probes):
start = time.time()
s.sendall(b''.join([b'p',bytes(payload)]))
print(f'Sent out the {i+1}th probe')
full_data = ''
while len(full_data) < payload:
# make sure the entire package is received
print('here I am!')
data = s.recv(BUFFER_SIZE)
print('here I am 3!')
full_data += data.decode()
end = time.time()
print('Here I am 2!')
print(f'Ping back {full_data}!')
print('Round Trip Time is:',end - start - int(delay))
results.append(end - start)
# result depict:
if calculation == 'rtt':
average = round(sum(results) - len(results) * delay / len(results) * 1000,2)
print(f'The average Round Trip Time is {average} ms')
plt.plot([_ for _ in range(1,11)],[res-delay for res in results],ls =':',marker='+')
plt.title(f'Message size={payload},average={average} ms')
plt.savefig(f'Round Trip Time when size is {payload}.png')
elif calculation == 'tput':
average = round((payload * sum([1/(res-delay) for res in results]) / len([results])) / 1000000,2)
print(f'The average throughput is {average} bps')
plt.plot([_ for _ in range(1,[payload/(res-delay) for res in results],average={average} MBps')
plt.savefig(f'Throughput when size is {payload}.png')
break
elif sys.argv[1][0] == 't':
s.sendall(sys.argv[1][:-2].encode('utf-8'))
print('Termination Command is sent!')
response = s.recv(BUFFER_SIZE)
print(response.decode())
s.close()
break
else:
print('Invalid Message Format')
break
else:
print('Invalid Message Format')
break
server.py:
import socket
import time
import sys
# HOST = 'csa3.bu.edu' # Standard loopback interface address (localhost)
HOST = '127.0.0.1'
PORT = 58444 # Port to listen on (non-privileged ports are > 1023)
BUFFER_SIZE = 1024 # The max size of one package that the socket can receive at a time
with socket.socket(socket.AF_INET,socket.soCK_STREAM) as s:
s.bind((HOST,PORT))
s.listen()
# placeholders
probes = None
message_size = None
delay = None
setup = False
calculation = None
results = []
while True:
conn,addr = s.accept()
with conn:
print('Connected by',addr)
full_data = b''
print('full_data',full_data)
if message_size is None:
full_data += conn.recv(BUFFER_SIZE)
else:
print('message_size is not null')
while len(full_data) < message_size:
print("I'm starting to iterate")
# make sure the entire package is received
if not conn.recv(BUFFER_SIZE):
# no packages are sent
break
print("Iteration terminated!")
data = conn.recv(BUFFER_SIZE)
full_data += data
print('full_data',full_data)
print('full_data length',len(full_data))
# setup phrase
if full_data.decode()[0] == 's':
elements = full_data.decode().split(' ')
if len(elements) == 5 and elements[1] in ['rtt','tput'] and \
type(int(elements[2])) is int and type(int(elements[3])) is int and \
type(int(elements[4])) is int:
calculation = elements[1]
probes = int(elements[2]) # number of probes
# message_size = int(elements[3])
delay = int(elements[4]) # delay time
setup = True # setup complete
conn.sendall(b'200 OK: Ready')
else:
conn.sendall(b'404 ERROR: Invalid Connection Setup Message')
# measurement phrase 1
elif full_data.decode()[0] == 'm':
elements = full_data.decode().split(' ')
if setup:
if len(elements) == 3 and int(elements[1]) >= 1 and \
int(elements[1]) <= probes and type(int(elements[2])) is int:
message_size = int(elements[2])
signal = str(probes) + ' ' + calculation + ' ' + str(delay)
conn.sendall(signal.encode())
else:
conn.sendall(b'404 ERROR: Invalid Measurement Message')
# setup is not complete
else:
conn.sendall(b'404 ERROR: Invalid Phrase')
# measurement phrase 2: receive the payload with a fixed size
# assume this phrase is called "phrase p"
elif full_data.decode()[0] == 'p':
# delay simulation
time.sleep(delay)
if (len(full_data) - 1 == message_size):
print('sent Message back!')
conn.sendall(full_data[1:])
else:
conn.sendall(b'404 ERROR: Incompatible Message Size')
# terminatation phrase
elif full_data.decode()[0] == 't':
if full_data.decode() == 't':
conn.sendall(b'200 OK: Closing Connection')
conn.close()
else:
conn.sendall(b'404 ERROR: Invalid Connection Termination Message')
conn.close()
break
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)