我只是无法让QTcpServer工作(从未调用newConnection)
我知道有人问过类似的问题,但我还没有找到解决问题的答案 我正在修改一些现有的Qt代码,以便将服务器功能添加到我公司使用的程序中。为此,我在现有对话框中添加了一个QTcpServer对象,调用listen()并将一个插槽连接到newConnection发射器,如:我只是无法让QTcpServer工作(从未调用newConnection),qt,qtcpserver,Qt,Qtcpserver,我知道有人问过类似的问题,但我还没有找到解决问题的答案 我正在修改一些现有的Qt代码,以便将服务器功能添加到我公司使用的程序中。为此,我在现有对话框中添加了一个QTcpServer对象,调用listen()并将一个插槽连接到newConnection发射器,如: .h class QConsole : public QDialog { Q_OBJECT public: void init(); public slots:
.h
class QConsole : public QDialog
{
Q_OBJECT
public:
void init();
public slots:
void new_Connection();
private:
QTcpServer m_Server;
}
.cpp
void QConsole::init()
{
m_Server.listen(QHostAddress::Any, 12346);
QDialog::connect(&m_Server, SIGNAL(newConnection()), this, SLOT(new_Connection()));
}
主要问题是:
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * _output_window = new QConsole(desktopRect);
_output_window->init();
_output_window->show();
return app.exec();
}
new_Connection()从未被调用,因此我看不出相关性,但它是:
void QConsole::new_Connection()
{
}
这很好,因为我的程序开始侦听指定的端口,如果我对它进行telnet连接,它会建立某种连接,但从未调用过new_connection()
我看到的关于这个问题的帖子可以追溯到2005年,所以这显然不是什么新鲜事,但我没有找到一个令人满意的答案(或者任何答案)。这让工作中的每个人都感到困惑,即使是编写了Qt服务器程序的人。我猜现有的框架有一些根本性的错误,但我不知道它可能是什么
为此,我花了一天半的时间,最后我成功地使用了waitForNewConnection(),它实际上会给我返回一个套接字,但当我连接到readReady()发射器时,它也没有被触发。那么,怎样才能防止这些信号永远不会被调用呢
请原谅我的理智,尽你所能帮助我。这里是一个完整的工作示例,使用MSVC++2010进行测试 这将侦听端口12346上的连接,用“HELLO WORLD”回复,并将连接记录到对话框上的列表中 main.cpp
#include <QtGui>
#include "console.hpp"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Console con;
con.show();
return app.exec();
}
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
class Console : public QDialog
{
Q_OBJECT
public:
Console();
public slots:
void connection();
private:
QTcpServer mServer;
QListWidget* mConnList;
};
#include "console.hpp"
Console::Console() :
QDialog(),
mServer(),
mConnList(new QListWidget())
{
if (!mServer.listen(QHostAddress::Any, 12346))
qDebug() << "Error during 'listen'" << mServer.errorString();
connect(&mServer, SIGNAL(newConnection()), this, SLOT(connection()));
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->addWidget(mConnList);
setLayout(mainLayout);
}
void Console::connection()
{
qDebug() << "CONNECTION";
QTcpSocket* skt = mServer.nextPendingConnection();
if (!skt)
return;
mConnList->addItem(QString("%1:%2").arg(skt->peerAddress().toString()).arg(skt->peerPort()));
skt->write("HELLO WORLD!\r\n");
skt->close();
}
另一个工作示例,同样是在Linux上,尽管我以前编写过一个程序,使用QTcpServer在Linux和Windows上运行,没有问题。如果这不起作用,肯定是Qt安装或操作系统配置问题。要么就是Qt版本中的错误
~/tcp_test$ qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
~/tcp_test$ for file in qconsole.{h,cpp} main.cpp tcp_test.pro ; do echo -e "$file:\n"; cat $file; echo; echo; done
qconsole.h:
#include <QDialog>
#include <QTcpServer>
class QConsole : public QDialog
{
Q_OBJECT
public:
QConsole();
public slots:
void connection();
private:
QTcpServer server;
};
qconsole.cpp:
#include "qconsole.h"
QConsole::QConsole()
{
server.listen(QHostAddress::Any, 12346);
QDialog::connect(&server, SIGNAL(newConnection()), this, SLOT(connection()));
}
void QConsole::connection()
{
qDebug("got connection");
}
main.cpp:
#include <QApplication>
#include "qconsole.h"
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * window = new QConsole();
window->show();
return app.exec();
}
tcp_test.pro:
QT = core gui network
CONFIG += debug
TARGET = tcp_test
SOURCES = main.cpp qconsole.cpp
HEADERS = qconsole.h
~/tcp_test$ ./tcp_test &
[3] 9784
~/tcp_test$ nc localhost 12346
got connection
^C
~/tcp\u测试$qmake--版本
QMake版本2.01a
在/usr/lib/x86_64-linux-gnu中使用Qt版本4.8.6
qconsole.{h,cpp}main.cpp tcp_test.pro中文件的~/tcp_test$;执行echo-e“$file:\n”;cat$文件;回声;回声;完成
qconsole.h:
#包括
#包括
QConsole类:公共QDialog
{
Q_对象
公众:
QConsole();
公众时段:
无效连接();
私人:
qtcserver服务器;
};
qconsole.cpp:
#包括“qconsole.h”
QConsole::QConsole()
{
侦听(QHostAddress::Any,12346);
QDialog::connect(&服务器,信号(newConnection()),this,SLOT(connection());
}
void QConsole::connection()
{
qDebug(“获得连接”);
}
main.cpp:
#包括
#包括“qconsole.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QConsole*窗口=新的QConsole();
窗口->显示();
返回app.exec();
}
tcp_test.pro:
QT=核心gui网络
配置+=调试
目标=tcp\U测试
SOURCES=main.cpp qconsole.cpp
HEADERS=qconsole.h
~/tcp\u测试$。/tcp\u测试&
[3] 9784
~/tcp\u测试$nc本地主机12346
有联系
^C
该代码在这里工作正常。请发布新的连接代码,并说明如何调用init
方法。添加一个最小的main
,使您的代码以最小的速度工作将是很好的。我的项目设置中一定有什么东西阻止了信号被调用,但我一辈子都搞不清楚!如何确定未调用插槽?在new_Connection()函数中放置断点。还有更多的代码,但考虑到它从未被调用,我认为它无关紧要。我想在这一点上,我应该提到,我正在使用VS2010开发一个Windows应用程序。正如我所说,您在WorksForMe(Linux上)上面有什么。尝试删除所有无关的代码,只保留现有的代码,并在插槽中添加调试打印。确保控制台中没有收到警告。
~/tcp_test$ qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
~/tcp_test$ for file in qconsole.{h,cpp} main.cpp tcp_test.pro ; do echo -e "$file:\n"; cat $file; echo; echo; done
qconsole.h:
#include <QDialog>
#include <QTcpServer>
class QConsole : public QDialog
{
Q_OBJECT
public:
QConsole();
public slots:
void connection();
private:
QTcpServer server;
};
qconsole.cpp:
#include "qconsole.h"
QConsole::QConsole()
{
server.listen(QHostAddress::Any, 12346);
QDialog::connect(&server, SIGNAL(newConnection()), this, SLOT(connection()));
}
void QConsole::connection()
{
qDebug("got connection");
}
main.cpp:
#include <QApplication>
#include "qconsole.h"
int main( int argc, char *argv[] )
{
QApplication app(argc, argv);
QConsole * window = new QConsole();
window->show();
return app.exec();
}
tcp_test.pro:
QT = core gui network
CONFIG += debug
TARGET = tcp_test
SOURCES = main.cpp qconsole.cpp
HEADERS = qconsole.h
~/tcp_test$ ./tcp_test &
[3] 9784
~/tcp_test$ nc localhost 12346
got connection
^C