QT实现网络编程---客户端、服务端

客户端:
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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值