原文:https://blog.csdn.net/uyy203/article/details/54960276
在Qt上建立Tcp server 和 client 间的简易通讯,实现效果如下
首先要记得在工程目录中的pro文件中 加入 ,这样才能开启网络服务
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()用于当有客户端访问时发出信号,acceptConnection()信号处理函数
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
//当tcpSocket在接受客户端连接时出现错误时,displayError(QAbstractSocket::SocketError)进行错误提醒并关闭tcpSocket。
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(displayError(QAbstractSocket::SocketError)));
}
//监听是否有客户端来访,且对任何来访者监听,端口为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;
}
//点击发送按钮发送消息
void TcpServerWindow::on_SendBtn_clicked()
{
//以utf-8字符集格式发送,支持中文
tcpSocket->write(ui->SendText->text().toUtf8());
//以拉丁字符集格式发送
// 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()表示服务端发送数据过来即发动信号,接着revData()进行处理。
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()用于当有客户端访问时发出信号,acceptConnection()信号处理函数
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
//当tcpSocket在接受客户端连接时出现错误时,displayError(QAbstractSocket::SocketError)进行错误提醒并关闭tcpSocket。
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(displayError(QAbstractSocket::SocketError)));
}
//监听是否有客户端来访,且对任何来访者监听,端口为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;
}
//点击发送按钮发送消息
void TcpServerWindow::on_SendBtn_clicked()
{
//以utf-8字符集格式发送,支持中文
tcpSocket->write(ui->SendText->text().toUtf8());
//以拉丁字符集格式发送
// tcpSocket->write(ui->SendText->text().toLatin1());
}