【问题标题】:QTcpSocket, can't read incoming dataQTcpSocket,无法读取传入数据
【发布时间】:2014-05-11 12:03:59
【问题描述】:

我尝试使用 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 则服务器仅响应“Writing”并退出。

【问题讨论】:

  • 您从类中删除了 sok,而不是 newConnection() 中的局部变量。现在 readyRead 信号没有连接到任何东西。从 nextPendingConnection() 获得套接字后立即连接
  • 从 nextPendingConnection 获取传入连接后,您将立即关闭它!你期望它如何接收数据?不要关闭它!
  • on_readyRead() 中的 sok 指的是什么??

标签: qt qtcpsocket


【解决方案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->nextPendingConnection();

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

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

正确使用 QTcpServer 应该是:

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

【讨论】:

  • 而且这种方式还允许多个并发连接,甚至不需要存储QTcpSocket指针数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
相关资源
最近更新 更多