2016-07-08 2 views
0

일부 서버 응용 프로그램에서 작업 중입니다.이 스켈레톤은 서브 클래스 QTcpServer이고 이름이 UeTcpServer입니다. 서버 응용 프로그램은 정상적으로 시작 내가 ueSlotTcpServerNewConnection()에 중단 점을 배치 할 때,새 연결시 QTcpServer :: hadPendingConnections()가 false를 반환합니다.

connect(this->ueTcpServer(), 
     SIGNAL(newConnection()), 
     this, 
     SLOT(ueSlotTcpServerNewConnection())); 

를 통해 UeTcpServernewConnectionSignal에 연결 한 후 Linux terminal를 사용하여 서버 응용 프로그램에 연결, 브레이크 포인트가 ueSlotTcpServerNewConnection() 슬롯 내부에 활성화되는 :

void UeMainWindow::ueSlotTcpServerNewConnection() 
{ 
    if(this->ueTcpServer()->hasPendingConnections()) 
    { 
     QTcpSocket* incomingSocket=this->ueTcpServer()->nextPendingConnection(); 

     this->ueSlotAddEventInfoLog(0, 
            tr("New incoming connection from host") 
            .append(" ") 
            //.append(this->ueTcpServer()->nextPendingConnection()->peerAddress().toString()) 
            .append(incomingSocket->peerName()) 
            .append(" ") 
            .append(tr("with IP address")) 
            .append(" ") 
            .append(incomingSocket->peerAddress().toString()) 
            .append(" ") 
            .append(tr("using port")) 
            .append(" ") 
            //.append(this->ueTcpServer()->nextPendingConnection()->peerPort())); 
            .append(QString::number(incomingSocket->peerPort()))); 
    } // if 
} // ueSlotTcpServerNewConnection 

그러나 this->ueTcpServer()->hasPendingConnection()false을 반환합니다. 왜? 또한 docs에 명시된대로

+1

내 자신의'QTcpServer' 사용법을 보면서'while (QTcpSocket * conn = server-> nextPendingConnection()) {'을 사용하는 데 문제가 없었습니다. 그래서 내 질문은 ...'hasPendingConnections'가 false를 반환하면'nextPendingConnection'은 NULL/nullptr을 리턴합니까? 아니면 실제로 유효한 연결을 리턴합니까? –

+0

@ G.M. 'if (this-> ueTcpServer() -> hasPendingConnections())'를 제거/주석하면'QTcpSocket'에'NULL' 포인터가 생깁니다. – KernelPanic

+1

응용 프로그램의 QTcpServer 하위 클래스에서 incomingConnection을 다시 구현 했습니까? 그렇다면 addPendingConnection을 호출하는 것을 잊어 버릴 수도 있습니다. –

답변

0

나는 incomingConnection(qintptr socketDescriptor)에서 addPendingConnection(QTcpSocket *socket 전화를 깜빡했습니다

void UeTcpServer::incomingConnection(qintptr socketDescriptor) 
{ 
    QTcpSocket* newSocket=new QTcpSocket(this); 

    newSocket->setSocketDescriptor(socketDescriptor); 

    this->addPendingConnection(newSocket); 
} // incomingConnection 

을 그리고 그것은 작동합니다.

+2

당신이하고 싶은 일이 있다면'incomingConnection'을 오버라이드 할 필요가 없습니다. 이것은 정확히 [base implementation] (https://code.qt.io/cgit/qt/qtbase.git/tree)입니다. /src/network/socket/qtcpserver.cpp#n579) 않습니다. – Mike

+1

함수 매개 변수를 사용하는 경우 왜'Q_UNUSED'입니까? – peppe

+0

@peppe 실수! – KernelPanic