class NetworkServer : public QObject{ Q_OBJECT...private:... QTcpServer *server; QList<QTcpSocket*>connections;private: void sendFileToClient(int socketIndex);public slots: void on_newConnection(); void on_recieveCommand();};
void NetworkServer::on_newConnection(){ QTcpSocket *socket = server->nextPendingConnection(); connect(socket, SIGNAL(readyRead()), this, SLOT(on_recieveCommand())); connections.push_back(socket); emit newConnection(socket->peerAddress().toString());}
void NetworkServer::on_recieveCommand(){ QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender()); if (socket == 0) return; sendFileToClient(connections.indexOf(socket));}void NetworkServer::sendFileToClient(int socketIndex){ QTcpSocket *sock = connections[socketIndex]; QByteArray outBuf; QDataStream out(&outBuf, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_6); QFile file(m_SendingFileName); file.open(QIODevice::ReadOnly); quint32 fileSize = file.size(); out<<fileSize; out<<file.readAll(); sock->write(outBuf); sock->flush(); sock->close(); connections.removeAt(socketIndex);}
C++ (Qt)#include <QCoreApplication>#include <QThread> class Thread : public QThread{public: explicit Thread() : QThread( 0 ) { setStackSize( 64000 ); } protected: virtual void run() { for(;;) sleep( 1 ); } }; int main( int argc, char **argv ){ QCoreApplication app( argc, argv ); for( int i = 0; i < 10000; ++i ) { Thread *th = new Thread(); th->start(); } return app.exec();}