2010-05-12 2 views
2

QT에서 간단한 서버 스레드를 만들어 연결을 허용하려고합니다. 그러나 서버가 수신 중이지만 (테스트 응용 프로그램과 연결할 수는 있지만) newConnection() 신호가 작동되도록합니다.QTcpServer에서 newConnection() 신호를 수신 할 수 없습니다.

여기에 누락 된 부분에 대한 도움은 매우 감사하겠습니다. 모든


class CServerThread : public QThread 
{ 
    Q_OBJECT 

protected: 
    void run(); 

private: 
    QTcpServer* server; 

public slots: 
    void AcceptConnection(); 
}; 


void CServerThread::run() 
{ 
    server = new QTcpServer; 

    QObject::connect(server, SIGNAL(newConnection()), this, SLOT(AcceptConnection())); 

    server->listen(QHostAddress::Any, 1000); // Any port in a storm 

    exec(); // Start event loop 
} 


void CServerThread::AcceptConnection() 
{ 
    OutputDebugStringA("\n***** INCOMING CONNECTION"); // This is never called! 
} 

답변

2

먼저 나는 CServerThread 인스턴스 (이 인스턴스가 작성된 스레드에서) 다른 스레드에 살고있는 동안 서버가 새 스레드에 살고 있다고 말할 수있다. 작성중인 신호/슬롯 연결은 inderect이며 두 개의 다른 스레드의 이벤트 루프간에 스레드 저장 이벤트 전달을 사용합니다. 실제로 CServerThread를 생성하는 스레드에 Qt 이벤트 루프가 실행되지 않는 경우 이러한 문제가 발생할 수 있습니다.

QTcpServer를 생성하고 수신 대기하는 일부 MyServer 클래스를 생성하고 QTcpServer :: newConnection() 신호를 자체 슬롯에 연결하는 것이 좋습니다. 그리고 이런 일에 서버 스레드 실행 방법을 재 작성 :이 방법에서

void CServerThread::run() { 
    server = new MyServer(host,port); 
    exec(); // Start event loop 
} 

을 동일한 스레드에서 QTcpServer 및 newConnection 처리 객체의 삶 모두. 이러한 상황은 다루기가 더 쉽습니다.

내가 하나 개 정말 간단한 동작하는 예제가 있습니다

헤더 : http://qremotesignal.googlecode.com/svn/tags/1.0.0/doc/html/hello_2server_2server_8h-example.html

출처 : http://qremotesignal.googlecode.com/svn/tags/1.0.0/doc/html/hello_2server_2server_8cpp-example.html

+0

감사합니다 아주 많이, 내가 제안이 재 작업 것이다. – Bob

관련 문제