LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to send custom frame in socket programming (http://www.linuxquestions.org/questions/programming-9/how-to-send-custom-frame-in-socket-programming-678703/)

rahb20 10-23-2008 10:09 PM

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.

thanks

ta0kira 10-24-2008 12:29 AM

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.
ta0kira

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.

theNbomr 10-24-2008 05:29 AM

if you are trying to send & receive non-ip frames, it can be done using raw sockets. For a starter,
Code:

man packet
man 7 raw
man socket

Raw frames support requires root privileges.
--- rod

rahb20 10-24-2008 08:13 AM

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.

dwhitney67 10-24-2008 09:56 PM

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...
send(sdbufframeSize0);

... 

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.


All times are GMT -5. The time now is 10:44 PM.