qt5 网络中的连接错误
Connection error in qt5 network
我想做一个像MSN Messenger这样的程序,我已经将Qt5与网络一起使用,当我打开与本地服务器的新连接时,它不起作用,它给了我我的服务器没有连接我不知道原因
主窗口.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtNetwork/QTcpServer> #include <QtNetwork/QTcpSocket> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); void newConnection (); private: Ui::MainWindow *ui; QTcpServer *server; }; #endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); server = new QTcpServer(this); connect(server , SIGNAL(newConnection()) , this , SLOT(newConnection())); if(server->listen(QHostAddress::Any , 5050)) { ui->label->setText("Not Start"); } else { ui->label->setText("Server Started Now"); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::newConnection() { QTcpSocket *socket = server->nextPendingConnection(); socket->write("Hello Islam"); socket->flush(); socket->waitForBytesWritten(3000); socket->close(); } void MainWindow::on_pushButton_clicked() { newConnection(); }
这用于调用库
#------------------------------------------------- # # Project created by QtCreator 2013-11-03T10:00:37 # #------------------------------------------------- QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = TCPTEST TEMPLATE = app SOURCES += main.cpp mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui
我尝试在主窗口构造函数中调用新连接,但它不起作用
我认为错误在
if(server->listen(QHostAddress::Any , 1234))
它有效,但一定是
if(!server->listen(QHostAddress::Any , 1234))
启动服务器
相关文章:
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- Mongocxx 连接错误
- JNI - WMI 连接错误
- boost :: Asio连接错误的界面
- QT 5.8 SQL连接错误:Windows 10上未加载QMYSQL驱动程序
- C# 客户端到C++服务器 Tcp 套接字连接错误
- MySQL 丢失连接错误
- 无线局域网连接错误
- vcpkg 安装失败,并显示"SSL 连接错误"
- 包装QObject::连接错误
- 提升安卓 Asio 连接错误
- C++连接错误
- qt5 网络中的连接错误
- 套接字连接错误 c 套接字
- Wt Dbo MySQL 后端连接错误"SET storage_engine=INNODB;":未知的系统变量'storage_engine'
- QT - 从成员 QWidget 连接错误的按钮
- mysql和c++在vs 2010中:sqlstatementhandle和连接错误
- Boost测试fpic连接错误
- HWLOC连接错误
- WinInet ftp连接错误123