Results 1 to 7 of 7

Thread: QTcpServer not reading as fast a QTcpSocket writing.

  1. #1
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QTcpServer not reading as fast a QTcpSocket writing.

    Hello everybody,
    i have a strange pbm here. I have QTcpServer Which do listening accepting new connections putting data on dialog level. I have fortune client type code which transfers the data to server.
    I have few problems here.Major problem is QTcpServer is not reading as fastly as client is writing.

    code for server is
    Qt Code:
    1. void MyDialog::settingServer()
    2. {
    3. tcpServer=new QTcpServer(this);
    4. if(!(tcpServer->listen(QHostAddress::Any,35000)))
    5. { cout<<"\n Tcp Server not listening";
    6. QMessageBox::critical(this,tr("server"),tr(" not able to start"));
    7. }
    8. else
    9. {
    10. int x=tcpServer->serverPort();
    11. label->setText(QString("Server is listening on port ")+QByteArray::number(x));
    12. connect(tcpServer,SIGNAL(newConnection()),this,SLOT(activatingNewConnection()));
    13. }
    14. }
    15.  
    16. MyDialog::~MyDialog()
    17. {
    18.  
    19. }
    20.  
    21. void MyDialog::activatingNewConnection()
    22. {
    23. tcpSocket=tcpServer->nextPendingConnection();
    24. connect(tcpSocket, SIGNAL(disconnected()),tcpSocket, SLOT(deleteLater()));
    25. connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataAvailable()));
    26.  
    27.  
    28. }
    29.  
    30. void MyDialog::dataAvailable()
    31. {
    32.  
    33. QDataStream in(tcpSocket);
    34. in.setVersion(QDataStream::Qt_4_0);
    35. if(tcpSocket->bytesAvailable() <= 0 )
    36. {
    37. //cout<<"\nNothing Available for reading";
    38. return;
    39. }
    40. QString st;bool ok;
    41. int i;
    42. in >> st;
    43. label->setText(st);
    44.  
    45. }
    To copy to clipboard, switch view to plain text mode 
    important code for client is
    Qt Code:
    1. void Client::sendingData(int x,int y)
    2. {
    3. QByteArray block;
    4. QDataStream stream(&block,QIODevice::ReadWrite);
    5. stream.setVersion(QDataStream::Qt_4_0);
    6. QString temp;
    7.  
    8. temp = QString( QString(QByteArray::number(x)));
    9.  
    10. stream.device()->seek(0);
    11. stream << temp;
    12. cout<<"\n"<<temp.toInt()<<endl;
    13. QString st="he;llo world";
    14. cout<<st.data()<<endl;
    15. if(tcpSocket->write(block)==-1)
    16. QMessageBox::critical(this,tr("Socket Warning"),tr("Not able to write to socket"));
    17. else
    18. {
    19. statusLabel->setText(tr("Hi")+QString(QByteArray::number(x)));
    20.  
    21. }
    22. temp = QString(QString(QByteArray::number(y)));
    23. stream << temp;
    24. cout<<temp.toInt();
    25. if(tcpSocket->write(block)==-1)
    26. QMessageBox::critical(this,tr("Socket Warning"),tr("Not able to write to socket"));
    27. else
    28. {
    29. statusLabel->setText(tr("Hi")+QString(QByteArray::number(y)) );
    30.  
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    i need guidance on one more issue, Say multiple clients get connected to socket. How i will diffrentiate between the data which i receives from various sockets.
    i think above code can cater to multiple clients.

    cya
    quick nitin

  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: QTcpServer not reading as fast a QTcpSocket writing.

    Quote Originally Posted by quickNitin
    Hello everybody,
    i have a strange pbm here. I have QTcpServer Which do listening accepting new connections putting data on dialog level. I have fortune client type code which transfers the data to server.
    I have few problems here.Major problem is QTcpServer is not reading as fastly as client is writing.
    In what way it is not reading as fast as the client is writing? What happens? Are the client and the server running on different machines? Are the machines on a local network or do they communicate through the Internet?


    i need guidance on one more issue, Say multiple clients get connected to socket. How i will diffrentiate between the data which i receives from various sockets.
    i think above code can cater to multiple clients.
    When a client connects to the server, it is given a separate socket on the server side. So you can differenciate clients easily because they are using different sockets.

    This line of code does the job:
    Qt Code:
    1. tcpSocket=tcpServer->nextPendingConnection();
    To copy to clipboard, switch view to plain text mode 

    Each client will get its own "tcpSocket".

  3. #3
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTcpServer not reading as fast a QTcpSocket writing.

    They both are is on same machine.
    client program is writing to the socket every 1 second at regular intervals.
    Reading function/slot of server is connected to signal readyRead(). I expect as soon as client write to socket server should be able to detect its presence but it is not happening. Server is lagging behind around by two third of data client writes.

  4. #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: QTcpServer not reading as fast a QTcpSocket writing.

    Do you have more than one processor in your computer?

  5. #5
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTcpServer not reading as fast a QTcpSocket writing.

    NO it is a uniprocessor p4 machine.
    Als o itried something else. In above posted client code , i am writing twice simultaneously to the socket( line 15). I changed this. I made a string of both x and y and write it socket. Now this is working fine. But i still has not got reason for previous behaviour.
    Last edited by quickNitin; 4th July 2006 at 06:33. Reason: addition

  6. #6
    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: QTcpServer not reading as fast a QTcpSocket writing.

    Quote Originally Posted by quickNitin
    NO it is a uniprocessor p4 machine.
    So how do you expect two processes to be executed simoultaneously? There will always be delays between different processes, because they get different time shares of the CPU.

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

    quickNitin (4th July 2006)

  8. #7

    Default Re: QTcpServer not reading as fast a QTcpSocket writing.

    I have same symptoms when using QTcpSocket/QTcpServer for transfering data block (1-10 Mb).

    After some investigation i discovered that that it's not slow reading from qsocket, but slow writing to qsocket.

    I increased (very much) data trasfer just by implementing this write method :
    Qt Code:
    1. while(total < size)
    2. {
    3. n = write(cBuffer + total, MIN(WRITE_PACKET_MAX_SIZE, size - total));
    4. flush();
    5. Q_ASSERT(-1 == n || n > 0);
    6.  
    7. if (-1 == n)
    8. return false;
    9.  
    10. total += n;
    11. }
    To copy to clipboard, switch view to plain text mode 

    This code "split" data block into number of "small" blocks and sends one after another and flushes.

    It looks like there is some strange(unxpected for me) behaviour of QTcpSocket write functional. I think there is some buffering routines performed.
    Last edited by Polonius; 8th January 2007 at 10:29.

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.