c++ - Best way to handle multiple sockets in Qt server application? -
i'm making simple client server program school project in qt, i've run problems on how handle multiple new connections server. code have new connections far pretty basic:
void loginserver::newconnection() { logsock = server->nextpendingconnection(); if(!logsock->waitforconnected(500)) { qdebug() << "connection failed"; }else { qdebug() << "it worked!"; connect(logsock, signal(readyread()),this, slot(readdata())); } }
one method saw on internet pushing new connections data structure such vector, , handling them 1 container. 1 issue have if connect different sockets same slot reading data, there elegant way tell socket sent signal?
another way considered doing when readyread() emitted, search through list of sockets see had bytesavailable, seems overly simplistic solution me. plus, if 2 sockets sent data @ same time program might identify wrong socket.
another option considered overriding readyread() include reference in signal, , realized don't have first idea how that.
connect(logsock, signal(readyread(qtcpsocket*)),this, slot(readdata(qtcpsocket*)));
any advice appreciated.
you can identify socket, emitted readyread() signal using following code in slot:
qtcpsocket* socket = qobject_cast< qtcpsocket* >(sender());
...and read data socket->readall()
. sender()
returns pointer qobject, emit signal.
Comments
Post a Comment