Results 1 to 4 of 4

Thread: Server can't receive RawData

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Server can't receive RawData

    Hi,

    I'm trying to develop client and server.

    client connects to server and sends raw data.
    This is how client sends data: (I need to send it like raw data)
    Qt Code:
    1. client->abort();
    2. client->connectToHost("my Ip",2342); // for ip I use dyndns
    3. QByteArray block;
    4. QDataStream out(&block, QIODevice::WriteOnly);
    5. out << out.writeRawData("353184039611442", 14);
    6. client->write(block);
    7. client->disconnectFromHost();
    To copy to clipboard, switch view to plain text mode 
    Client code is good, I just can't read it on server.

    Server listens for connection and tries to read data.

    Qt Code:
    1. //constructor//
    2. clientConnection=new QTcpSocket (this);
    3. ServerStart();
    4. connect(&Server, SIGNAL(newConnection()),
    5. this, SLOT(ServerAcceptConnection()));
    6.  
    7. void MainWindow::ServerStart()
    8. {
    9. Server.listen(QHostAddress::Any, 2342);
    10. }
    11. void MainWindow::ServerAcceptConnection()
    12. {
    13. clientConnection = Server.nextPendingConnection();
    14.  
    15. connect(clientConnection, SIGNAL(readyRead()),
    16. this, SLOT(readData()));
    17. }
    18.  
    19. void MainWindow::readData()
    20. {
    21. QString srt, a;
    22. QByteArray byteArray,b,byteArray1;
    23. QBuffer buffer(&byteArray);
    24. buffer.open(QIODevice::WriteOnly);
    25.  
    26. QDataStream in(clientConnection);
    27. while (clientConnection->bytesAvailable())
    28. {
    29. qDebug()<<"data reading started";
    30. in >> b;
    31. byteArray1.append(b);
    32. }
    33. srt.append(b);
    34. qDebug()<< byteArray1.toHex();
    35. qDebug()<< srt;
    36. }
    To copy to clipboard, switch view to plain text mode 
    I get this result
    "192.168.1.1"
    data reading started
    ""
    ""
    Can't understand, what I'm doing wrong.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Server can't receive RawData

    You can simpify your client code to:
    Qt Code:
    1. client->abort();
    2. client->connectToHost("my Ip",2342); // for ip I use dyndns
    3. client->waitForConnected(); // note this
    4. QByteArray block("353184039611442");
    5. client->write(block);
    6. client->waitForBytesWritten(); // note this
    7. client->disconnectFromHost();
    To copy to clipboard, switch view to plain text mode 

    Now what you read at the server is totally incompatible with what you send with the client - if you use writeRawData() on the client, you have to use readRawData() on the server. But again, what you want to do can be simplified to:
    Qt Code:
    1. void MainWindow::readData()
    2. {
    3. if(clientConnection->bytesAvailable() < 14) return; // wait until there are 14 (shouldn't it be 15?) bytes ready
    4. QByteArray data = clientConnection->read(14); // or readAll()
    5. qDebug() << data;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Tadas (19th September 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Server can't receive RawData Solved

    Thanks wysota, it worked,

    But after testing I had some problems if client is device, which send binary data.
    I have changed server function to get data like this:

    Qt Code:
    1. qDebug() << "bytes recieved";
    2. QByteArray buffer;
    3. qDebug() << "avaliable on start" << clientConnection->bytesAvailable();
    4. buffer = clientConnection->readAll();
    5. qDebug() << "read to buffer" << buffer.toHex();
    6. qDebug() << "availiable now" << clientConnection->bytesAvailable();
    To copy to clipboard, switch view to plain text mode 
    This code seems to be more portable, I can receive data from device and client you wrote above.
    I'm just wondering why this code can read data from device, and why my code which I wrote before and yours, can't do this?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Server can't receive RawData Solved

    Quote Originally Posted by Tadas View Post
    But after testing I had some problems if client is device, which send binary data.
    I have changed server function to get data like this:
    Using readAll() in this context is dangerous as you can read too much data and you will lose sync with your device.

    I'm just wondering why this code can read data from device, and why my code which I wrote before and yours, can't do this?
    I don't understand what you are asking about.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qextserialport receive
    By Max123 in forum Newbie
    Replies: 2
    Last Post: 28th March 2010, 22:40
  2. how to receive email by qt4.3
    By ghnie in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2009, 06:24
  3. How can i clear the rawData?
    By Vincenzo in forum Qwt
    Replies: 1
    Last Post: 15th March 2009, 11:45
  4. UDP datagram receive
    By mdannenb in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2008, 03:30
  5. Receive file over TCP
    By winarko in forum Qt Programming
    Replies: 18
    Last Post: 29th May 2008, 17:25

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.