#### Archived

This topic is now archived and is closed to further replies.

# Network programming problem with Qt

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm currently working on a small server ( C++, TrollTech's Qt-library ) application for a software project at my school and I ran into a little problem that I need some help with, so if any of you guys could make things a little clearer for me, that 'd be really great. So here's a description of my problem. Well, as I said I'm writing a server. The server runs several threads. One thread that takes care of all network operations, one that processes requests, and one that provides a minor GUI that allows a user to monitor and control the server. When a client sends a request to my server, my net communingcation thread reads the data and enqueues a pointer to the data in Queue A. My processing thread dequeues any pointers in Queue A, does whatever needs to be done and enqueues a pointer to the result data in Queue B. The netcom thread dequeues any pointers in Queue B and sends the results back to the client(s) who had sent the request(s). So far so good. Now the details. My netcom thread is derived from Qt's QServerSocket and QThread classes, like so ...
class NetcomThread : public QServerSocket, public QThread
{
Q_OBJECT

private:
QSocket* mv_pSocket;

/* further relevant internal data: pointers to the queues etc etc */

...
private:
// virtual method inherited from QServerSocket; gets called each time a new client contacts the server

virtual void newConnection( int sockNr );

Currently my design is as follows. Whenever a client contacts my server, newConnection(...) is invoked and creates a new QSocket object assigning it to my mv_pSocket pointer. I then read the data the client has sent, let my CalcThread do its job and then have my NetcomThread write the results back to the client. All the while the connection between the server and the client remains open. Then after the result data has been sent back, I somehow need to discard the connection. Several Qt-tutorials I've seen suggested that it's best to delete the QSocket object associated with the particular connection and that this should be triggered via a connectionClosed() or delayedCloseFinished() signal. My first problem is that apparently neither of these signals are ever sent (I've made sure I actually connected the signals to the corresponding slots!). Should I just manually invoke close() on my QSocket object and then delete it? The second thing is, why does closing a connection equate to deleting a socket anyway? Shouldn't it be adequate to just invoke close() on the QSocket object and then setSocket( int sockNr ) the next time a new client connects to the server? I'm obviously new to network programming and I haven't really quite figured out the intricacies of modern networking yet, so be sure to put your answer in as simple words as you can manage. Thanks in advance for any helpful replies. [edited by - Red Ant on November 27, 2003 11:19:55 AM]