Results 1 to 5 of 5

Thread: QTcpSocket write() Trasmission Data Design

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket write() Trasmission Data Design

    Hi,
    I have a network application.
    I want communicating between client and server whit QTcpsocket.

    My server process some information that i want to be able to send to the client.
    This information is a struct:

    typedef struct Test{
    QTcpSocket socket;
    QString string;
    };

    How I can transfer this structure with TcpSocket?
    The signature of "write" is:

    write ( const char *, qint64 ) : qint64
    write ( const char * ) : qint64
    write ( const QByteArray & ) : qint64
    writeData ( const char *, qint64 ) : qint64

    I don't know how use the write function of tcpSocket to do it.

    Can anyone help me please?

    Thanks, Bye

  2. #2
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket write() Trasmission Data Design

    I have try whit QDataStream:

    Qt Code:
    1. QByteArray block;
    2.  
    3. QDataStream out(&block, QIODevice::WriteOnly);
    4. out.setVersion(QDataStream::Qt_4_0);
    5. out << (quint16)0;
    6. out << m_xMyStruct;
    7. out.device()->seek(0);
    8. out << (quint16)(block.size() - sizeof(quint16));
    To copy to clipboard, switch view to plain text mode 

    where MyStruct is a personal data type.

    The problem is that QDataStream support operator << only for predefined data type.

    Thanks, Bye

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket write() Trasmission Data Design

    Quote Originally Posted by Daxos View Post
    The problem is that QDataStream support operator << only for predefined data type.
    Then you need to create a class that implements the << and >> operators.
    After that you need to register it.

    That said, you don't need to send the socket object as this is completely wrong logic. The object only lives on your pc.
    Why do you want to do this?

  4. The following user says thank you to tbscope for this useful post:

    Daxos (16th July 2010)

  5. #4
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket write() Trasmission Data Design

    Quote Originally Posted by tbscope View Post
    Then you need to create a class that implements the << and >> operators.
    After that you need to register it.
    Mmmm, do you know how I can do it? I think that it's complicated..
    Why Qt doesn't be able to send persona data type?

    Quote Originally Posted by tbscope View Post
    That said, you don't need to send the socket object as this is completely wrong logic. The object only lives on your pc.
    Why do you want to do this?
    Sorry, is an cut&paste error .

  6. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket write() Trasmission Data Design

    Quote Originally Posted by Daxos View Post
    Mmmm, do you know how I can do it? I think that it's complicated..
    Why Qt doesn't be able to send persona data type?
    But Qt can do this, you only need to tell what and how. Nokia can't provide serialisation of data for each and every case. That's why there is a mechanism to do this yourself.

    Some suggestions to make it yourself a little bit easier.
    Forget about the << and >> operators, instead do one of the following:

    1. If you only need to send text and not binary data, use a QTextStream
    2. If you need to send binary data too, use a QDataStream

    In both cases, you can do one of the following:
    1. Each segment of your stream has a certain predefined length (like in the old'en days), then you can parse it based on segment length.
    2. Use a separator like a comma for example.

    Both the above suggestions are stupid, you waste bandwidth in the first case and you make it yourself difficult by excluding control characters in the second case.

    Therefor I suggest you use the following class:
    http://doc.qt.nokia.com/4.6/qxmlstreamwriter.html
    and
    http://doc.qt.nokia.com/4.6/qxmlstreamreader.html

    They are very easy to use and you can keep your code top level without going into the gory details. You can even plug those directly into a tcp socket.

Similar Threads

  1. QTcpSocket Write & \0 \x00
    By NewGuy5800 in forum Newbie
    Replies: 1
    Last Post: 25th April 2010, 10:23
  2. How to write data into a file
    By grsandeep85 in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2009, 08:51
  3. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  4. QTcpSocket waiting for write to be read
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 19:12
  5. TCP Write Raw data
    By ^NyAw^ in forum General Programming
    Replies: 19
    Last Post: 23rd November 2007, 16:38

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.