Results 1 to 4 of 4

Thread: Also i need more guidance on issue of differentiating data read from different socket

  1. #1
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Also i need more guidance on issue of differentiating data read from different socket

    here is the code for server which will do reading from various clients.
    Qt Code:
    1. void MyDialog::settingServer()
    2. {
    3. tcpServer=new QTcpServer(this);
    4. if(!(tcpServer->listen(QHostAddress::Any,35000)))
    5. {
    6. cout<<"\n Tcp Server not listening";
    7. QMessageBox::critical(this,tr("server"),tr(" not able to start"));
    8. }
    9. else
    10. {
    11. int x=tcpServer->serverPort();
    12. label->setText(QString("Server is listening on port ")+QByteArray::number(x));
    13. connect(tcpServer,SIGNAL(newConnection()),this,SLOT(activatingNewConnection()));
    14. }
    15. }
    16.  
    17. MyDialog::~MyDialog()
    18. {
    19.  
    20. }
    21.  
    22. void MyDialog::activatingNewConnection()
    23. {
    24. tcpSocket=tcpServer->nextPendingConnection();
    25. connect(tcpSocket, SIGNAL(disconnected()),tcpSocket, SLOT(deleteLater()));
    26. connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataAvailable()));
    27. }
    28.  
    29. void MyDialog::dataAvailable()
    30. {
    31. QDataStream in(tcpSocket);
    32.  
    33. in.setVersion(QDataStream::Qt_4_0);
    34. if(tcpSocket->bytesAvailable() <= 0 )
    35. {
    36. //cout<<"\nNothing Available for reading";
    37. return;
    38. }
    39. QString st;bool ok;
    40. int i;
    41. in >> st;
    42. label->setText(st);
    43. }
    To copy to clipboard, switch view to plain text mode 

    i agree for each connection a new socket will be created.
    Qt Code:
    1. 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.
    Last edited by jacek; 3rd August 2006 at 22:49. Reason: fixed code formatting

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Also i need more guidance on issue of differentiating data read from different so

    You can identify the socket which sent the signal by calling QObject::sender() in the slot. Just be aware that the sender is undefined if the slot is called as a normal C++ function.
    J-P Nurmi

  3. #3
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Also i need more guidance on issue of differentiating data read from different so

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Also i need more guidance on issue of differentiating data read from different so

    You should cast the QObject pointer to an appropriate type.
    Eg.
    Qt Code:
    1. QTcpSocket* socket = dynamic_cast<QTcpSocket*>(sender());
    2. if (socket)
    3. {
    4. // do something
    5. }
    To copy to clipboard, switch view to plain text mode 

    And how about something like this for the data storage:
    Qt Code:
    1. QHash<int, HistoryData>
    2. // where int is QTcpSocket::socketDescriptor()
    3. // and HistoryData is any storage you decide to use, eg. QStringList
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    quickNitin (4th July 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.