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() ...
随机推荐
- UE4 保存为bitmap
TArray<FColor> colorData; colorData.Init(FColor(0, 0, 255, 255), 1920 * 1080); for (int ...
- block的修饰词为什么选用copy
想必很多开发人员知道一般用copy修饰block,接下来就讲解为什么需要用copy,甚至会讲到其实用strong修饰block也是可以的 在 Objective-C 语言中,一共有 3 种类型的 bl ...
- Redis 数据结构与内存管理策略(下)
Redis 数据结构与内存管理策略(下) 标签: Redis Redis数据结构 Redis内存管理策略 Redis数据类型 Redis类型映射 Redis 数据类型特点与使用场景 String.Li ...
- As a Start - 毫厘之间,宇宙之外
序 突然想聊聊多重宇宙——多重宇宙,这听上去像是一个科幻概念,但是严肃思考这一个问题时,将会带我们进入一个全新的格局和世界. 对宇宙学家而言,研究多重宇宙并不仅仅是为了猜测平行世界里某一历史事件是否有 ...
- JS中获取session中传过来的值对象
摘录自:http://www.360doc.com/content/11/0316/13/5790498_101627263.shtml 把某一对象置于session范围内,并在JSP页面中提取ses ...
- Azure Powershell对ASM资源的基本操作
本文主要介绍Windows Azure Powershell对ASM资源的基本操作 1.登陆ASM模式,命令:Add-AzureAccount -Environment AzureChinaCloud ...
- 一步步使用BMC Atrium Orchestrator Vmware Infrastructure Event Monitor
本教程将一步步演示怎么使用BMC Atrium Orchestrator (BAO) Vmware Infrastructure Event Monitor来监控VSphere Webservice的 ...
- myeclipse环境搭建
公司来了几个新人,老是在教他们环境搭建这些,每次在帮他们调试代码的时候老是不厌其烦的看着他们坐等myeclipse编译了,校验了什么的,而且在编码的时候也不使用快捷键,然后我就只能默默的坐回去了.为了 ...
- Linux安装JDK、MySQL和Tomcat
1 依赖的安装 因为JDK.Tomcat和MySQL的安装过程中需要从网上下载部分支持包才可以继续,所以需要提前安装好依赖. yum install glibc.1686 yum -y install ...
- 05_Javascript进阶第一天
内部私有函数 function a(){ alert('aaa'); return function b(){ alert('bbb'); } } //调用内部私有函数b,方法1 var func=a ...