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
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);
}
void MyDialog::settingServer()
{
tcpServer=new QTcpServer(this);
if(!(tcpServer->listen(QHostAddress::Any,35000)))
{ cout<<"\n Tcp Server not listening";
QMessageBox::critical(this,tr("server"),tr(" not able to start"));
}
else
{
int x=tcpServer->serverPort();
label->setText(QString("Server is listening on port ")+QByteArray::number(x));
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()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if(tcpSocket->bytesAvailable() <= 0 )
{
//cout<<"\nNothing Available for reading";
return;
}
QString st;bool ok;
int i;
in >> st;
label->setText(st);
}
To copy to clipboard, switch view to plain text mode
important code for client is
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
{
}
}
void Client::sendingData(int x,int y)
{
QByteArray block;
QDataStream stream(&block,QIODevice::ReadWrite);
stream.setVersion(QDataStream::Qt_4_0);
QString temp;
temp = QString( QString(QByteArray::number(x)));
stream.device()->seek(0);
stream << temp;
cout<<"\n"<<temp.toInt()<<endl;
QString st="he;llo world";
cout<<st.data()<<endl;
if(tcpSocket->write(block)==-1)
QMessageBox::critical(this,tr("Socket Warning"),tr("Not able to write to socket"));
else
{
statusLabel->setText(tr("Hi")+QString(QByteArray::number(x)));
}
temp = QString(QString(QByteArray::number(y)));
stream << temp;
cout<<temp.toInt();
if(tcpSocket->write(block)==-1)
QMessageBox::critical(this,tr("Socket Warning"),tr("Not able to write to socket"));
else
{
statusLabel->setText(tr("Hi")+QString(QByteArray::number(y)) );
}
}
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
Bookmarks