客户端:
Client.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_Client.h"
#include <QTcpSocket>//通信套接字
class Client : public QWidget
{
Q_OBJECT
public:
Client(QWidget *parent = Q_NULLPTR);
~Client();
private:
Ui::ClientClass ui;
QTcpSocket* m_tcpSocket;
QString m_msg;
QString m_recMsg;
public slots:
void doConnected();
void receiveMsg();
private slots:
void on_pbtConnect_clicked();
void on_pbtSend_clicked();
void on_pbtClose_clicked();
};
Client.cpp
#include "Client.h"
#include <QMessageBox>
#include <QHostAddress>
#include <QDebug>
#define cout qDebug()<<"[" <<__FILE__<<":"<<__LINE__<<"]"
#pragma execution_character_set("utf-8")
Client::Client(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setWindowTitle("客户端");
m_recMsg.clear();
m_tcpSocket = NULL;
m_tcpSocket = new QTcpSocket(this);
connect(m_tcpSocket, SIGNAL(connected()), this, SLOT(doConnected()));
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(receiveMsg()));
}
Client::~Client()
{
}
void Client::doConnected()
{
m_recMsg = "成功和服务器连接";
ui.textBrowser->setText(m_recMsg);
}
void Client::receiveMsg()
{
QByteArray array = m_tcpSocket->readAll();
QString str = array;
m_msg += str + "\r\n";
ui.textBrowser->setText(m_msg);
ui.textBrowser->moveCursor(QTextCursor::End);
}
void Client::on_pbtConnect_clicked()
{
if (m_tcpSocket == NULL)
m_tcpSocket = new QTcpSocket(this);
QString ip = ui.letIP->text();
qint16 port = ui.letPort->text().toInt();
m_tcpSocket->connectToHost(QHostAddress(ip), port);
}
void Client::on_pbtSend_clicked()
{
if (m_tcpSocket == NULL)
{
ui.textBrowser->setText("当前无连接");
return;
}
QString str = ui.textEdit->toPlainText();
m_tcpSocket->write(str.toUtf8().data());
}
void Client::on_pbtClose_clicked()
{
if (m_tcpSocket == NULL)
{
ui.textBrowser->setText("当前无连接");
return;
}
//主动和客户端断开连接
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
m_tcpSocket = NULL;
}
服务端:
Service.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_Service.h"
#include <QTcpServer>//监听套接字
#include <QTcpSocket>//通信套接字
class Service : public QWidget
{
Q_OBJECT
public:
Service(QWidget *parent = Q_NULLPTR);
~Service();
public slots:
void serverConnect();
void readMsg();
public slots:
void on_pbtSend_clicked();
void on_pbtClose_clicked();
private:
Ui::ServiceClass ui;
QTcpServer* m_tcpServer;
QTcpSocket* m_tcpSocket;
QString m_msg;
QString m_recMsg;
};
Service.cpp
#include "Service.h"
#include <QMessageBox>
#include <QDebug>
#define cout qDebug()<<"[" <<__FILE__<<":"<<__LINE__<<"]"
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
Service::Service(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
m_msg.clear();
m_recMsg.clear();
m_tcpServer = NULL;
m_tcpSocket = NULL;
m_tcpServer = new QTcpServer(this);
setWindowTitle("服务器");
m_tcpServer->listen(QHostAddress::Any, 8888);
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(serverConnect()));
}
Service::~Service()
{
}
void Service::serverConnect()
{
m_tcpSocket = m_tcpServer->nextPendingConnection();
QString ip = m_tcpSocket->peerAddress().toString();
int port = m_tcpSocket->peerPort();
QString temp = QString("[%1:%2]:连接成功").arg(ip).arg(port);
QString title = QString("服务器 ip%1:%2").arg(ip).arg(port);
setWindowTitle(title);
m_msg += temp + "\r\n";
ui.textBrowser->setText(m_msg);
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(readMsg()));
}
void Service::readMsg()
{
QByteArray array = m_tcpSocket->readAll();
QString str = array;
m_msg += str + "\r\n";
ui.textBrowser->setText(m_msg);
ui.textBrowser->moveCursor(QTextCursor::End);
}
void Service::on_pbtSend_clicked()
{
if (m_tcpSocket == NULL)
{
ui.textBrowser->setText("当前无连接");
return;
}
QString str = ui.textEdit->toPlainText();
m_tcpSocket->write(str.toUtf8().data());
}
void Service::on_pbtClose_clicked()
{
if (m_tcpSocket == NULL)
{
ui.textBrowser->setText("当前无连接");
return;
}
//主动和客户端断开连接
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
// m_tcpSocket=NULL;
}