🔒 Locked
Using a struct as a buffer with recvfrom (UDP, C)
Advertisement
Original Post
This has driven me potty all day yesterday and today. I have an app which is squirting a gob of data at me via UDP. It's a fairly complex chunk of data which is organised as a struct. How can I use a struct as the receive buffer with recvfrom ? No matter what I try, it bitches at me. This is what I want to use... struct AIRSTAT { float : pitch; float : roll; float : yaw; etc... }; AIRSTAT mybuf; How would/could I refer to mybuf using recvfrom? Would a union work which would combine my struct with a char array? I'm stuck - and believe me I've tried!! Help!
Nup. I've tried that, and just about every combination c has to offer!
With time always short, I finally relented and with my structure packed(1) I can memcpy the input char buffer to the struct and access the data from the struct.
Works like a charm, just a shame that I've got a 2k memcpy command in there that I really don't think I should need.
I've used Delphi a lot over the last 10 years and that gives you the wonderfully useful "absolute" command which allows you to 'overlay' variables on top of each other. A bit like a union, just easier.
I'm surprised I was unable to find a real solution to this problem, it isn't uncommon to send large and/or complex data structures across the net and being able to TX/RX structures of data would make life wonderfully easy.
Cheers.
With time always short, I finally relented and with my structure packed(1) I can memcpy the input char buffer to the struct and access the data from the struct.
Works like a charm, just a shame that I've got a 2k memcpy command in there that I really don't think I should need.
I've used Delphi a lot over the last 10 years and that gives you the wonderfully useful "absolute" command which allows you to 'overlay' variables on top of each other. A bit like a union, just easier.
I'm surprised I was unable to find a real solution to this problem, it isn't uncommon to send large and/or complex data structures across the net and being able to TX/RX structures of data would make life wonderfully easy.
Cheers.
Original post by FlyingSolo
Nup. I've tried that, and just about every combination c has to offer!/quote]In what way does it not work? That's the usual way of sending structs via sockets.
The MS C compiler whined about being unable to convert from my struct type to a char type.
Just had a chat with a friend of mine who has been a c programmer for about 35 gazillion years - and he can't do it either!
Just had a chat with a friend of mine who has been a c programmer for about 35 gazillion years - and he can't do it either!
Quote:
Original post by FlyingSolo
The MS C compiler whined about being unable to convert from my struct type to a char type.
Just had a chat with a friend of mine who has been a c programmer for about 35 gazillion years - and he can't do it either!
So... How about posting some code...
As well as the version of the compiler you are using. Microsoft doesn't have a C compiler, so this is suspect at best.
Also - post a hex dump of the data you are trying to decode (Wireshark or similar).
And - post the code that is actually sending the data, regardless of language.
Quote:
Original post by FlyingSolo
The MS C compiler whined about being unable to convert from my struct type to a char type.
Sounds to me like you're trying to cast the struct type itself (as opposed to a pointer to it) into a char*... For example:
AIRSTAT myStruct;recvfrom(sock, (char*)myStruct, sizeof(myStruct), &from, &fromLen); // notice the missing '&' in front of myStruct. You can't cast a non-pointer type to a char* just like that.
Oooh, lemme see....
struct nr {
float altitude;
float Vind;
float Alpha;
etc...
};
nr netrec;
char inbuf[2000];
int rxbytes=recvfrom(mysocket,(char*)inbuf, maximum_packet_size,0,(sockaddr*)&from,&fromLength);
Compiler is VC Express 2008.
Since I've now used memcpy I know that the integrity of the incoming data is good. It's a big lump at about 2k and no, I can't post the sending code.
Trying any which way to convince recvfrom to accept 'netrec' as the target buffer failed. The usual error was: "cannot convert from 'nr' to 'char *'"
Quote:The original code by Badgerr will work fine:
Original post by FlyingSolo
Oooh, lemme see....
struct nr {
float altitude;
float Vind;
float Alpha;
etc...
};
nr netrec;
char inbuf[2000];
int rxbytes=recvfrom(mysocket,(char*)inbuf, maximum_packet_size,0,(sockaddr*)&from,&fromLength);
Compiler is VC Express 2008.
Since I've now used memcpy I know that the integrity of the incoming data is good. It's a big lump at about 2k and no, I can't post the sending code.
Trying any which way to convince recvfrom to accept 'netrec' as the target buffer failed. The usual error was: "cannot convert from 'nr' to 'char *'"
int rxbytes=recvfrom(mysocket,(char*)&netrec, maximum_packet_size,0,(sockaddr*)&from,&fromLength);
So long as sizeof(netrec) <= maximum_packet_size. If you have variable sized packets, you'll need to do some manipulation.
Quote:
Original post by FlyingSolo
The usual error was: "cannot convert from 'nr' to 'char *'"
As pointed out you are doing this:
(char*)netrecinstead of:
(char*)&netrecYou need to cast a pointer to netrec to char*, not try to cast the struct itself to char*.
Methinks you should re-read the chapter on pointers.
-me
Ok, I'm horribly embarrassed here. The sucker just compiled using badgerr's typecasting - but I tried that yesterday.
Time to fire up the other app and see what the data looks like...
<click><load><wait>
Well I'll be damned, it works now.
I don't know what to say here - I tried /exactly/ the same code yesterday and it refused point blank. This is confusing in the extreme.
Although I've added a lot of other code since yesterday, I can't see there is anything new that would/could have affected this. I'm so sorry guys - I really don't know what to say!
Time to fire up the other app and see what the data looks like...
<click><load><wait>
Well I'll be damned, it works now.
I don't know what to say here - I tried /exactly/ the same code yesterday and it refused point blank. This is confusing in the extreme.
Although I've added a lot of other code since yesterday, I can't see there is anything new that would/could have affected this. I'm so sorry guys - I really don't know what to say!
Quote:
Original post by Palidine
Methinks you should re-read the chapter on pointers.
Oh I did, believe me! I would never post a question without doing all the research I could. I spent all day and all evening tinkering with this. All my C books got dusted off and Google took a battering.
I feel rather sheepish at the moment...
Quote:
Original post by Antheus
I wonder how the guy with bajillion years of C experience feels...
He now claims he told me to use that method. ;-/
I'm just happy that it works.
I can feel another network issue coming on but I'm afraid to mention it now.
Don't worry, the net works - but it works too well!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement