Unsolved How to send message to all client?
-
Hey, I'm new member here.
I want to create Chat Application with Qt in C++. My client application is done but my tcp server is not working very well! I can't send message to diferent client. You can help me?
Here is my TCP server
#include "myserver.h" #include "mythread.h" MyServer::MyServer(QObject *parent): QTcpServer(parent){ qDebug() << "Server -> Invited Constructor"; ClientList.clear(); } MyServer::~MyServer(){ qDebug() << "Server -> Invited Destructor"; } void MyServer::StartServer(){ if(!this->listen(QHostAddress::Any,SERVER_PORT)){ qDebug() << QObject::tr("Unable to start the server: %1.").arg(tcpServer->errorString()); //this->deleteLater(); return; }else{ qDebug() << QString::fromUtf8("Server is running"); } } void MyServer::incomingConnection(qintptr socketDescriptor) { printf("MyServer::incomingConnection\n"); qDebug() << socketDescriptor << " Thread is Connecting..."; MyThread *thread = new MyThread(socketDescriptor,SERVER_PORT, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }
Here is my Thread:
#include "mythread.h" MyThread::MyThread(qintptr ID,quint16 port, QObject * parent) : QThread(parent) { this->socketDescriptor = ID; SERVER_PORT = port; } void MyThread::run(){ //thread starts here qDebug()<< socketDescriptor << "Starting thread on port: "<<SERVER_PORT; tcpServer = new QTcpServer(this); socket = new QTcpSocket(this); // set the ID if(!socket->setSocketDescriptor(this->socketDescriptor)){ emit error(socket->error()); return; } connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection); connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection); qDebug()<< socketDescriptor <<"Client Connected"; exec(); } void MyThread::readyRead(){ QByteArray Data = socket->readAll(); qDebug() << socketDescriptor <<"Data in : "<< Data; socket ->write(Data); //its work, but i want to send for all connected client! } void MyThread::disconnected(){ qDebug() << socketDescriptor << "Disconnected: "; socket->deleteLater(); exit(0); }
My solution:
myserver.cpp
void MyServer::incomingConnection(int socketDescription){ qDebug() << socketDescription << "Connecting"; qDebug() <<this->socketDescriptor() << "this is the socket Descriptor"; //new client add to QList QTcpSocket *socket = this->nextPendingConnection(); ClientList.append(socket); qDebug() << socket << "This will be added to list"; ClientList.append(socket); MyThread * thread = new MyThread(socketDescription,ClientList,this); connect(thread, SIGNAL(finished()),thread,SLOT(deleLater())); thread ->start(); }
mythread.cpp
void MyThread::readyRead(){ QByteArray Data = socket->readAll(); qDebug() << socketDescriptor <<"Data in : "<< Data; //socket ->write(Data); foreach(QTcpSocket *clientConnection, threadClientList) { clientConnection->write(Data); qDebug() <<"MyThred -> ClientConnection: " << clientConnection; } }
but this solution dont work!
sorry for bad language,
Krizbi -
Don't use threads at all, remember all socket connections in a container and iterate over this list when you want to send to all clients.
-
you can help me with some code?
-
You could have a look at the server part of: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application#The_Single_Thread_Server especially the function
ChatServer::broadcast()
which does what you want to do.Regards