2012-08-22 4 views
1

내가이 일을 제대로 수행하고 있다고 말할 수 있습니까?TCP 서버 구현

Qt를 사용하여 QTcpServer 클래스에서 상속하여 TCP 서버를 구현하고 있습니다. 들어오는 연결에서 새 스레드, 새 Worker 개체를 만들고 개체를 새 스레드로 이동하고 스레드를 시작합니다. 여기에서 서버는 새 클라이언트를 계속 수신하고 각 스레드는 Worker 객체에 대한 run 메소드에 있습니다.

이제 노래를 재생할 때 1 초 간격으로 각 클라이언트에 업데이트를 보내야하므로 타이머를 만듭니다. readyRead 슬롯에서 readAll을 사용하여 데이터를 읽은 다음 몇 가지 작업을 수행하고 응답을 보냅니다.

그러나 실행 방법으로 돌아 가면 클라이언트에 응답없이 노래 데이터 업데이트를 계속 보내야합니다. 이 모든 것이 잠시 (참) 루프에 들어가야하고 타이머를 시작하고 중지하기 위해 몇 가지 부울을 확인해야합니까? 보내야 할 트랙 정보는 노래 진행 시간입니다.

제 질문은 이렇게 할 수 있을까요? 그것은 조금 복잡해 보이지만 다시 한번 당신을위한 동시성입니다. 기본적으로 일부 조건이 충족되면 클라이언트에 반복적으로 데이터를 보내려면 TCP 서버가 필요합니다. 타이머를 시작하고 멈출 때를 확인하는 무한 루프는 쓸데없는 일이라고 생각합니다.

게시 코드가 명확 해 지겠습니까?

+1

을, 나는 TCP 업로드 서버에 한 어떤 클라이언트가 파일을 업로드 할 수 있습니다. 나는 QTcpServer와 많은 QTCPSocket을 가지고있다. QTCPSocket에서 파생 된 클래스를 만들고 일부 데이터 수신 함수가 있습니다. 서버가 들어오는 연결을 받으면 스레드 ID 또는 소켓 ID가있는 QTCPSocket에 대한 새 스레드를 만듭니다. 서버와 소켓은 신호와 슬롯으로 연결됩니다. –

답변

0

이 질문은 매우 오래된 질문이지만, 여전히 도움이 될 수 있습니다.

Qt는 스레드에 대해

:
많은 사람들이 당신이 모든 작업에 다른 스레드에 필요한 .NET처럼 Qt는 병렬 처리에 대해 생각, QT이 필요하지 않습니다! 당신은 내가 당신 같은 차단 작업을하지 않아도 제대로 이해가 있으면 큰 일을 계산 또는 SQLServer에

에서 대답을 syncron을 기다리고 같은 코드를 차단 한 경우
은 QT에서 당신은 단지 스레드가 필요합니다.
그래서 내가 상속하지 않고 잘하면 문제를 해결, (물론 주요 eventloop 스레드 제외) 단일 스레드없이 아주 작은 tcpserver는 프로그래밍 등 도움이있다 : 일단

#include <QObject> 
#include <QSet> 
#include <QTcpServer> 
#include <QTcpSocket> 
#include <QTimer> 

class TcpServer : public QObject 
{ 
    Q_OBJECT 
    public: 
     TcpServer() 
     { 
      // handle new connections 
      this->connect(&this->serverTcp, &QTcpServer::newConnection, this, &TcpServer::handleClientConnect); 

      // init client refresh timer 
      this->timer.setInterval(1000); 
      this->connect(&this->timer, &QTimer::timeout, this, &TcpServer::handleClientUpdates); 
      this->timer.start(); 
     } 

     bool startListen(qint16 port) 
     { 
      return this->serverTcp.listen(QHostAddress::Any, port); 
     } 

    private slots: 
     void handleClientConnect() 
     { 
      QTcpSocket* socketClient = *this->setConnectedClients.insert(this->serverTcp.nextPendingConnection()); 
      this->connect(socketClient, &QTcpSocket::disconnected, this, &TcpServer::handleClientDisconnect); 
      this->connect(socketClient, &QTcpSocket::readyRead, this, &TcpServer::handleClientData); 
     } 

     void handleClientDisconnect() 
     { 
      this->setConnectedClients.remove((QTcpSocket*)this->sender()); 
     } 

     void handleClientData() 
     { 
      QTcpSocket* socketSender = (QTcpSocket*)this->sender(); 
      // handle here the data sent by the client 
     } 

     void handleClientUpdates() 
     { 
      // construct here your update data 
      QByteArray baUpdateResponse = "test"; 

      // send update data to all connected clients 
      foreach(QTcpSocket* socketClient, this->setConnectedClients) { 
       socketClient->write(baUpdateResponse); 
      } 
     } 

    private: 
     QTcpServer serverTcp; 
     QTimer timer; 
     QSet<QTcpSocket*> setConnectedClients; 
};