PDA

View Full Version : Also i need more guidance on issue of differentiating data read from different socket



quickNitin
4th July 2006, 06:43
here is the code for server which will do reading from various clients.

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,SLO T(activatingNewConnection()));
}
}

MyDialog::~MyDialog()
{

}

void MyDialog::activatingNewConnection()
{
tcpSocket=tcpServer->nextPendingConnection();
connect(tcpSocket, SIGNAL(disconnected()),tcpSocket, SLOT(deleteLater()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(da taAvailable()));
}

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);
}

i agree for each connection a new socket will be created.


tcpSocket=tcpServer->nextPendingConnection();

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.

jpn
4th July 2006, 07:17
You can identify the socket which sent the signal by calling QObject::sender() (http://doc.trolltech.com/4.1/qobject.html#sender) in the slot. Just be aware that the sender is undefined if the slot is called as a normal C++ function.

quickNitin
4th July 2006, 09:36
thanks jpn,
i can get a refrence using QObject::sender() but i am still not able to catch the point.
What i will get i s a QObject *ptrof sender. I read the doc but i couldn't figure out what characterstic of it wil be useful to me.
situation is like this. I have multiple algo writing data to their socket . A server which listen to these socket on readyRead() signal read from the socket. Now till now my code was for one client only. Now i wan to make it for multiple clients. So i have to keep track of data send by which socket and store their history( previous send data) also. Thats why i am looking for diferentiation between received data.
ALso next issue come when i am making a list of list of received data. One list for each socket. How i will address or index them. Like i am creating a list whenever a new connection appears and append that to main list. But when next piece of data comes how i am going to decide which list i am going to use for storage. These are major issues form e currently.

Is the way i am addressing this problem correct. Seond method which i am avoiding is creation of a thread for each new connection. and when socket exit i copy all data at one place.

jpn
4th July 2006, 10:15
You should cast the QObject pointer to an appropriate type.
Eg.


QTcpSocket* socket = dynamic_cast<QTcpSocket*>(sender());
if (socket)
{
// do something
}


And how about something like this for the data storage:


QHash<int, HistoryData>
// where int is QTcpSocket::socketDescriptor()
// and HistoryData is any storage you decide to use, eg. QStringList