【发布时间】:2019-11-27 08:18:38
【问题描述】:
我正在创建一个 TCP 服务器 x 客户端通信,但我遇到了一个逻辑问题,我无法找出它发生的原因。我的 sizeWindow 的值以某种方式获取了以前的值并打破了我的窗口的大小......
如您所见:
我需要找出为什么它从 256 变为 1024,它应该变为 512 但它不是......
我的猜测是它没有更新 sizeWindow 值,但我不知道为什么。
要测试我的项目,您将需要 Server 和 Client 类:
服务器类(你应该先运行这个类):
# ServerTCP
import socket
MY_IP = "127.0.0.1"
PORT_NUMBER = 13000
# socket TCP/IP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (MY_IP, PORT_NUMBER)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
sock.listen(1)
while True:
print('Waiting a connection')
connection, client_address = sock.accept()
try:
print('Connection from ', client_address)
while True:
data = connection.recv(16)
print('received {!r}'.format(data))
if data:
print('sending data back to the client')
connection.sendall(data)
else:
print('no data came from ', client_address)
break
finally:
connection.close()
这是我的客户端类(问题出在哪里......):
# ClientTCP - 4.0
import socket
import time
MY_IP = "127.0.0.1"
PORT_NUMBER = 13000
g_windowTime = 0
# socket TCP/IP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (MY_IP, PORT_NUMBER)
sock.connect(server_address)
def execute():
global g_windowTime
g_windowTime = time.time()
sizeWindow = 1
id_packet = "1"
packets_resend = []
while True:
packets_sent = []
# Send data
sizeWindow, packets_sent, id_packet = send_packets(sizeWindow, packets_sent, packets_resend, id_packet)
# Waits for the amount
amount_received = 0
amount_expected = len(packets_sent)
while amount_received < amount_expected:
try:
sock.settimeout(5.0)
data = sock.recv(16)
amount_received += len(data)
except:
print("The packet needs to be resend")
sizeWindow = sizeWindow * 2
tempo = (time.time() - g_windowTime) * 1000
print(f'{str(round(tempo, 2)).replace(".", ",")}; {int(sizeWindow)}')
if tempo > 10000:
exit()
def send_packets(sizeWindow, packets_sent, packets_resend, id_packet):
global g_windowTime
i = 0
j = 0
timer = 0
while i < sizeWindow:
if packets_resend == []:
packet = id_packet.encode('utf-8')
id_packet = str(int(id_packet) + 1)
elif packets_resend != []:
packet = packets_resend.pop(0)
if packet not in packets_sent:
packets_sent.append(packet)
# Send the packet
try:
sock.sendall(packet)
except:
print("Problem with sendall")
connect()
# Timer
if (i == 0):
timer = time.time()
elif (i > 0) and (time.time() > (timer + 0.01)):
if sizeWindow > 1:
j = i + 1
while j < sizeWindow:
packet = id_packet.encode('utf-8')
id_packet = str(int(id_packet) + 1)
packets_resend.append(packet)
j += 1
sizeWindow = sizeWindow / 2
currentTime = (time.time() - g_windowTime) * 1000
print(f'{str(round(currentTime, 2)).replace(".", ",")}; {int(sizeWindow)}')
send_packets(sizeWindow, packets_sent, packets_resend, id_packet)
i += 1
return sizeWindow, packets_sent, id_packet
def connect():
sock.Connect(server_address)
execute()
sock.close()
我最好的猜测是问题出在方法 send_packets 的 return sizeWindow, packets_sent, id_packet 中,它返回 3 次...,2 次返回 sizeWindow 的正确值,但不知何故第三次它改变了 sizeWindow 的值到以前的值,在我的算法中创造了一个很大的问题......
我不知道是我忘记了什么还是语法错误,但我找不到它发生的原因......
如果有人能尝试找出它发生的原因,我会非常高兴。
非常感谢。
【问题讨论】:
-
乍一看,我没有看到
send_packets方法有什么问题。但是我想你会遇到time.time() > (timer + 0.01)是False的情况,因此不会执行你的sizeWindow = sizeWindow / 2。我会尝试更深入地调查。 -
是的,就是这样,它忽略了
if(它应该这样做)并使用return sizeWindow, packets_sent, id_packet。它进入方法send_packets三次,但最后一次返回sizeWindow 作为前一次。
标签: python python-3.x