Question

I am building peer to peer application in python. Its going to work over UDP. I have function called getHeader(packetNo,totalPackets) which returns me the header for that packet.Depending on size of header I am chopping data, attaching data to header and getting same packet size.

Header size is not fixed because length consumed by different no of digits is different e.g. I am writing header for packetNo=1 as PACKET_NO=1 , its length will be different for packetNo 10, 100,.. etc

I am currently not including no of packets in header. I am just including packet number, I want to include it, but how can I know no of packets prior to computing header size as header should now contain no of packets and NO_OF_PACKETS=--- can be of any length.

I can pass it through some function which will compute no of packets but that will be something like brute force and will consume unnecessary time and processing power. Is there any intelligent way to do it?

Was it helpful?

Solution

Don't use plain-text. Make packet's header a two packed 4-byte (or 8-byte, depending on how many packets you expect) integers, e.g.

import struct
header = struct.pack('!II', packetNo, totalPackets)

Here's documentation for struct module.

OTHER TIPS

Why not zero-pad your number of packets, so that the header becomes fixed. Say you want to support 1 billion packets in a message:

PACKET_NO=0000000001

is the same length as:

PACKET_NO=1000000000

Of course, this will create an upper bound on the possible number of packets, but there has to be some upper limit, no?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top