Re: using socket descriptor to identify the clients connected to the Threded server
Quote:
Originally Posted by
SwanseaLover
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.
Code:
QTcpSocket *clientConnection0
=clientConnections.
at(1);
Access to the second entry in list clientConnections
Quote:
Originally Posted by
SwanseaLover
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,
_
Re: using socket descriptor to identify the clients connected to the Threded server
it's clear now :D
:o
Many Thanks
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)
Re: using socket descriptor to identify the clients connected to the Threded server
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
Code:
clientConnections.insert(socketDescriptor, clientConnection);
Retrieval
Code:
QTcpSocket *socket
= clientConnections.
value(socketDescriptor
);
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,
_