PDA

View Full Version : My client can't send data



hiuao
8th February 2007, 08:50
hi,all.I want to realize the communication between the server and client,but I have tested that the client can't send data,although I have thought for quite a while,I couldn't find the reason,because I have studied to use it for a short time.The send function for client and receive function for server are as following:
void Client::sendFortune(){
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_2);
out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
}

void Server::readFortune(){
QDataStream in(tcpsocket);
in.setVersion(QDataStream::Qt_4_2);
if (blockSize == 0) {
if (tcpsocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpsocket->bytesAvailable() < blockSize)
return;
QString nextFortune;
in >> nextFortune;
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewConnect()));
return;
}
currentFortune = nextFortune;
}
Anyone who knows why,please tell me,thank you very much!

wysota
8th February 2007, 09:50
How do you know the data is not sent?

hiuao
8th February 2007, 11:19
Thank you for your reply!
In the client's send function I added
tcpSocket->write(block);
connect(tcpSocket,SIGNAL(bytesWritten(qint64)),thi s,SLOT(flag()));
....
void Client::flag(){
QMessageBox::information(this, tr("Fortune Client"),
tr("data has been sent"));
}
but there is no QMessageBox,and in the server's send function I added
tcpsocket = tcpServer->nextPendingConnection();
tcpsocket->write(block);
connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(re adTest()));
.....
void Server::readTest(){
statusLabel->setText("ready read");//statusLabel is a label on the window
}
there is also no "ready read".
Here in the loop,after the server feels connection,it sends data,in the send function's end it reveive data.For the client,when it receives data,in the receive function's end it sends data.Wether I have some misunderstanding TCP/IP protocal?Thank you very much!

wysota
8th February 2007, 11:26
Could you use some network sniffer to see if the data is sent and what data is sent? Is the connection open before you try to send data? What does the write() method return?

hiuao
9th February 2007, 07:31
Sorry,I don't know how to use network sniffer,here I attach the whole function of server and client.The client can receive data and show them on the window.

hiuao
9th February 2007, 08:17
Just now I clicked the getFortuneButton for more than 100 times,about more 40 times the client receives data,after that it shows "data has been sent" for another more than 40 times and during this time the client doesn't receive data,but the server still has no change .Then the client receives data ....
Why?What's the problem?I am headache now.Please help me.

wysota
9th February 2007, 08:49
Where do you connect in the client? I can't see the respective code...

hiuao
10th February 2007, 04:20
void Client::requestNewFortune(){
.....
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
it can receive the right data from server.

wysota
10th February 2007, 12:36
And how are your signals and slots set up? How do you react to a successful connection? Do you have an event loop running in the thread which owns the socket?

hiuao
11th February 2007, 05:27
I made some change again.
For the server:
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
in the sendFortune()
tcpsocket = tcpServer->nextPendingConnection();
connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(re adFortune()));
in the readFortune()
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
For the client:
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
in the readFortune()
if(tcpSocket->isWritable())
sendFortune();
in the sendFortune()
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
Wether I made a mistake?Please tell me,thank you!

hiuao
23rd February 2007, 09:32
I have found the problem but I can't understand.The data sent by server can be received by client,but the client send the some form of data ,the server can't receive.Then I let the client one data which I use actually,then the server can receive.
Why?Can anyone tell me?