Results 1 to 2 of 2

Thread: problem with client/server application

  1. #1
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default problem with client/server application

    hi there
    i am designing a C/S application. The client is supposed to query the server sending some data (basically just one opcode which describes what the server has do. if the server reply 'busy', the client will retry for a certain number of times, if the server reply available, the server will wait for a message containing the status of the task the server have been asked for

    since the fortune client/server example did not seem suited fort this (the cleint just connect to the server but does not transmit anything), i am doing something based on this example

    http://doc.qt.nokia.com/4.7-snapshot...ialog-cpp.html

    The server connects immediately as soon as the constructor is invoked and the newConncection signal is connected to the acceptConnection() slot (see dialog example
    Qt Code:
    1. tcpServer = new QTcpServer(this);
    2. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
    3. ....
    4. void ecmqMfgServer::acceptConnection()
    5. {
    6. tcpServerConnection = tcpServer->nextPendingConnection();
    7. connect(tcpServerConnection, SIGNAL(readyRead()), this, SLOT(readCli2SvrMsg()));
    8. connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    9. tcpServer->close(); // should i close EVERY time
    10. }
    To copy to clipboard, switch view to plain text mode 
    the acceptConnection code just connect the readyRead signal to my 'read' method. First question is: why the tcpServer must be 'closed' here ?
    then, after getting an available ipAddress and port, i ask the server to listen and notify the outcome
    Qt Code:
    1. if (!tcpServer->listen(HostAddress, this->ipPort) &&
    2. !tcpServer->listen(HostAddress, this->ipPort))
    3. ...
    To copy to clipboard, switch view to plain text mode 
    finally my read method
    Qt Code:
    1. void ecmqMfgServer::readCli2SvrMsg()
    2. {
    3. qint16 bytesAvail;
    4. bytesAvail= tcpServerConnection->bytesAvailable();
    5. const QString msg = tr("bytes received : ")
    6. + QString("%5").arg(bytesAvail)
    7. + tr(" on ") + QDateTime::currentDateTime().toString(Qt::ISODate);
    8. ui.plainTextEditLog->appendPlainText(msg);
    9.  
    10. QByteArray block;
    11. QDataStream in(&block, QIODevice::ReadOnly);;
    12. in.setVersion(QDataStream::Qt_4_1);
    13. block.resize(bytesAvail);
    14. in>>block;
    15.  
    16. return;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I then experience the following strange behavior:
    - the first time my client send 2 bytes to the server, i can see 2 received bytes in my output
    - if the client attempt to send again the same bytes nothing is reported as 'received'

    i am currently debugging this issue but cannot get rid of this. i feel there is something basically wrong in this approach

    any help is appreciated (have mercy on the newbie, please)

  2. #2
    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: problem with client/server application

    Quote Originally Posted by marco.stanzani View Post
    First question is: why the tcpServer must be 'closed' here ?
    In the loopback example, I guess this is to limit the server to connect to only one client.

    Note that all communication between the client and the server is not done via the server itself, it is always done via a client to client connection. So you can stop the server from listening at any time, it will not stop the client communication. It only prevents more clients from connecting.

    In other words, if it is not your goal to limit the amount of client connections, you can leave out closing the server.
    There are other methods to limit the amount of clients without closing the server.
    And, if you plan to have a very busy server (meaning: lots and lots of connections) you do want to have some safeguards implemented.

    Qt Code:
    1. if (!tcpServer->listen(HostAddress, this->ipPort) &&
    2. !tcpServer->listen(HostAddress, this->ipPort))
    To copy to clipboard, switch view to plain text mode 
    That doesn't seem right.

    - if the client attempt to send again the same bytes nothing is reported as 'received'
    It might also be a problem with your client.

    But...

    This code:
    Qt Code:
    1. QByteArray block;
    2. QDataStream in(&block, QIODevice::ReadOnly);;
    3. in.setVersion(QDataStream::Qt_4_1);
    4. block.resize(bytesAvail);
    5. in>>block;
    To copy to clipboard, switch view to plain text mode 
    It does not read anything from the tcpServerConnection object. Where do you actually use it?

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

    marco.stanzani (9th April 2011)

Similar Threads

  1. TCP server-client application with external access
    By dominate in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2011, 19:18
  2. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 16:32
  3. TCP Client-Server Application
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 13:20
  4. Client-Server Application design suggestion
    By berzeck in forum Qt Programming
    Replies: 6
    Last Post: 17th December 2007, 18:13

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.