PDA

View Full Version : sending structure through udp socket programming



hemrajn
15th May 2009, 10:15
HI ALL,

I am using udp socket programming to send data from one application to another, is there any way to pass data structure from one application to another?

for example


struct message
{
int id;
char *name;
int sal;
};


this message structure has to be pass through UDP socket from one application to another, please help me out how to communicate by using structure.

Regards,
hemraj

talk2amulya
15th May 2009, 10:33
you can try sending each part of the structure one by one, using writeDatagram

hemrajn
15th May 2009, 10:42
you can try sending each part of the structure one by one, using writeDatagram

Hi talk2amulya,

I done it by passing each member variable into the datastream, but i want to know is there any way to send datastructure through udp.

My problem is at a time i want to transfer same type of structure 5 time.

Is it possible or not?

Regards,
hemraj

talk2amulya
15th May 2009, 11:24
i guess u can send a char* of size 2*size(int) + lenght(name) using writeDatagram and parse it in the other application accordingly..u could even send the whole five structures at once..but you should know how to parse them

hemrajn
15th May 2009, 13:42
i guess u can send a char* of size 2*size(int) + lenght(name) using writeDatagram and parse it in the other application accordingly..u could even send the whole five structures at once..but you should know how to parse them

HI amulya,

Thanks for ur suggestion, but i tried to pass structure by type casting into (char *). sending is ok, but while receiving it show size of the structure. but i am not able to parse the data. is there any class in Qt to parse the data.


Regards,
hemraj

talk2amulya
15th May 2009, 14:14
no, there is no such class and thats good :) its your data, you should to be able to parse it, you know the size of the structure when you are sending it..it shouldnt be that hard

montylee
15th May 2009, 19:16
ya, just put all your data in a single string separated by say semicolon or comma and then parse that data at the receiving end. For parsing the data, you can either use strtok() C function or the functions available in QString class.

auba
15th May 2009, 20:45
XML export is another possibilty :)

aamer4yu
15th May 2009, 21:02
XML will be a easy way I guess.
However you may also look at using QDataStream. You can stream your class through it and read back. Read the documentation to get an idea

auba
16th May 2009, 00:55
Mmmh... in this context: when I use QDatastream and sockets (maybe especially UDP sockets) do I have to deal with timeouts, broken telegrams and things like that? Even with XML and plain text via QCtpSocket, I currently use BOT/EOT bytes, timeouts and 1024k reading buffers to assure that nothing is lost. Is that ok or paranoia? Especially on Windows CE systems, I heard there may could be trouble?