here is the code for server which will do reading from various clients.
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
i agree for each connection a new socket will be created.
tcpSocket=tcpServer->nextPendingConnection();
tcpSocket=tcpServer->nextPendingConnection();
To copy to clipboard, switch view to plain text mode
But i couldn't figure out how i will address to different sockets in my code. How i will recognise them individually. tcpSocket will point only to newly connected socket.
Bookmarks