一、导入pro依赖
二、例子
(一)完整程序运行图
(二)核心代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <qtcpserver.h>
class Login;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void initUI();
void initConnect();
void networkServerTest();
void networkClientTest();
private:
Ui::MainWindow *ui;
Login *login;
QTcpServer *server;
QTcpSocket *client;
QMap<QString,QTcpSocket *> *session;
};
#endif
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QDebug>
#include <QPaintEvent>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QMap>
#include <QStringListModel>
#include "login.h"
#include "sqlutil.h"
#include "handle/loginhandle.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
login(new Login())
{
ui->setupUi(this);
initUI();
initConnect();
session = new QMap<QString,QTcpSocket *>();
networkServerTest();
networkClientTest();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initUI()
{
if(LoginHandle::getLoginHandle()->isLogin()){
ui->goLogin->hide();
ui->userCenter->show();
ui->logout->show();
ui->userCenter->setText(LoginHandle::getLoginHandle()->getUserInfo().getUserName());
}else {
ui->goLogin->show();
ui->logout->hide();
ui->userCenter->hide();
}
}
void MainWindow::initConnect()
{
connect(ui->goLogin,&QPushButton::clicked,[=]{
login->show();
});
connect(ui->logout,&QPushButton::clicked,[=]{
LoginHandle::getLoginHandle()->logout();
initUI();
});
connect(ui->startServer,&QPushButton::clicked,[=]{
if(ui->serverPort->text()==""){
QMessageBox::information(this, "提示", "端口不能为空",QMessageBox::Ok);
}else {
int port = ui->serverPort->text().toInt();
bool isListening = server->listen(QHostAddress::LocalHost,port);
if(isListening){
ui->stopServer->setEnabled(true);
ui->startServer->setEnabled(false);
ui->serverPort->setEnabled(false);
ui->timeout->setEnabled(false);
ui->textServer->append(">>服务器启动成功");
}else {
ui->textServer->append(">>服务器启动失败");
qDebug()<< server->errorString();
}
}
});
connect(ui->stopServer,&QPushButton::clicked,[=]{
if(server->isListening()){
server->close();
ui->stopServer->setEnabled(false);
ui->startServer->setEnabled(true);
ui->serverPort->setEnabled(true);
ui->timeout->setEnabled(true);
ui->textServer->append(">>服务器已停止运行");
}else {
ui->textServer->append(">>服务器未停止运行");
}
});
connect(ui->linkServer,&QPushButton::clicked,[=]{
QString ip = ui->ip->text();
int port = ui->port->text().toInt();
if(ip==""||ui->port->text()==""){
QMessageBox::information(this, "提示", "不能为空",QMessageBox::Ok);
}else {
client->connectToHost(ip,port);
}
});
connect(ui->closeLink,&QPushButton::clicked,[=]{
client->close();
});
connect(ui->send,&QPushButton::clicked,[=]{
QString msg = ui->msgBody->toPlainText();
if(client->isOpen()){
client->write(msg.toUtf8());
}else {
qDebug()<< "连接已断开";
}
});
connect(ui->clearClient,&QPushButton::clicked,[=]{
ui->textClient->clear();
});
connect(ui->clearServer,&QPushButton::clicked,[=]{
ui->textServer->clear();
});
}
void MainWindow::networkServerTest()
{
server = new QTcpServer();
connect(server, &QTcpServer::newConnection, [=]{
QTcpSocket *tcpLink = server->nextPendingConnection();
QString key = tcpLink->peerAddress().toString();
session->insert(key,tcpLink);
qDebug()<< tcpLink->peerAddress();
qDebug()<< tcpLink->peerName();
qDebug()<< tcpLink->peerPort();
connect(tcpLink, &QTcpSocket::readyRead, this, [=]()
{
QByteArray msg= tcpLink->readAll();
ui->textServer->append(msg);
});
connect(tcpLink, &QTcpSocket::disconnected, this, [=]()
{
ui->textServer->append(">>客户端断开连接");
});
});
connect(server,&QTcpServer::acceptError,[=](QAbstractSocket::SocketError socketError){
if(socketError==QAbstractSocket::SocketError(1)){
qDebug()<< "发生错误";
}
});
}
void MainWindow::networkClientTest()
{
client = new QTcpSocket();
connect(client, &QTcpSocket::connected, [=]{
ui->textClient->append(">>成功连接服务器");
ui->linkServer->setEnabled(false);
ui->closeLink->setEnabled(true);
ui->ip->setEnabled(false);
ui->port->setEnabled(false);
});
connect(client, &QTcpSocket::readyRead, [=](){
QByteArray msg= client->readAll();
ui->textClient->append(msg);
});
connect(client, &QTcpSocket::disconnected, [=](){
ui->textClient->append(">>连接已断开");
ui->linkServer->setEnabled(true);
ui->closeLink->setEnabled(false);
ui->ip->setEnabled(true);
ui->port->setEnabled(true);
});
connect(client, &QTcpSocket::hostFound, [=](){
ui->textClient->append(">>连接的服务器存在,等待建立连接");
});
}
(三)完整代码
代码路径