473,472 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2539
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
4
by: 0k | last post by:
Hi everyone, I am trying to write a small app that sends multicast udp packets using a socket object. I have more than one NIC on my PC and the following code works OK only if I disable all the...
6
by: Da Vinci | last post by:
Hello, I am starting a project that requires data transfer via UDP. It will be between two different computers. I have all of the information that needs to be sent in the packet and what the...
4
by: QQ | last post by:
Hello I am a newbie on network programming. I am trying to receive a packet if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct sockaddr*)&register_addr, &addr_len))==-1){ fprintf(stderr,...
6
by: SRLoka | last post by:
I am designing a UDP server(still on paper) that will receive data from various client applications(residing on PocketPCs using GPRS) I am using Richard Blum's 'C# Network Programming' as my guide....
1
by: MakeFeel | last post by:
hi. i made ReverseArrary() and i wanna Get PACKET Struct in ReverseArrary of function but i don't know that's error.. How to Use SafeArrary ??? can u help plz..!!!...
2
by: David | last post by:
I have an application that stores the time information received via the serial port from some hardware. The application needs to compare this time gainst the time received in the last packet and...
0
by: George Burdell | last post by:
I'm pretty new to Windows programming (but not programming in general). I'm trying to write a program that will generate IGMP packets where I can control all of the payload in the packet but I'm...
6
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.