2014-05-11 6 views
0

Qt와 C++를 사용하여 간단한 서버를 작성하려고합니다. PHP에QTcpSocket, 들어오는 데이터를 읽을 수 없습니다.

#include "mytcpserver.h" 

MyTcpServer::MyTcpServer(QObject *parent) : 
    QObject(parent) 
{ 
    server = new QTcpServer(this); 


    // whenever a user connects, it will emit signal 
    connect(server, SIGNAL(newConnection()), 
      this, SLOT(newConnection())); 



    if(!server->listen(QHostAddress("127.0.0.1"), 3333)) 
    { 
     qDebug() << "Server could not start"; 
    } 
    else 
    { 
     qDebug() << "Server started!"; 
    } 
} 

    void MyTcpServer::on_readyRead() 
    { 
QTcpSocket * senderSocket = dynamic_cast<QTcpSocket *>(sender()); 
if (senderSocket) 
{ 
    qDebug() << "reading data"; 
    qDebug() << senderSocket->readAll(); 
} 
    } 

    void MyTcpServer::newConnection() 
{ 
    // need to grab the socket 
QTcpSocket *sok=new QTcpSocket(); 
sok = server->nextPendingConnection(); 
if (sok) 
{ 
    connect(sok,SIGNAL(readyRead()),this,SLOT(on_readyRead())); 
    connect(sok,SIGNAL(disconnected()),sok,SLOT(deleteLater())); 
} 
sok->write("Writing"); 
} 

클라이언트의 일부 : 다음 코드

$address = "127.0.0.1"; 
$service_port = "3333"; 
echo "Attempting to connect to '$address' on port '$service_port'..."; 
$result = socket_connect($socket, $address, $service_port); 
if ($result === false) { 
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socke$ 
} else { 
    echo "OK.\n"; 
} 

$in = "HEAD/HTTP"; 
$out = ''; 

echo "Sending HTTP HEAD request..."; 
socket_write($socket, $in, strlen($in)); 
echo "OK.\n"; 
echo "Reading response:\n\n"; 
while ($out = socket_read($socket, 2048)) { 
    echo $out; 
} 

문제는 I는 즉, PHP 클라이언트로부터 판독 readyRead가 방출되지 않는 신호를 할 수 없다는 것이다. 또한 내가 명령 줄에 입력하려고하면 : 텔넷 127.0.0.1 3333 다음 서버는 "쓰기"및 종료 응답합니다.

+0

(). 이제 readyRead 신호가 아무 것도 연결되지 않습니다. nextPendingConnection()에서 소켓을 얻 자마자 연결하십시오. – galinette

+0

nextPendingConnection에서 가져 오는 즉시 들어오는 연결을 닫습니다! 어떻게 데이터를받을 것으로 기대합니까? 그것을 닫지 마십시오! – galinette

+0

on_readyRead()에서 sok이란 무엇입니까? – galinette

답변

1

이 시도 :

#include "mytcpserver.h" 

MyTcpServer::MyTcpServer(QObject *parent) : 
    QObject(parent) 
{ 
    server = new QTcpServer(this); 

    // whenever a user connects, it will emit signal 
    connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); 


    if(!server->listen(QHostAddress("127.0.0.1"), 3333)) 
    { 
     qDebug() << "Server could not start"; 
    } 
    else 
    { 
     qDebug() << "Server started!"; 
    } 
} 

void MyTcpServer::on_readyRead() 
{ 
    QTcpSocket * senderSocket = dynamic_cast<QTcpSocket*>(sender()); 
    if(senderSocket) 
    { 
     qDebug() << "reading data"; 
     qDebug() << senderSocket->readAll(); 
    } 
} 

void MyTcpServer::newConnection() 
{ 
    // need to grab the socket 
    QTcpSocket * newSocket = server->nextPendingConnection(); 
    if(newSocket) 
    { 
     connect(newSocket ,SIGNAL(readyRead()),this,SLOT(on_readyRead())); 

     //Additionnal auto clean-up without keeping track of the pointer 
     connect(newSocket ,SIGNAL(disconnected()),newSocket ,SLOT(deleteLater())); 
    } 
} 

을 (이 선언 할 때 :

QTcpSocket의 *에 쏙 = 새로운 QTcpSocket을();

이있는 가죽 로컬 QTcpSocket 포인터를 생성 sok 클래스의 구성원 또한 new를 사용하여 QTcpSocket을 만들고 다음 줄에서 누락 (메모리 누수)

sok = server-> nextPendingConnection();

따라서 sok 클래스의 멤버 변수는 들어오는 연결과 함께 사용되지 않습니다.

당신은 MyTcpServer 클래스 sok라는 멤버 변수와 MyTcpServer::on_readyRead에서 지역 변수 sok를 사용하고,이 나쁜 생각입니다.

QTcpServer의

적절한 사용은 다음과 같습니다 일원에서

  • 스토어 들어오는 연결 변수 포인터
  • 는 readyRead 신호를 연결
  • 당신은 클래스하지 newConnection의 로컬 변수에서 쏙을 제거
+0

이렇게하면 QTcpSocket 포인터 배열을 저장하지 않고도 여러 동시 연결을 허용합니다 – galinette

관련 문제