原文:https://blog.csdn.net/uyy203/article/details/54960276
在Qt上建立Tcp server 和 client 間的簡易通訊蝴乔,實(shí)現(xiàn)效果如下
image.png
首先要記得在工程目錄中的pro文件中 加入 傲醉,這樣才能開啟網(wǎng)絡(luò)服務(wù)
QT += network
//mainwindow.h
#include "tcpserverwindow.h"
#include "ui_tcpserverwindow.h"
TcpServerWindow::TcpServerWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TcpServerWindow)
{
ui->setupUi(this);
tcpSocket=new QTcpSocket(this);
tcpServer=new QTcpServer(this);
setListener();
/**信號與槽**/
//newConnection()用于當(dāng)有客戶端訪問時發(fā)出信號,acceptConnection()信號處理函數(shù)
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
//當(dāng)tcpSocket在接受客戶端連接時出現(xiàn)錯誤時另锋,displayError(QAbstractSocket::SocketError)進(jìn)行錯誤提醒并關(guān)閉tcpSocket总处。
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(displayError(QAbstractSocket::SocketError)));
}
//監(jiān)聽是否有客戶端來訪狈惫,且對任何來訪者監(jiān)聽,端口為6666
void TcpServerWindow::setListener(){
if(!tcpServer->listen(QHostAddress::Any,6666)){
qDebug()<<tcpServer->errorString();
close();
}else {
qDebug()<<"listening";
}
}
void TcpServerWindow::displayError(QAbstractSocket::SocketError)
{
qDebug()<<tcpSocket->errorString();
tcpSocket->close();
}
void TcpServerWindow::acceptConnection(){
tcpSocket=tcpServer->nextPendingConnection();
}
TcpServerWindow::~TcpServerWindow()
{
delete ui;
}
//點(diǎn)擊發(fā)送按鈕發(fā)送消息
void TcpServerWindow::on_SendBtn_clicked()
{
//以utf-8字符集格式發(fā)送鹦马,支持中文
tcpSocket->write(ui->SendText->text().toUtf8());
//以拉丁字符集格式發(fā)送
// tcpSocket->write(ui->SendText->text().toLatin1());
}
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tcpServerWindow=new TcpServerWindow;
tcpClientWindow=new TcpClientWindow;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_server_clicked()
{
tcpServerWindow->show();
}
void MainWindow::on_client_clicked()
{
tcpClientWindow->show();
}
*************Client***************
//tcpclientwindow.h
#ifndef TCPCLIENTWINDOW_H
#define TCPCLIENTWINDOW_H
#include<QtNetwork/QTcpSocket>
#include<QtNetwork>
#include <QDialog>
namespace Ui {
class TcpClientWindow;
}
class TcpClientWindow : public QDialog
{
Q_OBJECT
public:
explicit TcpClientWindow(QWidget *parent = 0);
~TcpClientWindow();
private:
Ui::TcpClientWindow *ui;
QTcpSocket *tcpSocket;
void newTcpConnection();
private slots:
void displayError(QAbstractSocket::SocketError);
void revData();
};
#endif // TCPCLIENTWINDOW_H
//tcpclientwindow.cpp
#include "tcpclientwindow.h"
#include "ui_tcpclientwindow.h"
TcpClientWindow::TcpClientWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TcpClientWindow)
{
ui->setupUi(this);
tcpSocket=new QTcpSocket(this);
newTcpConnection();
/**信號與槽**/
//readyRead()表示服務(wù)端發(fā)送數(shù)據(jù)過來即發(fā)動信號胧谈,接著revData()進(jìn)行處理。
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(revData()));
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
}
//接收字符串
void TcpClientWindow::revData(){
QString data=tcpSocket->readAll();
ui->ReceiveText->setText(data);
}
//新建連接
void TcpClientWindow::newTcpConnection(){
tcpSocket->abort();
tcpSocket->connectToHost("127.0.0.1",6666);
}
void TcpClientWindow::displayError(QAbstractSocket::SocketError){
qDebug()<<tcpSocket->errorString();
tcpSocket->close();
}
TcpClientWindow::~TcpClientWindow()
{
delete ui;
}
***************Server***************
//tcpserverwindow.h
#ifndef TCPSERVERWINDOW_H
#define TCPSERVERWINDOW_H
#include<QtNetwork/QTcpSocket>
#include<QtNetwork/QTcpServer>
#include <QDialog>
namespace Ui {
class TcpServerWindow;
}
class TcpServerWindow : public QDialog
{
Q_OBJECT
public:
explicit TcpServerWindow(QWidget *parent = 0);
~TcpServerWindow();
private:
Ui::TcpServerWindow *ui;
QTcpSocket *tcpSocket;
QTcpServer *tcpServer;
void setListener();
private slots:
void acceptConnection();
void displayError(QAbstractSocket::SocketError);
void on_SendBtn_clicked();
};
#endif // TCPSERVERWINDOW_H
//tcpserverwindow.cpp
#include "tcpserverwindow.h"
#include "ui_tcpserverwindow.h"
TcpServerWindow::TcpServerWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TcpServerWindow)
{
ui->setupUi(this);
tcpSocket=new QTcpSocket(this);
tcpServer=new QTcpServer(this);
setListener();
/**信號與槽**/
//newConnection()用于當(dāng)有客戶端訪問時發(fā)出信號荸频,acceptConnection()信號處理函數(shù)
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
//當(dāng)tcpSocket在接受客戶端連接時出現(xiàn)錯誤時菱肖,displayError(QAbstractSocket::SocketError)進(jìn)行錯誤提醒并關(guān)閉tcpSocket。
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(displayError(QAbstractSocket::SocketError)));
}
//監(jiān)聽是否有客戶端來訪旭从,且對任何來訪者監(jiān)聽稳强,端口為6666
void TcpServerWindow::setListener(){
if(!tcpServer->listen(QHostAddress::Any,6666)){
qDebug()<<tcpServer->errorString();
close();
}else {
qDebug()<<"listening";
}
}
void TcpServerWindow::displayError(QAbstractSocket::SocketError)
{
qDebug()<<tcpSocket->errorString();
tcpSocket->close();
}
void TcpServerWindow::acceptConnection(){
tcpSocket=tcpServer->nextPendingConnection();
}
TcpServerWindow::~TcpServerWindow()
{
delete ui;
}
//點(diǎn)擊發(fā)送按鈕發(fā)送消息
void TcpServerWindow::on_SendBtn_clicked()
{
//以utf-8字符集格式發(fā)送,支持中文
tcpSocket->write(ui->SendText->text().toUtf8());
//以拉丁字符集格式發(fā)送
// tcpSocket->write(ui->SendText->text().toLatin1());
}