471,119 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

help on packet format for tcp/ip programming

I want a specific packet format for packet exchange between a client
server across the network. For example frame format
as a python class could be:
class Frame:
def __init__(self, buffer=None, count=0, offset=0):
self.buffer = buffer
self.count = count
self.offset = offset
the question is how to convert it to a byte stream so that format of
count and offset also becomes a sequence of bytes. I tried
the pickle module as:
a = Frame()
dat = pickle.dumps(a)
but the size of dat is quite large almost to 4 X the size of a, which
probably is an overkill for the numbers of bytes exchanged. Is there a
simpler solution (similar to C language -- set aside a buffer and fill
it with bytes and network byteorder values for count and offset and
send it out, there is no increase in byte count in the outgoing
packet)?

Any pointers will be appreciated.
-ishwar

Feb 8 '07 #1
9 2444
ra****@cps.cmich.edu wrote:
I want a specific packet format for packet exchange between a
client server across the network. For example frame format
as a python class could be:
class Frame:
def __init__(self, buffer=None, count=0, offset=0):
self.buffer = buffer
self.count = count
self.offset = offset
the question is how to convert it to a byte stream so that format
of count and offset also becomes a sequence of bytes.
Try struct.

Regards,
Björn

--
BOFH excuse #208:

Your mail is being routed through Germany ... and they're censoring
us.

Feb 8 '07 #2
On Feb 8, 1:43 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
rat...@cps.cmich.edu wrote:
I want a specific packet format for packet exchange between a
client server across the network. For example frame format
as a python class could be:
class Frame:
def __init__(self, buffer=None, count=0, offset=0):
self.buffer = buffer
self.count = count
self.offset = offset
the question is how to convert it to a byte stream so that format
of count and offset also becomes a sequence of bytes.

Try struct.

Regards,

Björn

--
BOFH excuse #208:

Your mail is being routed through Germany ... and they're censoring
us.
struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(

-ishwar

Feb 8 '07 #3
On 7 Feb 2007 19:14:13 -0800, ra****@cps.cmich.edu <ra****@cps.cmich.edu>
struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(
Then send the size of the buffer before the buffer, so the recipient
can expect that many bytes.

--
Felipe.
Feb 8 '07 #4
En Thu, 08 Feb 2007 00:14:13 -0300, <ra****@cps.cmich.eduescribió:
On Feb 8, 1:43 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
>rat...@cps.cmich.edu wrote:
I want a specific packet format for packet exchange between a
client server across the network. For example frame format
as a python class could be:
class Frame:
def __init__(self, buffer=None, count=0, offset=0):
self.buffer = buffer
self.count = count
self.offset = offset
the question is how to convert it to a byte stream so that format
of count and offset also becomes a sequence of bytes.

Try struct.

struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(
Use struct to pack count and offset into a fixed size field (2 or 4 bytes
depending on range); send buffer after that.
buffer must come *after* you send its size, else it's a lot harder to
retrieve at the other end.

--
Gabriel Genellina

Feb 8 '07 #5
On 2007-02-08, ra****@cps.cmich.edu <ra****@cps.cmich.eduwrote:
struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(
Oh for Pete's sake...

struct.pack('>%dsIL' % len(buffer), buffer, count, offset)

--
Grant Edwards grante Yow! I want the presidency
at so bad I can already taste
visi.com the hors d'oeuvres.
Feb 8 '07 #6
On Feb 8, 3:40 am, Grant Edwards <gra...@visi.comwrote:
On 2007-02-08, rat...@cps.cmich.edu <rat...@cps.cmich.eduwrote:
struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(

Oh for Pete's sake...

struct.pack('>%dsIL' % len(buffer), buffer, count, offset)

--
Grant Edwards grante Yow! I want the presidency
at so bad I can already taste
visi.com the hors d'oeuvres.
that is great but how does one unpack on the other side?

-ishwar

Feb 8 '07 #7
ra****@cps.cmich.edu wrote:
On Feb 8, 3:40 am, Grant Edwards <gra...@visi.comwrote:
>On 2007-02-08, rat...@cps.cmich.edu <rat...@cps.cmich.eduwrote:
struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(

Oh for Pete's sake...

struct.pack('>%dsIL' % len(buffer), buffer, count, offset)

--
Grant Edwards grante Yow! I want the
presidency
at so bad I can already
taste
visi.com the hors d'oeuvres.

that is great but how does one unpack on the other side?
By peeking into the header, determining the number of bytes necessary?

But why do you need this anyway - if you know that you will have python on
both ends, use Pyro or xmlrpc.

Diez
Feb 8 '07 #8
On 2007-02-08, ra****@cps.cmich.edu <ra****@cps.cmich.eduwrote:
On Feb 8, 3:40 am, Grant Edwards <gra...@visi.comwrote:
>On 2007-02-08, rat...@cps.cmich.edu <rat...@cps.cmich.eduwrote:
struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(

Oh for Pete's sake...

struct.pack('>%dsIL' % len(buffer), buffer, count, offset)

that is great but how does one unpack on the other side?
struct.unpack('>%dsIL' % buflen ,packet)

--
Grant Edwards grante Yow! Yow! Did something
at bad happen or am I in a
visi.com drive-in movie??
Feb 8 '07 #9
On 2007-02-08, Diez B. Roggisch <de***@nospam.web.dewrote:
>>>struct module pack and unpack will only work for fixed size buffer :
pack('>1024sIL', buffer, count. offset) but the buffer size can vary
from one packet to the next :-(

Oh for Pete's sake...

struct.pack('>%dsIL' % len(buffer), buffer, count, offset)


that is great but how does one unpack on the other side?

By peeking into the header, determining the number of bytes necessary?
Better yet, use a protocol that's not brain-dead: send the
buffer size _before_ you send the buffer.
But why do you need this anyway - if you know that you will
have python on both ends, use Pyro or xmlrpc.
--
Grant Edwards grante Yow! A dwarf is passing
at out somewhere in Detroit!
visi.com
Feb 8 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by 0k | last post: by
6 posts views Thread by Da Vinci | last post: by
6 posts views Thread by SRLoka | last post: by
reply views Thread by George Burdell | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.