PDA

View Full Version : problem with client/server application



marco.stanzani
8th April 2011, 16:47
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/network-loopback-dialog-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


tcpServer = new QTcpServer(this);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
....
void ecmqMfgServer::acceptConnection()
{
tcpServerConnection = tcpServer->nextPendingConnection();
connect(tcpServerConnection, SIGNAL(readyRead()), this, SLOT(readCli2SvrMsg()));
connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
tcpServer->close(); // should i close EVERY time
}

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


if (!tcpServer->listen(HostAddress, this->ipPort) &&
!tcpServer->listen(HostAddress, this->ipPort))
...

finally my read method


void ecmqMfgServer::readCli2SvrMsg()
{
qint16 bytesAvail;
bytesAvail= tcpServerConnection->bytesAvailable();
const QString msg = tr("bytes received : ")
+ QString("%5").arg(bytesAvail)
+ tr(" on ") + QDateTime::currentDateTime().toString(Qt::ISODate) ;
ui.plainTextEditLog->appendPlainText(msg);

QByteArray block;
QDataStream in(&block, QIODevice::ReadOnly);;
in.setVersion(QDataStream::Qt_4_1);
block.resize(bytesAvail);
in>>block;

return;
}


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)

tbscope
9th April 2011, 07:36
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.




if (!tcpServer->listen(HostAddress, this->ipPort) &&
!tcpServer->listen(HostAddress, this->ipPort))



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:

QByteArray block;
QDataStream in(&block, QIODevice::ReadOnly);;
in.setVersion(QDataStream::Qt_4_1);
block.resize(bytesAvail);
in>>block;
It does not read anything from the tcpServerConnection object. Where do you actually use it?