嵌入式linux上QtcpServer不能接收到newConnection信号 qt4.8.6

srz1play 2018-02-08 03:55:15
请大神给指点指点!
下面的代码在windows和linux桌面上运行都没问题
///////mainwindow.h///////////////////
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QtNetwork>
//#include<QTcpSocket>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

QTcpSocket *m_tcpsocket;
QTcpServer *m_tcpserver;

private slots:
void newConnect();
// void readMessage();
// void sendMessage();
void testsignalslot();

private:
Ui::MainWindow *ui;
signals:
void testsignal();
};

#endif // MAINWINDOW_H

/////////////////////////////////////////////////////////
///////mainwindow.cpp///////////////////
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTcpServer>
#include<QTcpSocket>
#include<QDebug>
#include<QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_tcpserver = new QTcpServer(this);
m_tcpsocket = new QTcpSocket(this);
connect(this->m_tcpserver,SIGNAL(newConnection()), this,SLOT(newConnect()));
if (!m_tcpserver->listen(QHostAddress::Any,2404))
{
QMessageBox::about(NULL, "fail", "listen fail");
}
else
{
QMessageBox::about(NULL, "success", "listen success");
}
connect(this,SIGNAL(testsignal()),this,SLOT(testsignalslot()));
emit testsignal();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::newConnect()
{
m_tcpsocket = m_tcpserver->nextPendingConnection();//设置监听
connect(m_tcpsocket,SIGNAL(readyRead()),
this,SLOT(readMessage())); //服务器接收客户端的消息
connect(m_tcpsocket,SIGNAL(disconnected()),
m_tcpsocket,SLOT(deleteLater()));

QMessageBox::about(NULL, "About", "About this application");
ui->pushButton->setText("come on");
}

void MainWindow::testsignalslot()
{
QMessageBox::about(NULL, "test", "test sig!");
}
...全文
2321 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyousi 2018-04-11
  • 打赏
  • 举报
回复 1
另外,你的QTcpServer使用方法是不推荐的,所有的客户端连接后事件的处理都在主线程中执行,这样会有阻塞, 说不定会是造成socket server监听失败的原因。 正确的用法是,重载QTcpServer,实现incomingConnection函数(tcp服务器收到客户端连接请求时会调用这个函数), 在incomingConnection中开线程创建QTcpSocket对象。 这样的做法是正规的,可以使所有的客户端socket连接都由独立的线程来处理,而不是都由主线程来处理,主线程只处理socket连接事件。

TcpServer::TcpServer(QObject *parent) : QTcpServer(parent)
{

}

void TcpServer::incomingConnection(qintptr socketDescriptor)
{
    TcpSocketThread* thread = new TcpSocketThread(socketDescriptor, this);
//    connect(this, SIGNAL(broadcasted(QByteArray)), thread, SLOT(socketWrite(QByteArray)));
    ci.clientSocket = thread;
    g_clientMap[ci.id] = ci;
    thread->setId(ci.id);
    thread->start();
}
TcpSocketThread::TcpSocketThread(qintptr socketDescriptor, QObject *parent)
    : m_socketDescriptor(socketDescriptor), QThread(parent)
{

}

void TcpSocketThread::run()
{
    qDebug("[%x][TcpSocketThread::run]", QThread::currentThread());

    m_tcpSocket = new QTcpSocket();
    connect(m_tcpSocket, SIGNAL(connected()), this, SLOT(socketConnected()), Qt::DirectConnection);
    connect(m_tcpSocket, SIGNAL(aboutToClose()), this, SLOT(socketClosed()), Qt::DirectConnection);
    connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()), Qt::DirectConnection);
    connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(quit()), Qt::DirectConnection);
    connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(socketRead()), Qt::DirectConnection);
    connect(this, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::DirectConnection);
    connect(this, SIGNAL(writeRequested(QByteArray)), m_tcpSocket, SLOT(writeData(QByteArray)));
    connect(this, SIGNAL(finished()), m_tcpSocket, SLOT(deleteLater()));
    connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));

    if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor))
    {
        return;
    }

    this->exec();
}
tyousi 2018-04-11
  • 打赏
  • 举报
回复
在windows上有这样一个机制,所有的消息事件都要依赖于窗口句柄hwnd,socket事件也同样。 我做过实验,如果不创建任何hwnd,GetMessage是取不到任何消息的,原理上我说不太清楚,之前看过一篇帖子,但找不到了。 但是,上面的机制在linux上是不存在的, 在linux上使用基本的socket的api创建socket server,监听是正常的,但是我没有在linux上试验过QTcpServer。 你的问题是在嵌入式linux上不好用,不知道是不是因为Qt的QTcpServer也对窗口句柄有依赖, 建议使用基础的socket编程做socket server,也不难,socket()、bind()、listen()三个函数就搞定了,然后自己封装一个跨平台的SocketServer类, 放在Qt的环境中也是可以运行的。

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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