qt中的tcp编程
server
.server.h
#define DIALOG_H #include <QDialog>
#include <QTcpServer>
#include <QTcpSocket>
#include<QHostAddress>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked(); void on_pushButton_2_clicked(); private:
Ui::Dialog *ui;
QTcpServer *p;
QTcpSocket *newp;//产生的新socket
QHostAddress *hp;
public slots:
void apprecvconnect();//处理连接函数
void getmsg();
}; #endif // DIALOG_H
server.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include<QString>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
p = new QTcpServer(); // newp = new QTcpSocket();
hp = new QHostAddress("192.168.1.30");
connect(p,SIGNAL(newConnection()),this,SLOT(apprecvconnect()));
} Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{
p->listen(*hp,); }
void Dialog::apprecvconnect()
{
newp = p->nextPendingConnection();
connect(newp,SIGNAL(readyRead()),this,SLOT(getmsg())); }
void Dialog::getmsg()
{
QByteArray tmp = newp->readAll();
QString tmp2(tmp);
ui->label_2->setText(tmp2);
} void Dialog::on_pushButton_2_clicked()
{
QString tmp = ui->lineEdit->text();
newp->write(tmp.toLatin1());
}
client.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include<QTcpServer>
#include<QTcpSocket>
#include<QHostAddress>
#include<QMessageBox>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked(); public slots:
void sett();
void getmsg(); private:
Ui::Dialog *ui;
QTcpSocket *p;
QHostAddress *hp;
}; #endif // DIALOG_H
sever.cpp
#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
p= new QTcpSocket();
hp = new QHostAddress("192.168.1.30");
} Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{
p->connectToHost(*hp,);
connect(p,SIGNAL(connected()),this,SLOT(sett()));
}
void Dialog::sett()
{
QMessageBox::about(this,"about","connect success");
connect(p,SIGNAL(readyRead()),this,SLOT(getmsg())); }
void Dialog::getmsg()
{
QByteArray tmp = p->readAll();
QString tmp2(tmp);
ui->label_2->setText(tmp2);
}
void Dialog::on_pushButton_2_clicked()
{
QString tmp = ui->lineEdit->text();
p->write(tmp.toLatin1());
}
ui.h
#ifndef UI_DIALOG_H
#define UI_DIALOG_H #include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton> QT_BEGIN_NAMESPACE class Ui_Dialog
{
public:
QPushButton *pushButton;
QPushButton *pushButton_2;
QLabel *label;
QLineEdit *lineEdit;
QLabel *label_2; void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(, );
pushButton = new QPushButton(Dialog);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(, , , ));
pushButton_2 = new QPushButton(Dialog);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
pushButton_2->setGeometry(QRect(, , , ));
label = new QLabel(Dialog);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(, , , ));
lineEdit = new QLineEdit(Dialog);
lineEdit->setObjectName(QStringLiteral("lineEdit"));
lineEdit->setGeometry(QRect(, , , ));
label_2 = new QLabel(Dialog);
label_2->setObjectName(QStringLiteral("label_2"));
label_2->setGeometry(QRect(, , , )); retranslateUi(Dialog); QMetaObject::connectSlotsByName(Dialog);
} // setupUi void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", ));
pushButton->setText(QApplication::translate("Dialog", "connect", ));
pushButton_2->setText(QApplication::translate("Dialog", "send", ));
label->setText(QApplication::translate("Dialog", "client", ));
label_2->setText(QString());
} // retranslateUi }; namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui QT_END_NAMESPACE #endif // UI_DIALOG_H
qt中的tcp编程的更多相关文章
- QT中的SOCKET编程(QT-2.3.2)
转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...
- QT中的SOCKET编程
转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...
- Qt中的多线程编程
http://www.ibm.com/developerworks/cn/linux/l-qt-mthrd/ Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功 ...
- python中的TCP编程学习
今天看了一下关于python的TCP编程. 发现思路和其他语言(比如java)思路基本上差点儿相同. 先看client.基本过程例如以下: 第一步:创建一个socket 第二步:建立连接 第三步:发送 ...
- Qt中的串口编程之三
QtSerialPort 今天我们来介绍一下QtSerialPort模块的源代码,学习一下该可移植的串口编程库是怎么实现的. 首先,我们下载好了源代码之后,使用QtCreator打开整个工程,可以看到 ...
- 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
1 新建一个项目:TCPServer.pro A 修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...
- Qt中的串口编程之一
QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...
- qt中的udp编程
UDP QUdpSocket ---> upd socket 1.创建 QUdpSocket *p = new QUdpSocket(); 2.最先接收数据一方 调用bind-> ip/p ...
- golang中的tcp编程
1. tcp server package main import ( "bufio" "fmt" "net" ) func main() ...
随机推荐
- js object 常用方法总结
Object.assign(target,source1,source2,...) 该方法主要用于对象的合并,将源对象source的所有可枚举属性合并到目标对象target上,此方法只拷贝源对象的自身 ...
- 请问:关于织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表的问题
要设置织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表, 就按照如下图所示的方法进行操作,为什么 点击导航上的父栏目出现死循环呢, 根本浏览不了网页. 请各位大神指点指点,为什么点击 ...
- vue源码入口文件分析
开发vue项目有段时间了, 之前用angularjs 后来用 reactjs 但是那时候一直没有时间把自己看源码的思考记录下来,现在我不想再浪费这 来之不易的思考, 我要坚持!! 看源码我个人感觉非常 ...
- SSH的jar包下载地址
spring http://repo.spring.io/libs-release-local/org/springframework/spring/ 条理清晰的搭建SSH环境之添加所需jar包 ht ...
- JVM-类的四种载入方式
package org.burning.sport.javase.classloader; public class ClassLoaderMain { public static void main ...
- [转]怎么查看和修改 MySQL 的最大连接数?
使用 MySQL 数据库的站点,当访问连接数过多时,就会出现 "Too many connections" 的错误.出现这种错误有两种情况,一种是网站访问量实在太大,服务器已经负担 ...
- yum错误,Cannot find a valid baseurl for repo: base 和 No more mirrors to try
可能出错原因: 1. yum 配置错误 2. 虚拟机无法连接外网 3. 域名解析没有 如何解决这个错误? 1. 网上找 /ect/yum.conf 和 /etc/yum.repos.d/CentOS- ...
- Windows挂钩注入DLL
注入DLL实现源码:HINSTANCE g_hInstDll = NULL; HHOOK g_hHook = NULL; DWORD g_dwThreadId = 0; #ifdef _MANAGED ...
- junit设计模式--命令者模式
命令模式的意图 将一个请求封装成一个对象,从而使你可以用不同的请求对客户进行参数化: 对请求排队或记录请求日志,以及支持可撤销的操作: 命令模式告诉我们可以为一个操作生成一个对象并给出它的一个执行方法 ...
- rpm命令的使用
如果服务器配置了本地yum源,大部分的常用软件包都是有的.如果差了头文件或者什么so文件,可以按如下方式找:#以memory.h为例 [root@sz-cdn-centos7-1 tmp]# wher ...