Search code examples
pythonsocketsudppackets

How to split data into equal size packets having variable header size..


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?


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.