3

I'm writting a client using socket to receive EEG data from a recording PC (RP) to produce some online feedback.

The RP has a server which sends the data over TCP. The data is being sent with blocks, each having a header and data (alltogether is 2560 bytes). The blocks are sent every 20 ms (50 Hz).

When I run the client, it receives the blocks in bursts (e.g. one block for 40ms then next one instantaniously, 0ms). First I thought this is because the server uses Nagle's algorithm and packets are small to be sent individually, but when I reduce the block size to, say, 400 bytes, the recv() returning time becomes much more stable (around 20ms now. Still some variation but no bursts anymore). Even with the 2.5k packets, the total required bandwidth doesn't look large: 50*2560 = 128 kB/s. Same unstability is present when I'm running both client and server on localhost. What might be the problem here?

Herer's the (simplified) client's code:

# ...
# packet definitions as ctypes structures:
from protocol_defines import header, message

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((addr, port))
hdr = header() # 24 bytes
msg = message() # 2560 bytes

while True:
    s.recv_into(hdr) # this thing should return once the hdr buffer is filled
    # ... check if the message is ok ...
    s.recv_into(msg) # this thing should return once the hdr buffer is filled
    print time.time() # this is how I measure arrival times

UPD: I checked the conversation with wireshark. The problem seems to be in the client: it sends [ACK] packets only after 40ms since the last server's [PSH, ACK] (the server responds almost instantaneously on client's [ACK]). The server has already acquired 2 packets by the time, so it sends 2 glued packets. The question remains opened :(

PS: I'm using Ubuntu with 2.6.35 kernel

dmytro
  • 1,293
  • 9
  • 21
  • 1
    I'm not very good at network programming, but I believe the problem is somewhere in server (and related to network buffers). Have you tried to run a network sniffer (I can recommend wireshark) to check what's going on on network level. – rvs Dec 23 '11 at 16:00
  • Could be, but I've tried two different server implementations both localy and remotely and had the same pattern. Actually that's a nice idea to wireshark the thing, I'll try it. – dmytro Dec 23 '11 at 16:07
  • looks like it's a client's problem. Check the UPD – dmytro Dec 24 '11 at 18:25
  • 1
    The Client is allowed to Delay its ACK as per the TCP spec, it can do so because it can save bandwidth and TCP overhead if it can piggyback it on some data to transmit, so it can wait to see if you give it some data to send. http://srohit.tripod.com/tcpslowstart.txt. However, after the "Slow Start" of the TCP connection, the server should not wait for every ACK before sending the next packet: http://srohit.tripod.com/tcpslowstart.txt – bdk Dec 24 '11 at 18:41

2 Answers2

1

You could try disabling Nagle by:

Possibly using

    sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

will help send each packet as you want it as this disables Nagle's algorithm, as most TCP stacks use this to join several packets of small-sized data together (and is on by default I believe)

as has been suggested in https://stackoverflow.com/a/647835/1132184

Community
  • 1
  • 1
juke
  • 319
  • 3
  • 14
  • Yeah, as I wrote in the description, I tried that one in the first place. Didn't quite work :( – dmytro Feb 05 '12 at 12:20
0

TCP is stream based. There is never a guarantee that you will receive everything exactly as you sent it. Use UDP for that (but UDP does not guarantee that everything arrives or that everything arrives in the same order as being sent.)

Other than that, disable nagle as suggested, since it queues up sent messages to reduce the overhead that the TCP header adds for small packages.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Okay, I'll just leave it with the following thought in mind: "TCP is not for real-time systems" – dmytro Mar 27 '12 at 22:09
  • Depends on how small the time window has to be to be a real-time system. You might have to resend the packets yourself using UDP which could take longer time than the built in features in TCP. – jgauffin Mar 28 '12 at 05:26