I want to use QTcpServer (I think) to create QTcpsocket each time a client connects.
I want the connection to stay open until the client side disconnects. Then I want the socket to be closed and associated memory freed.
So, I was thinking something like this for the newConnection slot of QTcpServer:
oid EjsServer::newConnection()
{
QTcpSocket *socket
= server
->nextPendingConnection
();
connect(socket,SIGNAL(disconnected()),socket,SLOT(deleteLater()));
clientList.append(socket);
socket->write("Connected to APES EJS Server\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
//socket->close();
}
oid EjsServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
connect(socket,SIGNAL(disconnected()),socket,SLOT(deleteLater()));
clientList.append(socket);
socket->write("Connected to APES EJS Server\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
//socket->close();
}
To copy to clipboard, switch view to plain text mode
But, I think once newConnection finishes, "socket" will go out of scope. But will the memory it's pointing still be good so it's ok to have it as an argument to the deleteLater() slot?
I also need to remove the socket from "clientList, so instead of just connecting to the socket's "deleteLater", I'm thinking I need a slot in EjsServer that removes the socket from the list then calls deleteLater. But, how to get a handle to the right socket? None of the QTcpSocket signals include a pointer to the QTcpSocket the emitted the signal.
I guess I need a good QTcpServer example. The fortune example isn't helpful, since it just writes to a client on connection, then closes the connection. Or, implement without using QTcpserver.
Thanks
Added after 21 minutes:
I could use QObject::sender() in the slot to remove the right QTcpSocket * from the list and invoke deleteLater(), but it "feels like" i'm not using the class as intended.
Bookmarks