Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: using socket descriptor to identify the clients connected to the Threded server

  1. #21
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    Quote Originally Posted by SwanseaLover View Post
    i have tried .at() as following but i got an error:

    QTcpSocket *clientConnection = new QTcpSocket;
    clientConnection->setSocketDescriptor(socketDescriptor);
    clientConnections.append(clientConnection);
    QTcpSocket clientConnection0 =clientConnections.at(1);

    error:24: error: conversion from ‘QTcpSocket* const’ to non-scalar type ‘QTcpSocket’ requested
    The list contains QTcpSocket* elements, you try to assign to a QTcpSocket variable.

    Qt Code:
    1. QTcpSocket *clientConnection0 =clientConnections.at(1);
    To copy to clipboard, switch view to plain text mode 
    Access to the second entry in list clientConnections


    Quote Originally Posted by SwanseaLover View Post
    i tried

    clientConnections.at(0)->write("hello");
    but i got error index out of range
    Meaning the list is empty.
    Check the index you attempt to use against QList::count() to see it it is valid.

    The index you are using has to be >= 0 and < QList::count()

    Cheers,
    _

  2. The following user says thank you to anda_skoa for this useful post:

    SwanseaLover (16th April 2013)

  3. #22
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    it's clear now

    Many Thanks

  4. #23
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    hi again
    i'm trying to access clientConnections according to socketDescriptor insted of index inorder to use write();
    i was trying to comper the valuse inserted in the list according to socketDescriptor obtained

    so when server accepted a new connection , i debuge socket discrptor


    void Server::incomingConnection(int socketDescriptor)
    {

    QTcpSocket *clientConnection = new QTcpSocket;
    clientConnection->setSocketDescriptor(socketDescriptor);
    clientConnections.append(clientConnection);

    qDebug()<<"socket id :"<<socketDescriptor; ......out put 23 for example

    }


    so when compering latter :
    Server server;
    QTcpSocket *checkSocket=new QTcpSocket();
    checkSocket->setSocketDescriptor(23)
    qDebug()<<server.clientConnections.value(0)<<check Socket; ....it is first connection at index 0

    out put:
    they are difffernt
    QTcpSocket(0x8972438) QTcpSocket(0x896d0e8)

  5. #24
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    Qt Code:
    1. QTcpSocket *checkSocket=new QTcpSocket();
    To copy to clipboard, switch view to plain text mode 
    You create a new QTcpSocket instance, of course its pointer will be different.

    If you want to access the socket by socket descriptor, then better us a QHash instead of a list, using the socket descriptor as the key

    Qt Code:
    1. QHash<int, QTcpSocket*> clientConnections;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. clientConnections.insert(socketDescriptor, clientConnection);
    To copy to clipboard, switch view to plain text mode 

    Retrieval
    Qt Code:
    1. QTcpSocket *socket = clientConnections.value(socketDescriptor);
    To copy to clipboard, switch view to plain text mode 

    Any reason you want to use the socketDescriptor as the identifier? Where do you store that that you cannot store the pointer to the QTcpSocket directly?

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 30th November 2010, 21:09
  2. Replies: 7
    Last Post: 10th May 2010, 11:26
  3. Socket Descriptor
    By ManuMies in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 09:42
  4. remote port of a socket that hasn't connected yet
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2008, 17:24
  5. Staying connected to a MySQL server
    By fnmblot in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 10:39

Tags for this Thread

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.