LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2006, 05:58 PM   #1
carcassonne
Member
 
Registered: Jul 2005
Distribution: Fedora6 x86_64
Posts: 118

Rep: Reputation: 15
C++: Packing buffers for TCP


Folks,

I'm doing that just about everyday in Perl and it's downright easy. But what about C++ ?

The goal is to 'prepare' a buffer ready to be transmitted over TCP. For instance the buffer can have (in the right big-endian network format) :

- a command code (WORD)
- the length of the following string (LONG)
- the ASCII string, non-null terminated

In Perl we simply do:

my $test = pack("vVv", $command, $strLength, $string);

The 'v' being a word in big-endian network format, while the 'V' is a long in same format.

You get the idea.

How is it done in C/C++ ?

Thanks !
 
Old 07-13-2006, 02:41 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Well what i have understood is that you want to make a buffer to transmit over TCP. And you want to have a customized messege structure, like (command code, Length, data)

So simply make a structure using struct key word like

struct myBuffer
{
WORD commandCode;
LONG Length;
String str;
};

Now make an instance of that structure like,

myBeffer buf;
buf.commandCode = 1234; // store the command code.
buf.Length = 1234 // store the length.
buf.str = "asdasdasd" // store the string.

Now your buf is ready to be sent over TCP.

And if you want to store something in Big Indian order or Network Order, you can use the following set of commands,

htons(yourValue) // Host to network order. (for short)
ntohs(yourValue) // Network to host order. (for short)
htonl(yourValue) // Host to network order. (for long)
ntohl(yourValue) // Network to host order. (for long)

Hope that will help you...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vcdimager + problem packing a video kurrupt Linux - Software 0 08-23-2005 12:54 PM
Woody 3.0 Open Ports 1470/tcp/uaiact 1518/tcp/vpvd What for?How can I remove them? alexxxis Debian 5 07-05-2004 05:18 PM
QMail packing a wobbly still AMMullan Linux - Software 2 06-11-2004 09:36 AM
Packing Customise data in Linux sachinnshah Linux - Laptop and Netbook 0 12-11-2003 07:42 AM
High I/O Wait times and TCP buffers? oswald21 Linux - General 0 08-26-2003 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration