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
Code:
void MyDialog::settingServer()
{
{ cout<<"\n Tcp Server not listening";
QMessageBox::critical(this,tr
("server"),tr
(" not able to start"));
}
else
{
int x=tcpServer->serverPort();
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(activatingNewConnection()));
}
}
MyDialog::~MyDialog()
{
}
void MyDialog::activatingNewConnection()
{
tcpSocket=tcpServer->nextPendingConnection();
connect(tcpSocket, SIGNAL(disconnected()),tcpSocket, SLOT(deleteLater()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataAvailable()));
}
void MyDialog::dataAvailable()
{
if(tcpSocket->bytesAvailable() <= 0 )
{
//cout<<"\nNothing Available for reading";
return;
}
int i;
in >> st;
label->setText(st);
}
important code for client is
Code:
void Client::sendingData(int x,int y)
{
stream.device()->seek(0);
stream << temp;
cout<<"\n"<<temp.toInt()<<endl;
cout<<st.data()<<endl;
if(tcpSocket->write(block)==-1)
QMessageBox::critical(this,tr
("Socket Warning"),tr
("Not able to write to socket"));
else
{
}
stream << temp;
cout<<temp.toInt();
if(tcpSocket->write(block)==-1)
QMessageBox::critical(this,tr
("Socket Warning"),tr
("Not able to write to socket"));
else
{
}
}
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
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?
Quote:
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:
Code:
tcpSocket=tcpServer->nextPendingConnection();
Each client will get its own "tcpSocket".
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.
Re: QTcpServer not reading as fast a QTcpSocket writing.
Do you have more than one processor in your computer?
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.
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.
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 :
Code:
while(total < size)
{
n = write(cBuffer + total, MIN(WRITE_PACKET_MAX_SIZE, size - total));
flush();
Q_ASSERT(-1 == n || n > 0);
if (-1 == n)
return false;
total += n;
}
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.