0

How do I set the packet size when using recvfrom()?

I am trying to capture tcp packets using a Raspberry Pi Model B. I have a python program that uses recvfrom to capture the packets and write them to a .dat file. To be compatible with the matlab code I wrote for post-processing the data. I need size (bytes) of the file to yield an integer value when divided by the packet size (in this case 8568 bytes). Is this possible? If so how do I do this?

Also I am having to shutdown and power back up the tcp source every time I run the code in order to get the recvfrom function to work. I want this to stop, do I need to add a command to clear the port at the beginning or end of the code?

 while t < 5:
time.sleep(1)
t+=1
print t
i=str(t)
f=open('test_' + i  ,'wb')
radar=s.recvfrom(8568)
data=str(radar)
f.write(data)
print radar
if radar == 0:
    print 'no packets'
if radar != 0:
    print radar

f.close() s.close()

  • Please post some code to expedite the answer process - note: only enough code to show the problem – KevinDTimm Jul 02 '14 at 20:32
  • You want to receive data from a TCP socket, and make sure they're written in batches of 8568 bytes to a file? – johntellsall Jul 02 '14 at 20:36
  • 3
    Are you sure that this is TCP and not UDP? TCP is a *stream-based* protocol, and there's no concept of packet size at the application layer. You use `recv()`, not `recvfrom()` with TCP sockets. Conversely, UDP is a *message-oriented* protocol and does have a packet size (i.e. datagram size) associated with each message; you use `recvfrom()`, not `recv()` with UDP sockets. – Adam Rosenfield Jul 02 '14 at 20:58
  • 1
    your code is very flawed: `recvfrom` returns a tuple, not the packet the `radar` being a tuple is never equal to 0. – Antti Haapala -- Слава Україні Jul 02 '14 at 21:02

0 Answers0