qt tcp编程一个基本sample QtcpServer 类的incomingConnection()方法在有新的连接建立时 没有调用

jzb2008lds 2013-12-19 03:05:24
qt tcp编程一个基本sample
QtcpServer 类的incomingConnection()方法在有新的连接建立时没有调用
重写的虚函数代码如下:

void TcpServer::incomingConnection(int socketDescriptor)
{

TcpSocket *tcpSocket = new TcpSocket(this);
connect(tcpSocket, SIGNAL(updateClients(QString,int)),
this, SLOT(updateClients(QString,int)));
connect(tcpSocket, SIGNAL(disconnected(int)),
this, SLOT(tcpDisconnected(int)));

tcpSocket->setSocketDescriptor(socketDescriptor);
tcpSocketList.append(tcpSocket);
//在list末尾插入数据
return emit QTcpServer::newConnection ();
}


其中自定义的QTcpServer类子类构造函数实现如下:

TcpServer::TcpServer(QObject *parent, int port) :
QTcpServer(parent)
{

if(this->listen (QHostAddress::Any,port))
{
qDebug("invocation!!!");

}
else
{
qDebug("disinvocation!!!");


}


//监听本机的IP地址和端口
}




为什么当listen()方法执行了,并且客户端使用
tcpSocket->connectToHost(ipAddress, port);
也成功连接了,但是incomingConnection()并没有执行。
求解释啊!!!!
...全文
2002 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜豆豆 2016-07-15
  • 打赏
  • 举报
回复
qt5 以及之后的参数都是qintptr。qt5之前的都是int,所以我从网上下载源码后,发现有新的连接时,并不会调用此虚函数。当我把参数改为qinptr就能成功调用了。
QQ_278397935 2014-12-15
  • 打赏
  • 举报
回复
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QTcpSocket>
#include <QDebug>

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(int ID, QObject *parent = 0);
    int ID;
    void run();
    struct messge_test
    {
        std::string SN;
        std::string IP;
        std::string Condition;
    };//message_rev;
    quint16 nextBlockSize;
signals:
    void error(QTcpSocket::SocketError socketerror);

public slots:
    void readyRead();
    void disconnected();

private:
    QTcpSocket *socket;
    int socketDescriptor;
};

#endif // MYTHREAD_H
#include "mythread.h"
MyThread::MyThread(int ID , QObject *parent) :
    QThread(parent)
{
    this->socketDescriptor = ID;
}

void MyThread::run()
{
    socket = new QTcpSocket();
    if(!socket->setSocketDescriptor(this->socketDescriptor))
    {
        emit error(socket->error());
        return;
    }
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    exec();
}

void MyThread::readyRead()
{
    messge_test *message_rev; 
    QByteArray data = socket->readAll();
    message_rev = (messge_test*)data.data();
    qDebug()<<"data:"<<data.data();
}

void MyThread::disconnected()
{
    socket->deleteLater();
    exit(0);
}
QQ_278397935 2014-12-15
  • 打赏
  • 举报
回复
大咖们好,我也是用的这个例子,单独调试没问题。 把这几个代码放进我做的程序里面就有问题了:incomingconnection()没有被调用。 代码如下:(myserver的。)
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QDebug>
#include <QTcpServer>

class MyServer : public QTcpServer
{
    Q_OBJECT
public:
    explicit MyServer(QObject *parent = 0);
    void startServer();
signals:

public slots:

protected:
    void incomingConnection(int  socketDescriptor);

};

#endif // MYSERVER_H
#include "myserver.h"
#include "mythread.h"

MyServer::MyServer(QObject *parent) :
    QTcpServer(parent)
{
}
void MyServer::startServer()
{
    int port = 6666;

    if(!this->listen(QHostAddress::Any, port))
    {
        qDebug() << "Could not start server";
    }
    else
    {
        qDebug() << "Listening to port " << port << "...";
    }
}
void MyServer::incomingConnection(int socketDescriptor)
{
    qDebug()<<"incomingconnection!";
    MyThread *thread = new MyThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}
jzb2008lds 2013-12-19
  • 打赏
  • 举报
回复
我自己回答吧!在qt5中 incomingConnection(type) 应该是qintptr,之前的版本是int类型的参数
jzb2008lds 2013-12-19
  • 打赏
  • 举报
回复
好冷清啊!11

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧