網站首頁 編程語言 正文
Qt TCP的客戶端與服務端的連接,供大家參考,具體內容如下
可以實現局域網內簡單的信息傳遞(文件傳輸,稍后更新)
界面是用ui設計師做的簡單設計
客戶端
(1)、ClientWidget.h 頭文件
#ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H
#include <QWidget>
#include "ui_ClientWidget.h"
#include <QTcpSocket>
#include <QHostAddress>
#include <QTextCodec>
class ClientWidget : public QWidget, public Ui::ClientWidget
{
?? ?Q_OBJECT
public:
?? ?ClientWidget(QWidget *parent = 0);
?? ?~ClientWidget();
private slots:
?? ?//連接按鈕
?? ?void onConnectButtonClicked();
?? ?//
?? ?void onTextEditRead();
?? ?//發送按鈕
?? ?void onButtonSendClicked();
?? ?//獲取對方發送的內容
?? ?void onRecesiveDataFromServer();
?? ?//斷開連接
?? ?void onDisConnect();
private:
?? ?//Ui::ClientWidget ui;
?? ?QTcpSocket *tcpSocket;
};
#endif // CLIENTWIDGET_H
(2)、ClientWidget.cpp文件
#include "stdafx.h"
#include "ClientWidget.h"
#include <QPushButton>
ClientWidget::ClientWidget(QWidget *parent)
?? ?: QWidget(parent)
{
?? ?setupUi(this);
?? ?setWindowTitle(QString::fromWCharArray(L"客戶端"));
?? ?tcpSocket = NULL;
?? ?//分配空間,指定父對象
?? ?tcpSocket = new QTcpSocket(this);
?? ?ButtonDisconnect->setEnabled(false);
?? ?//tcpSocket->abort();
?? ?//發送與服務器連接信號
?? ?connect(connectBtn, SIGNAL(clicked()), this, SLOT(onConnectButtonClicked()));
?? ?
?? ?//連接成功后接收到connected信號
?? ?connect(tcpSocket, SIGNAL(connected()), this, SLOT(onTextEditRead()));
?? ?//給服務器發送內容
?? ?connect(ButtonSend, SIGNAL(clicked()), this, SLOT(onButtonSendClicked()));
?? ?//接收來自服務器的內容,信號readReady
?? ?connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onRecesiveDataFromServer()));
?? ?connect(ButtonDisconnect, SIGNAL(clicked()), this, SLOT(onDisConnect()));
?? ?
}
ClientWidget::~ClientWidget()
{
}
void ClientWidget::onConnectButtonClicked()
{
?? ?//獲取服務器IP和端口
?? ?QString ipStr = LineEditIPName->text();
?? ?qint16 portName = LineEditPortName->text().toInt();
?? ?QHostAddress ip = QHostAddress(ipStr);
?? ?//主動和服務器建立連接
?? ?tcpSocket->connectToHost(ip, portName);
}
void ClientWidget::onTextEditRead()
{
?? ?TextEditRead->setText(QString::fromLocal8Bit("成功和服務器建立好連接!!!"));
?? ?connectBtn->setEnabled(false);
?? ?ButtonDisconnect->setEnabled(true);
}
void ClientWidget::onButtonSendClicked()
{
?? ?if (tcpSocket == NULL)
?? ?{
?? ??? ?return;
?? ?}
?? ?//獲取編輯框內容
?? ?QString str = TextEditWrite->toPlainText();
?? ?//發送文本內容
?? ?tcpSocket->write(str.toUtf8().data());
?? ?TextEditWrite->clear();
}
void ClientWidget::onRecesiveDataFromServer()
{
?? ?QByteArray arrayAll = tcpSocket->readAll();
?? ?QTextCodec *tc = QTextCodec::codecForName("UTF-8");
?? ?QString str = tc->toUnicode(arrayAll);
?? ?//追加到讀取編輯區中
?? ?TextEditRead->append(str);
}
void ClientWidget::onDisConnect()
{
?? ?if (tcpSocket == NULL)
?? ?{
?? ??? ?return;
?? ?}
?? ?tcpSocket->disconnectFromHost();
?? ?tcpSocket->close();
?? ?connectBtn->setEnabled(true);
?? ?ButtonDisconnect->setEnabled(false);
?? ?
?? ?TextEditRead->setText(QString::fromLocal8Bit("連接已斷開!!!"));
}
服務器
(1)、ServerWidget.h文件
#ifndef SERVERWIDGET_H
#define SERVERWIDGET_H
#include <QtGui/QWidget>
#include "ui_ServerWidget.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
#include <QTextCodec>
class ServerWidget : public QWidget
{
?? ?Q_OBJECT?? ?
public:
?? ?ServerWidget(QWidget *parent = 0, Qt::WFlags flags = 0);
?? ?~ServerWidget();
?? ?QTcpServer *tcpServer;
?? ?QTcpSocket *tcpSocket;
private slots:
?? ?void OnConnectTcpServer();?
?? ?void OnSendButtonClicked();
?? ?void OnCloseButtonClicked();
?? ?void OnSeResiveData();
?? ?void OnDisconnected();
private:
?? ?Ui::ServerWidgetClass ui;
};
#endif // SERVERWIDGET_H
(2)、ServerWidget.cpp 文件
#include "stdafx.h"
#include "ServerWidget.h"
ServerWidget::ServerWidget(QWidget *parent, Qt::WFlags flags)
?? ?: QWidget(parent, flags)
{
?? ?ui.setupUi(this);
?? ?tcpServer = NULL;
?? ?tcpSocket = NULL;
?? ?setWindowTitle(QString::fromWCharArray(L"服務器(端口:8888)"));
?? ?//箭筒套接字,指定父對象,讓其自動回收空間
?? ?tcpServer = new QTcpServer(this);
?? ?//監聽并綁定端口
?? ?tcpServer->listen(QHostAddress::Any, 8888);
?? ?
?? ?connect(tcpServer, SIGNAL(newConnection()), this, SLOT(OnConnectTcpServer()));
?? ?connect(ui.sendButton, SIGNAL(clicked()), this, SLOT(OnSendButtonClicked()));
?? ?connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(OnCloseButtonClicked()));
?? ?connect(tcpServer, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
}
ServerWidget::~ServerWidget()
{
?? ?
}
#include <QDebug>
void ServerWidget::OnConnectTcpServer()
{
?? ?//取出建立好的套接字
?? ?tcpSocket = tcpServer->nextPendingConnection();
?? ?//獲取對方的IP和端口號?
?? ?QString ipStr = tcpSocket->peerAddress().toString();
?? ?qint16 portName = tcpSocket->peerPort();
?? ?QString connectStr = QString::fromLocal8Bit("成功連接");
?? ?
?? ?QString tempStr = QString("[%1 : %2]:" + connectStr).arg(ipStr).arg(portName);
?? ?ui.textRead->setText(tempStr);
?? ??
?? ?connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(OnSeResiveData()));
}
void ServerWidget::OnSendButtonClicked()
{
?? ?if (tcpSocket == NULL)
?? ?{
?? ??? ?return;
?? ?}
?? ?//獲取編輯區的內容
?? ?QString str = ui.textWrite->toPlainText();
?? ?//給對方發送數據。使用套接字是tcpSocket
?? ?tcpSocket->write(str.toUtf8().data());
?? ?ui.textWrite->clear();
}
?
void ServerWidget::OnCloseButtonClicked()
{
?? ?if (tcpSocket == NULL)
?? ?{
?? ??? ?return;
?? ?}
?? ?//主動和客戶端斷開連接
?? ?tcpSocket->disconnectFromHost();
?? ?
?? ?ui.textRead->setText(QString::fromLocal8Bit("連接已斷開!!!"));
?? ?tcpSocket = NULL;
}
void ServerWidget::OnSeResiveData()
{
?? ?//從通信套接字中取出內容
?? ?QByteArray dataAll = ?tcpSocket->readAll();
?? ?QTextCodec *tc = QTextCodec::codecForName("UTF-8");
?? ?QString str = tc->toUnicode(dataAll);
?? ?ui.textRead->append(str);
}
void ServerWidget::OnDisconnected()
{
?? ?ui.textRead->setText(QString::fromLocal8Bit("連接已斷開!!!"));
}
原文鏈接:https://blog.csdn.net/Li__muamua/article/details/124262427
相關推薦
- 2022-08-30 C++超詳細介紹模板_C 語言
- 2022-08-26 Python中True(真)和False(假)判斷詳解_python
- 2023-03-13 Android自定義Toast樣式實現方法詳解_Android
- 2022-10-04 Go語言底層原理互斥鎖的實現原理_Golang
- 2022-03-22 詳解_beginthreadex()創建線程_C 語言
- 2023-04-19 Invalid prop: custom validator check failed for pr
- 2022-05-02 詳解SQL之CASE?WHEN具體用法_MsSql
- 2022-06-20 關于Golang獲取當前項目絕對路徑的問題_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支