Results 1 to 4 of 4

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

Threaded View

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

    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

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
  •  
Qt is a trademark of The Qt Company.