-2

Is there any way to send large amounts of data (around 10,000 bytes) relatively fast? I'm currently using socket.recvfrom and it takes around a minute to recieve each message. Edit: This is how I recover the message, sock is a UDP socket, and max_buffer starts at 1024

 while True:
     try:
         data, addr = sock.recvfrom(max_buffer) # buffer 
         print(data)
         break
     except OSError:
         max_buffer *= 2
 data = json.loads(data)

Im not going to paste the code for getting the message because its pretty long, but its a json dictionary which in total takes up around 10000 bytes. This is how I send it:

sock.sendto(json.dumps(data).encode(), address)

I'm sending messages to the server repeatedly.

Gderu
  • 131
  • 8
  • Over what media are you sending the packets? Ethernet? WiFi? Dial-up modem? How many hops between here and there? I'd expect that 10K bytes would go much more quickly than 1 second, let alone 1 minute. How are you measuring the time? – Robᵩ Jun 19 '18 at 18:19
  • Post your code. There's no reason for it to take a minute to send 10KB. – John Kugelman Jun 19 '18 at 18:54
  • Im sending it to my own computer. – Gderu Jun 19 '18 at 19:18

1 Answers1

1

Is there any way to send large amounts of data (around 10,000 bytes) relatively fast?

Yes, use socket.sendto(). Here is an example client:

import socket
import datetime

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = bytes(i%256 for i in range(10000))
print(datetime.datetime.now())
s.sendto(data, ('localhost', 5000))
print(datetime.datetime.now())

Notice that it creates a bytes object of 10,000 bytes and passes it in a single call to s.sendto().

Using this server:

import socket
import datetime

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 5000))
data, addr = s.recvfrom(100*1024)
print(datetime.datetime.now())

I got this result from the client:

2018-06-19 13:36:13.443418
2018-06-19 13:36:13.443636

I got this result from the server:

2018-06-19 13:36:13.443666

So to the sendto() call took about 2/10000 of a second and the data arrived less than 1/10000 of a second later.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • You showed me the answer. I was starting max_buffer too low, so it was taking a long time for it to go up to a size in which it can recieve the packet. Thanks! – Gderu Jun 19 '18 at 19:25
  • @Gderu - You might find this interesting: https://stackoverflow.com/a/2862176/8747 Especially, "*Use a buffer large enough to hold the biggest packet that your application-level protocol ever sends*". – Robᵩ Jun 19 '18 at 19:43