Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-23-2008, 10:09 PM   #1
LQ Newbie
Registered: Sep 2008
Posts: 6

Rep: Reputation: 0
how to send custom frame in socket programming

hi dudes !

I want to send a frame from client/server and then makes some checks on the frame type. can some body tell me how to send it.

frame will contain 3 things: type, length, and data

i created a structure, place some data in the structure but the problem is how to send this structure. will its values be accessed on the client and through which variable. will i have to create another instance of the structure in client or what ?

i have also to make check on type field, that if this type of frame then do this work and if this type then do that.

Help is needed from the programmers to please mention the method and little code example will be appreciated.

Old 10-24-2008, 12:29 AM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Have you looked into datagram sockets? You should take a look at info libc and find the section on sockets. As far as transmitting structures: they have to be 100% self-contained, i.e. no pointers, otherwise the receiving end will be looking for the actual data within its own memory space. You also might run into endian-ness or register size problems if you're using two different architectures. That's a lot to take into account, so I'd consider sending text, e.g. XML. Of course, I don't know if that will suit your purpose.

PS Do you mean a frame "proper"? That is at the kernel level, which is generally more work than it's worth unless you're inventing a new protocol, such as a replacement for TCP or UDP. I can't help you with that! Take a look at less /etc/services; I doubt you'll find many that aren't either!

PPS The sending and receiving setup takes 10+ lines on either end; not just a single method. It's a lot of work if you've never dealt with it!

P3S I've been assuming C/C++, but that's because people generally specify a language if it isn't one of those when asking this type of question. I'm also assuming *nix, but that isn't a requirement of the forum.

Last edited by ta0kira; 10-24-2008 at 12:44 AM.
Old 10-24-2008, 05:29 AM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
if you are trying to send & receive non-ip frames, it can be done using raw sockets. For a starter,
man packet
man 7 raw
man socket
Raw frames support requires root privileges.
--- rod
Old 10-24-2008, 08:13 AM   #4
LQ Newbie
Registered: Sep 2008
Posts: 6

Original Poster
Rep: Reputation: 0
i want to establish some client/server communication in C
the connection was established sending a welcome message from the server to the client.
now the client would like to request for some file from the server.
the frame would contain 3 fields, one is type, 2nd is length and 3rd is payload which in this case would be file name.

now the problem is how to send all these three information using a single structure and then how i will display them on the server, that the client has requested for this file, its size is this.
Old 10-24-2008, 09:56 PM   #5
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I don't think a structure is really necessary in your situation. Concerning the data you want to send to the server, consider packing it into a char buffer.

For example:
PHP Code:

int    type      1;
char*  filename  "/etc/hosts";
size_t frameSize sizeof(type) + sizeof(frameSize) + strlen(filename);

// Pack info into buffer...
char buf[1024] = {0};

memcpy(buf, &frameSizesizeof(frameSize));
memcpy(buf sizeof(frameSize), &typesizeof(type));
memcpy(buf sizeof(frameSize) + sizeof(type), filenamestrlen(filename));

// Send buf to server...

You may have to test the code above to ensure that it is syntactically correct. I coded it just now, from memory, and I have not tested it.

I always compile C code with the -std=gnu99 option.

Last edited by dwhitney67; 10-24-2008 at 10:07 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Frame Buffer Programming - Need Documentation qwijibow Programming 3 08-07-2012 02:29 PM
3Com 3c905 - Problems trying to send a frame in my own kernel module gambuzo Linux - Networking 0 05-06-2008 07:30 AM
recv() and send() in socket lrios Programming 5 04-23-2008 11:32 AM
Socket Programming: SMTP program to send email through Unix WinX187 Programming 1 05-12-2007 06:35 AM > Forums > Non-*NIX Forums > Programming

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

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