Reputation: 3
I am making a code that the server sends from 1 to 256 kbytes to client. The client receives the message and must return it to the server. The process must be repeated 1000 times. The message is read from a file. The server is sending and the client is picking it up and sending it back, but at a certain moment it stops. I'd like to know what's wrong with my code.
Server:
import socket
hostA = '127.0.0.1'
portA = 50031
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind((hostA, portA))
dest = ('127.0.0.1',50008)
arquivo = 'texto.txt'
arq = open(arquivo , 'r')
arq = arq.read()
for i in range(0, 9):
dado = arq[0:(1024 * (2**i))]
for j in range(0, 1000):
for k in range(0, (len(dado) / 1024)):
x = dado[k:k+1024]
udp.sendto(x, dest)
for k in range(0, (len(dado) / 1024)):
msg, cliente = udp.recvfrom(1024)
udp.close()
Client:
import socket
hostB = '127.0.0.1'
portB = 50008
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind((hostB, portB))
orig = ('127.0.0.1',50031)
dado = ""
for i in range(0, 9):
for j in range(0, 1000):
for l in range(0, ((1024 * (2**i))/1024)):
msg, cliente = udp.recvfrom(1024)
dado += msg
for k in range(0, ((1024 * (2**i))/1024)):
x = dado[k:k+1024]
udp.sendto(x, orig)
udp.close()
Upvotes: 0
Views: 70
Reputation: 182753
Your question asks about "TCP sockets", but you aren't using TCP.
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind((hostA, portA))
You are using UDP. Unlike TCP, UDP does not detect lost packets or retransmit or re-order data.
Your protocol has no tolerance for packet loss. If a single packet from either the server to the client or the client to the server is lost, each side will wait for the other forever.
You have other issues too. Imagine if two datagrams sent by the client are received in the wrong order by the server. The data will be corrupted.
If you want to design on top of UDP rather than TCP, you have to implement yourself everything TCP provides that you need. If you need lost datagram detection and retransmission, you need to implement it yourself or use TCP. Ditto for transmit pacing, handling out of order reception, and so on.
Upvotes: 1