2012-11-02 2 views
0

Qt 생성자를 사용하여 원격 데스크톱에 액세스하기위한 응용 프로그램을 디자인하고 있습니다. 내 목적을 완료 한 후 원격 데스크톱에서 "Quit"신호를 얻으려면 Tcpserver와 Tcpsocket을 사용하고 있습니다. 내 PC는 원격 PC가 클라이언트 역할을하는 동안 서버 역할을합니다. 다음 개념을 사용하고 있습니다.몇 번의 연결 성공 후 QTcpSocket에서 "Connection Refused Error"메시지를 표시합니까?

서버 pc 1. PushButton을 눌러 원격 화면에 액세스하십시오 (tightvnc를 사용하여 전체 화면 모드에서). 2. 서버를 시작하고 활성 연결을 수신합니다 (포트 9876을 사용 중입니다). 3. 활성 연결을 찾았습니다. 클라이언트에 연결되었습니다. 4. 원격 액세스를 닫습니다. 5. 로컬 화면으로 다시 전환하십시오. 6. 서버 닫기

클라이언트 Pc 1. 종료 버튼을 눌러 원격 액세스를 닫습니다. 2. 종료 버튼을 눌렀을 때 3. 호스트에 연결하십시오. 4. "Quit"를 서버에 보내십시오. 5. 호스트와의 연결을 끊으십시오. 6. 연결을 닫으십시오.

그것은 몇 가지 시도를 잘 작동이

하지만 거기는 "연결이 오류를 거부"오류를주고 시작 후를 (10 배 말할 수 있습니다). 원격 PC를 재부팅 할 때까지는 원격 액세스에서 돌아올 수 없습니다.

재설정을 사용해 보았지만 결과는 같습니다.

아무도 아이디어가 있습니까 ??? 여기

는 클라이언트 측에 대한 내 코드

#include "ctrlboardclient.h" 
#include <QHostAddress> 
#include <QObject> 
#include <QtGui/QApplication> 
#include <QDebug> 


bool CtrlBoardClient::status_flag = false;   /* Flag to check the transfer status of Data */ 


CtrlBoardClient::CtrlBoardClient() 
{ 
    connect(&client, SIGNAL(connected()), this, SLOT(startTransfer())); 
    connect(&client, SIGNAL(readyRead()), this, SLOT(recieve_msg())); 
    connect(&client, SIGNAL(disconnected()), this, SLOT(disconnectstatus())); 
    connect(&client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(getErrorCode(QAbstractSocket::SocketError))); 
} 


bool CtrlBoardClient::start(QString address, quint16 port) 
{ 
    QHostAddress addr(address); 
    bool rval = client.reset(); 
    qDebug() << "Reset before Connect to Host = " << rval; 
    client.connectToHost(address, port); 

    if (!client.waitForConnected(3000)) { 
     bool rval = client.reset(); 
     qDebug() << "Reset after Connect to Host = " << rval; 
     qDebug() << "Client is unable to connect to Server "; 
     return false; 
    } 
    qDebug() << "Client Server Connection Successful"; 
    status_flag = false; 
    return true; 
} 

void CtrlBoardClient::getErrorCode(QAbstractSocket::SocketError errorCode) 
{ 
    qDebug() << "Socket Error = " << errorCode; 
} 


void CtrlBoardClient::SendMessage(QString Message) 
{ 
    status_flag = true; 
    msg = Message; 
    startTransfer(); 
    qDebug() << "Message sent to the Server"; 
    client.disconnectFromHost(); 
    while (!client.waitForDisconnected(3000)); 
    qDebug() << "Disconnected from the Host"; 
    return; 
} 


void CtrlBoardClient::startTransfer() 
{ 
    if (status_flag) { 
     QByteArray block = ""; 
     block.append(msg); 
     client.write(block); 
    } 

    status_flag = false; 
    return; 
} 


QByteArray CtrlBoardClient::RecieveMessage() 
{ 
    return indata; 
} 


void CtrlBoardClient::recieve_msg() 
{ 
    indata = ""; 
    indata.append(client.readAll()); 
    emit recievemsg(); 
} 


void CtrlBoardClient::disconnectstatus() 
{ 
    qDebug() << "Closing Client connection"; 
    CloseClientConnection(); 
    emit connection_aborted(); 
} 


void CtrlBoardClient::CloseClientConnection() 
{ 
    bool rval = client.reset(); 
    qDebug() << "Reset after Disconnect from Host = " << rval; 
    client.close(); 
} 

내 서버 코드입니다 :

#include "mainboardserver.h" 

MainBoardServer::MainBoardServer() 
{ 
    connect(&mainserver, SIGNAL(newConnection()), this, SLOT(acceptConnection())); 
    connect(this, SIGNAL(disconnected()), this, SLOT(DisconnectMessage())); 

    if (!mainserver.listen(QHostAddress::Any, 9876)) { 
     emit no_incoming_connection(); 
    } 
} 

void MainBoardServer::acceptConnection() 
{ 
    ctrlclient = mainserver.nextPendingConnection(); 
    connect(ctrlclient, SIGNAL(readyRead()), this, SLOT(startRead())); 
    connect(ctrlclient, SIGNAL(disconnected()), this, SLOT(DisconnectMessage())); 
    emit connection_found(); 
} 

void MainBoardServer::startRead() 
{ 
    char buffer[1024] = {0}; 
    ClientChat = ""; 
    ctrlclient->read(buffer, ctrlclient->bytesAvailable()); 
    ClientChat.append(buffer); 
    ctrlclient->close(); 
    emit data_recieved(); 
} 

QString MainBoardServer::RecieveData() 
{ 
    return ClientChat; 
} 

void MainBoardServer::TransferData(QByteArray data) 
{ 
    ctrlclient->write(data); 
} 

void MainBoardServer::DisconnectMessage() 
{ 
    emit connection_lost(); 
} 


void MainBoardServer::closeServer() 
{ 
    mainserver.close(); 
    emit disconnected(); 
} 

어떤 생각 방법이 문제를 해결하려면? 내가 저지른 실수는 무엇입니까 ???

+0

서버 코드는 어떤 모양입니까? 그것이 바로 문제가되는 곳입니다. –

+0

'bytesAvailable()'이 1024보다 클 경우'MainBoardServer :: startRead()'에 잠재적 인 버퍼 오버 플로우가 있습니다.'connection_lost()'핸들러는 무엇을합니까? closeServer()가 언제 호출됩니까? –

+0

@Remy closeServer()가 호출되면 클라이언트에서 "Quit"메시지가 수신됩니다. 또한 클라이언트 측에서 "Quit"(Hardcoded)을 보내고 있습니다. – skg

답변

1

이 문제에 대한 해결책을 찾았습니다.

위의 코드는 정확하고 올바르게 동작합니다. 문제는 PushButton을 통한이 애플리케이션 호출에있었습니다.

푸시 버튼 호출에 사용 된 포인터 변수로 인해 문제가 발생했습니다.

클래스의 직접적인 Object로 포인터를 대체하여 해결했습니다.

어쨌든 포인터에 대해 더 많이 배우고 복잡한 응용 프로그램에서 얼마나 위험 할 수 있는지를 배웠습니다.

4

나는 Qt는에 대해 아무것도 모르지만, 소켓 오류는 두 가지 가능성 중 하나를 의미한다 "연결 거부"

1) 대상 IP에/포트 모두를 듣고 어떤 서버 소켓이 없습니다.

2) 수신 대기중인 서버 소켓이 있지만 보류중인 클라이언트 연결의 백 로그가 가득 차서 그 순간에 새 연결을 허용 할 수 없습니다. 서버 코드가 모두 accept() 호출을 중지했거나 연결하려고하는 클라이언트의 수를 충족시킬만큼 충분히 빠르게 호출하지 않습니다.

어느 쪽이든 클라이언트는 실제로 어떤 상태가 발생했는지 알 수 없습니다. 잠시 기다렸다가 다시 연결 해보세요.

클라이언트 코드가 아닌 서버 코드를 진단해야합니다. 서버 코드가 예상대로 작동하지 않습니다.

+0

답변 해 주셔서 감사합니다. 필자의 경우 원격 화면에 액세스해야 할 때마다 누름 버튼을 누릅니다. 이로 인해 서버가 "새 연결 시작"을하고 "종료"후에 서버가 다시 시작됩니다. 나는 서버를 닫는다. 이 문제를 해결하기 위해 나를 안내 해주세요. – skg

관련 문제