QTcpSocket,无法读取传入的数据

时间:2014-05-11 12:03:59

标签: qt qtcpsocket

我尝试使用Qt和C ++编写简单的服务器。这是代码:

   #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");
}

PHP客户端的一部分:

$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;
}

问题是我无法从php客户端读取,特别是信号readyRead没有被发出。此外,如果我尝试键入命令行:telnet 127.0.0.1 3333然后服务器只响应&#34;写&#34;并退出。

1 个答案:

答案 0 :(得分: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 * sok = new QTcpSocket();

这将创建一个本地QTcpSocket指针,该指针隐藏了类的sok成员。 此外,您使用new创建QTcpSocket,并在下一行丢失它(内存泄漏)

  

sok = server-&gt; nextPendingConnection();

因此,类的sok成员变量永远不会与传入连接一起使用。

您在sok类中使用了名为MyTcpServer的成员变量,在sok中使用了局部变量MyTcpServer::on_readyRead,这是一个坏主意。

正确使用QTcpServer将是:

  • 将传入连接存储在成员变量指针
  • 连接readyRead信号