QT总结之TCP连接
来源:互联网 发布:网络教育毕业总结 编辑:程序博客网 时间:2024/06/08 09:27
服务器界面
serverwidget.h
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QTcpServer>//监听套接字#include <QTcpSocket>//通信套接字namespace Ui {class Widget;}class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); ~Widget();private slots: void on_pushButton_clicked(); void on_Close_clicked();private: Ui::Widget *ui; QTcpServer *tcpServer; QTcpSocket *tcpSocket;};#endif // WIDGET_H
serverwidget.cpp
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); tcpServer = NULL; tcpSocket = NULL; //监听 tcpServer = new QTcpServer(this); tcpServer->listen(QHostAddress::Any, 8888); setWindowTitle("title"); connect(tcpServer, &QTcpServer::newConnection, [=]() { //取出建立好的连接的套接字 tcpSocket = tcpServer->nextPendingConnection(); //获取对方ip 和 窗口 QString ip=tcpSocket->peerAddress().toString(); qint16 port=tcpSocket->peerPort(); QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port); ui->read->setText(temp);//读取数据 connect(tcpSocket, &QTcpSocket::readyRead, [=](){ QByteArray array = tcpSocket->readAll(); ui->read->append(array); } ); } );}Widget::~Widget(){ delete ui;}void Widget::on_pushButton_clicked(){ if(tcpSocket == NULL){ return; } //获取编辑区内同 QString str = ui->write->toPlainText(); //发送数据 tcpSocket->write( str.toUtf8().data());}void Widget::on_Close_clicked(){ if(tcpSocket == NULL){ return; } tcpSocket->disconnectFromHost(); tcpSocket->close(); tcpSocket = NULL;}
客户端:
clientwidget.h:
#ifndef CLIENTWIDGET_H#define CLIENTWIDGET_H#include <QWidget>#include <QTcpSocket>namespace Ui {class ClientWidget;}class ClientWidget : public QWidget{ Q_OBJECTpublic: explicit ClientWidget(QWidget *parent = 0); ~ClientWidget();private slots: void on_connect_clicked(); void on_send_clicked(); void on_close_clicked();private: Ui::ClientWidget *ui; QTcpSocket *tcpSocket;};#endif // CLIENTWIDGET_H
clientwidget.cpp
#include "clientwidget.h"#include "ui_clientwidget.h"#include <QHostAddress>ClientWidget::ClientWidget(QWidget *parent) : QWidget(parent), ui(new Ui::ClientWidget){ ui->setupUi(this); tcpSocket = NULL; tcpSocket = new QTcpSocket(this); connect(tcpSocket, &QTcpSocket::connected, [=](){ ui->read->setText("success!"); } ); connect(tcpSocket, &QTcpSocket::readyRead, [=](){ //获取对方发送的内容 QByteArray array = tcpSocket->readAll(); ui->read->append(array); } );}ClientWidget::~ClientWidget(){ delete ui;}void ClientWidget::on_connect_clicked(){ QString ip = ui->ip->text(); qint16 port = ui->port->text().toInt(); tcpSocket->connectToHost(QHostAddress(ip),port);}void ClientWidget::on_send_clicked(){ QString str = ui->write->toPlainText(); //发送数据 tcpSocket->write(str.toUtf8().data());}void ClientWidget::on_close_clicked(){ tcpSocket->disconnectFromHost(); tcpSocket->close();}
界面:
客户端
服务器:
阅读全文
0 0
- QT总结之TCP连接
- Qt TCP之Server/Client/Socket信号、槽等总结
- TCP连接关闭总结
- TCP连接关闭总结
- TCP连接关闭总结
- qt之tcp
- TCP之连接管理
- TCP连接的坑总结
- Qt之连接MySQL
- Qt之连接MySQL
- Qt模拟串口-tcp连接发送数据
- qt tcp长连接 粘包
- Qt TCP客户端无法连接服务器
- QT开发之TCP协议
- qt之tcp(一)
- qt之tcp(二)
- Qt TCP之自定义通信协议
- TCP之种种连接异常
- TCP/IP三次握手和四次挥手
- POJ 2241 The Tower of Babylon 笔记
- Java包命名规则
- [Freemarker] freemarker内建函数
- 栈的各种操作
- QT总结之TCP连接
- 关于Flex的ScrollBar
- 杂记
- Spring IOC(-)
- tensorflow用skipgram+nce模型实现中文word2vec
- java 学习笔记--利用反射实现泛型数组的复制
- SpringMVC的处理模型方式
- laravel验证码
- hibernate 注解全方案