Results 1 to 10 of 10

Thread: sending structure through udp socket programming

  1. #1
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb sending structure through udp socket programming

    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
    Qt Code:
    1. struct message
    2. {
    3. int id;
    4. char *name;
    5. int sal;
    6. };
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending structure through udp socket programming

    you can try sending each part of the structure one by one, using writeDatagram

  3. #3
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending structure through udp socket programming

    Quote Originally Posted by talk2amulya View Post
    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

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending structure through udp socket programming

    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

  5. #5
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending structure through udp socket programming

    Quote Originally Posted by talk2amulya View Post
    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
    Last edited by hemrajn; 15th May 2009 at 12:52.

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending structure through udp socket programming

    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

  7. #7
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: sending structure through udp socket programming

    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.

  8. #8
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: sending structure through udp socket programming

    XML export is another possibilty

  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending structure through udp socket programming

    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

  10. #10
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: sending structure through udp socket programming

    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?

Similar Threads

  1. Can any One help me? - Socket Programming?
    By vinod in forum Qt Programming
    Replies: 6
    Last Post: 18th November 2009, 09:46
  2. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  3. sending int array through socket
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 20th December 2007, 08:16
  4. Socket Programming Port53 QTcpSocket/QUdpSocket
    By patrik08 in forum General Programming
    Replies: 5
    Last Post: 10th May 2007, 16:59
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.