2014-07-23 3 views
0

QTcpSocket과 QTcpServer가 함께 작동하는 방식을 이해하려고합니다. 그래서 로컬 호스트에서 서버와 클라이언트 소켓을 시작하는이 간단한 예를 썼다 :QTcpSocket/QTcpServer가 함께 작동하지 않음

QTcpServer server; 
qDebug() << "Listen: " << server.listen(QHostAddress::Any, 10590); 

usleep(500000); //1/2 sec 

QTcpSocket client; 
client.connectToHost(QHostAddress("127.0.0.1"), 10590); 

usleep(5000000); 
qDebug() << "Client socket available: " << client.isValid(); 
qDebug() << "Pending connections:" << server.hasPendingConnections(); 

을 그리고 난이 출력을 가지고 : 보류중인 연결이없는 이유

Listen: true 
Client socket available: true 
Pending connections false 

를?

PS> SLOT/SIGNAL 메커니즘을 사용하고 싶지 않습니다.

답변

1
int main() 
{ 
    QTcpServer server; 
    qDebug() << "Listen: " << server.listen(QHostAddress::Any, 10590); 

    usleep(5000000); 

    QTcpSocket client; 
    client.connectToHost(QHostAddress("127.0.0.1"), 10590); 

    usleep(5000000); 

    qDebug() << "Client socket connected: " << (client.state() == QTcpSocket::ConnectedState); 
    qDebug() << "Pending connections:" << server.hasPendingConnections(); 
} 

출력 :

Listen: true 
Client socket connected: false 
Pending connections: false 

QTcpServerQTcpSocket 쿼리에 응답 할 수 있기 때문입니다 ... 모두가 동일한 스레드에 있으며 QTcpSocket이 실행되는 경우, QTcpServer가 유휴 상태에 있습니다.

멀티 스레드 앱에서 사용해보세요.

+0

감사합니다. @eferion, 나는 그들이 한 스레드에 살 수 없다는 것을 몰랐습니다. –