I am creating a TCP Server x Client communication but I am suffering from a logic problem that I cannot find out why it is happening. The value of my sizeWindow somehow gets the previous value and breaks the size of my window...
As you can see here:
I need to find out why it goes from 256 to 1024, it should be going to 512 but it isn't...
My guess is that it is not updating the sizeWindow value, but I cannot find out why.
To test my project you will need the Server and the Client classes:
Server Class (you should run this class first):
# 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()
This is my Client class (where the problem is...):
# 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()
My best guess is that the problem is in return sizeWindow, packets_sent, id_packet
of the method send_packets
, it returns 3 times..., 2 times it returns the correct value of sizeWindow but somehow the third time it changes the value of sizeWindow to the previous value, creating a great problem in my algorithm...
I don't know if I am forgetting something or if it is a syntax error, but I cannot find why it is happening...
I would be very pleased if someone could try to find out why it is happening.
Many many thanks.