欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt基于TCP實現(xiàn)客戶端與服務(wù)端的連接

 更新時間:2022年08月22日 10:50:21   作者:李木木呀~  
這篇文章主要為大家詳細(xì)介紹了Qt基于TCP實現(xiàn)客戶端與服務(wù)端的連接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Qt TCP的客戶端與服務(wù)端的連接,供大家參考,具體內(nèi)容如下

可以實現(xiàn)局域網(wǎng)內(nèi)簡單的信息傳遞(文件傳輸,稍后更新)

界面是用ui設(shè)計師做的簡單設(shè)計

客戶端

(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();
?? ?//發(fā)送按鈕
?? ?void onButtonSendClicked();

?? ?//獲取對方發(fā)送的內(nèi)容
?? ?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();

?? ?//發(fā)送與服務(wù)器連接信號
?? ?connect(connectBtn, SIGNAL(clicked()), this, SLOT(onConnectButtonClicked()));
?? ?
?? ?//連接成功后接收到connected信號
?? ?connect(tcpSocket, SIGNAL(connected()), this, SLOT(onTextEditRead()));
?? ?//給服務(wù)器發(fā)送內(nèi)容
?? ?connect(ButtonSend, SIGNAL(clicked()), this, SLOT(onButtonSendClicked()));
?? ?//接收來自服務(wù)器的內(nèi)容,信號readReady
?? ?connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onRecesiveDataFromServer()));

?? ?connect(ButtonDisconnect, SIGNAL(clicked()), this, SLOT(onDisConnect()));
?? ?
}

ClientWidget::~ClientWidget()
{

}

void ClientWidget::onConnectButtonClicked()
{
?? ?//獲取服務(wù)器IP和端口
?? ?QString ipStr = LineEditIPName->text();
?? ?qint16 portName = LineEditPortName->text().toInt();
?? ?QHostAddress ip = QHostAddress(ipStr);
?? ?//主動和服務(wù)器建立連接
?? ?tcpSocket->connectToHost(ip, portName);
}

void ClientWidget::onTextEditRead()
{
?? ?TextEditRead->setText(QString::fromLocal8Bit("成功和服務(wù)器建立好連接?。?!"));

?? ?connectBtn->setEnabled(false);
?? ?ButtonDisconnect->setEnabled(true);

}

void ClientWidget::onButtonSendClicked()
{
?? ?if (tcpSocket == NULL)
?? ?{
?? ??? ?return;
?? ?}
?? ?//獲取編輯框內(nèi)容
?? ?QString str = TextEditWrite->toPlainText();

?? ?//發(fā)送文本內(nèi)容
?? ?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);

?? ?//追加到讀取編輯區(qū)中
?? ?TextEditRead->append(str);
}

void ClientWidget::onDisConnect()
{
?? ?if (tcpSocket == NULL)
?? ?{
?? ??? ?return;
?? ?}

?? ?tcpSocket->disconnectFromHost();

?? ?tcpSocket->close();
?? ?connectBtn->setEnabled(true);

?? ?ButtonDisconnect->setEnabled(false);
?? ?
?? ?TextEditRead->setText(QString::fromLocal8Bit("連接已斷開?。。?));
}

服務(wù)器

(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"服務(wù)器(端口:8888)"));
?? ?//箭筒套接字,指定父對象,讓其自動回收空間
?? ?tcpServer = new QTcpServer(this);
?? ?//監(jiān)聽并綁定端口
?? ?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;
?? ?}
?? ?//獲取編輯區(qū)的內(nèi)容
?? ?QString str = ui.textWrite->toPlainText();
?? ?//給對方發(fā)送數(shù)據(jù)。使用套接字是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()
{
?? ?//從通信套接字中取出內(nèi)容
?? ?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("連接已斷開?。?!"));

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言超詳細(xì)文件操作基礎(chǔ)下篇

    C語言超詳細(xì)文件操作基礎(chǔ)下篇

    這篇文章主要為大家詳細(xì)介紹了C語言的文件操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C++實現(xiàn)讀入二進制數(shù)并轉(zhuǎn)換為十進制輸出

    C++實現(xiàn)讀入二進制數(shù)并轉(zhuǎn)換為十進制輸出

    本文給大家介紹的是一則使用C++實現(xiàn)讀入二進制數(shù)并轉(zhuǎn)換為十進制輸出的代碼,實現(xiàn)起來其實非常簡單,C++本身就提供了二進制類庫的,大家看代碼吧,簡單又實用。
    2015-03-03
  • C語言函數(shù)超詳細(xì)講解下篇

    C語言函數(shù)超詳細(xì)講解下篇

    函數(shù)是一組一起執(zhí)行一個任務(wù)的語句。每個?C?程序都至少有一個函數(shù),即主函數(shù)?main()?,所有簡單的程序都可以定義其他額外的函數(shù),函數(shù)我們分兩篇來講解,接下來開始第二篇
    2022-04-04
  • 詳解C++中多態(tài)的底層原理

    詳解C++中多態(tài)的底層原理

    要了解C++多態(tài)的底層原理需要我們對C指針有著深入的了解,這個在打印虛表的時候就可以見功底,所以快來跟隨小編一起學(xué)習(xí)一下吧
    2022-04-04
  • C語言 typedef:給類型起一個別名

    C語言 typedef:給類型起一個別名

    本文主要介紹C語言 typedef,這里整理了相關(guān)資料及簡單示例代碼幫助大家學(xué)習(xí)理解,有興趣的小伙伴可以參考下
    2016-08-08
  • C語言編程數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)詳解小白篇

    C語言編程數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)詳解小白篇

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ),非常適合初學(xué)數(shù)據(jù)結(jié)構(gòu)的小白,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家多多進步,早日升職加薪
    2021-09-09
  • 淺談C++流庫的基本結(jié)構(gòu)

    淺談C++流庫的基本結(jié)構(gòu)

    本文主要介紹了淺談C++流庫的基本結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Windows程序內(nèi)部運行機制實例詳解

    Windows程序內(nèi)部運行機制實例詳解

    這篇文章主要介紹了Windows程序內(nèi)部運行機制實例詳解,對于學(xué)習(xí)Windows程序設(shè)計來說是非常重要的一課,需要的朋友可以參考下
    2014-08-08
  • 使用Visual Studio 2010/2013編譯V8引擎步驟分享

    使用Visual Studio 2010/2013編譯V8引擎步驟分享

    這篇文章主要介紹了使用Visual Studio 2013編譯V8引擎步驟分享,需要的朋友可以參考下
    2015-08-08
  • C++整數(shù)拼接技巧大揭秘

    C++整數(shù)拼接技巧大揭秘

    C++整數(shù)拼接技巧大揭秘,讓你的代碼更簡潔高效!你是否還在為如何優(yōu)雅地將整數(shù)拼接成字符串而煩惱?本指南將為你揭示C++中最實用、最酷炫的整數(shù)拼接技巧,助你提升編程技能,需要的朋友可以參考下
    2024-03-03

最新評論