1.实现效果
2.服务端
mainwindow.h
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit_Port->setText("8765");
ui->pushButton_Send->setEnabled(false);
server=new QTcpServer();
//连接信号槽
connect(server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect);
}
MainWindow::~MainWindow()
{
server->close();
server->deleteLater();
delete ui;
}
void MainWindow::on_pushButton_Listen_clicked()
{
if(ui->pushButton_Listen->text()==tr("侦听"))
{
//从输入框里获取端口号
int port=ui->lineEdit_Port->text().toInt();
//监听指定的端口
if(!server->listen(QHostAddress::Any,port))
{
//若出错,则输出错误信息
qDebug()<<server->errorString();
return;
}
//修改按键文字
ui->pushButton_Listen->setText("取消侦听");
qDebug()<<"Listen successfully!!!";
}
else
{
//如果正在连接
if(socket->state()==QAbstractSocket::ConnectedState)
{
//关闭连接
socket->disconnectFromHost();
}
//取消侦听
socket->close();
//修改按键文字
ui->pushButton_Listen->setText("帧听");
//发送按键失能
ui->pushButton_Send->setEnabled(false);
}
}
void MainWindow::on_pushButton_Send_clicked()
{
qDebug() <<"Send: "<<ui->textEdit_Send->toPlainText(); //简单文本
//获取文本框内容并以ASCII码形式发送
socket->write(ui->textEdit_Send->toPlainText().toLatin1()); //ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF。亦称为Latin1。
socket->flush();//清空缓冲区数据,
}
void MainWindow::server_New_Connect()
{
//获取客户端连接
socket=server->nextPendingConnection();
//连接QTcpSocket的信号槽,以便读取新数据
QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
QObject::connect(socket,&QTcpSocket::disconnected,this,&MainWindow::socket_Disconnected);
//发送按键使能
ui->pushButton_Send->setEnabled(true);
}
void MainWindow::socket_Read_Data()
{
QByteArray buffer;
//读取缓冲区数据
buffer=socket->readAll();
if(!buffer.isEmpty())
{
QString str=ui->textEdit_Recv->toPlainText();
str+=tr(buffer);
ui->textEdit_Recv->setText(str);
}
}
void MainWindow::socket_Disconnected()
{
//发送按键失能
ui->pushButton_Send->setEnabled(false);
qDebug()<<"Disconnected!!";
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit_Port->setText("8765");
ui->pushButton_Send->setEnabled(false);
server=new QTcpServer();
//连接信号槽
connect(server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect);
}
MainWindow::~MainWindow()
{
server->close();
server->deleteLater();
delete ui;
}
void MainWindow::on_pushButton_Listen_clicked()
{
if(ui->pushButton_Listen->text()==tr("侦听"))
{
//从输入框里获取端口号
int port=ui->lineEdit_Port->text().toInt();
//监听指定的端口
if(!server->listen(QHostAddress::Any,port))
{
//若出错,则输出错误信息
qDebug()<<server->errorString();
return;
}
//修改按键文字
ui->pushButton_Listen->setText("取消侦听");
qDebug()<<"Listen successfully!!!";
}
else
{
//如果正在连接
if(socket->state()==QAbstractSocket::ConnectedState)
{
//关闭连接
socket->disconnectFromHost();
}
//取消侦听
socket->close();
//修改按键文字
ui->pushButton_Listen->setText("帧听");
//发送按键失能
ui->pushButton_Send->setEnabled(false);
}
}
void MainWindow::on_pushButton_Send_clicked()
{
qDebug() <<"Send: "<<ui->textEdit_Send->toPlainText(); //简单文本
//获取文本框内容并以ASCII码形式发送
socket->write(ui->textEdit_Send->toPlainText().toLatin1()); //ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF。亦称为Latin1。
socket->flush();//清空缓冲区数据,
}
void MainWindow::server_New_Connect()
{
//获取客户端连接
socket=server->nextPendingConnection();
//连接QTcpSocket的信号槽,以便读取新数据
QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
QObject::connect(socket,&QTcpSocket::disconnected,this,&MainWindow::socket_Disconnected);
//发送按键使能
ui->pushButton_Send->setEnabled(true);
}
void MainWindow::socket_Read_Data()
{
QByteArray buffer;
//读取缓冲区数据
buffer=socket->readAll();
if(!buffer.isEmpty())
{
QString str=ui->textEdit_Recv->toPlainText();
str+=tr(buffer);
ui->textEdit_Recv->setText(str);
}
}
void MainWindow::socket_Disconnected()
{
//发送按键失能
ui->pushButton_Send->setEnabled(false);
qDebug()<<"Disconnected!!";
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.ui
3.客户端
mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTcpSocket *socket;
private slots:
void on_pushButton_Connect_clicked();
void on_pushButton_Send_clicked();
void socket_Read_Data();
void socket_Disconnected();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
socket=new QTcpSocket();
//连接信号槽
QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
QObject::connect(socket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);
ui->pushButton_Send->setEnabled(false);
ui->lineEdit_IP->setText("127.0.0.1");
ui->lineEdit_Port->setText("8765");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::socket_Read_Data()
{
QByteArray buffer;
//读取缓冲区数据
buffer=socket->readAll();
if(!buffer.isEmpty())
{
QString str=ui->textEdit_Recv->toPlainText();
str+=tr(buffer);
//刷新显示
ui->textEdit_Recv->setText(str);
}
}
void MainWindow::socket_Disconnected()
{
//发送按键失能
ui->pushButton_Send->setEnabled(false);
//修改按键文字
ui->pushButton_Connect->setText("连接");
qDebug()<<"Disconnected!!!";
}
void MainWindow::on_pushButton_Connect_clicked()
{
if(ui->pushButton_Connect->text()==tr("连接"))
{
QString IP;
int port;
//获取IP地址
IP=ui->lineEdit_IP->text();
//获取端口号
port=ui->lineEdit_Port->text().toInt();
//取消已有的连接
socket->abort();
//连接服务器
socket->connectToHost(IP,port);
//等待连接成功
if(!socket->waitForConnected(30000))
{
qDebug()<<"Connection failed!";
return;
}
qDebug()<<"Connect successfuly!";
//发送按键使能
ui->pushButton_Send->setEnabled(true);
//修改按键文字
ui->pushButton_Connect->setText("断开连接");
}
else
{
//断开连接
socket->disconnectFromHost();
//修改按键文字
ui->pushButton_Connect->setText("连接");
ui->pushButton_Send->setEnabled(false);
}
}
void MainWindow::on_pushButton_Send_clicked()
{
qDebug() << "Send: " << ui->textEdit_Send->toPlainText();
//获取文本框内容并以ASCII码形式发送
socket->write(ui->textEdit_Send->toPlainText().toLatin1());
socket->flush();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}