1、编译环境
Qt5.6+VS2013
1、运用VS创建Qt项目时,选择Network:
注:用QtCreator建立项目,需要在.pro文件中,引入NetWork模块
2、服务器端代码
sever_tcp.h
#ifndef SEVER_TCP_H
#define SEVER_TCP_H
#include <QtWidgets/QMainWindow>
#include "ui_sever_tcp.h"
#include<QtNetwork/QtNetwork>
class sever_tcp : public QMainWindow
{
Q_OBJECT
public:
sever_tcp(QWidget *parent = 0);
~sever_tcp();
QTcpSocket* m_socket = NULL;//定义通讯套接字
QTcpServer* m_server = NULL;
void Server_New_Connect();//收到客户端连接请求 处理
void socket_Read_Data();//收到客户端发送消息处理
void socket_Disconnect();//客户端断开连接处理
private slots:
void My_Server_Start();
void My_Send_Info();
private:
Ui::sever_tcpClass ui;
};
#endif // SEVER_TCP_H
sever_tcp.cpp
#include "sever_tcp.h"
#pragma execution_character_set("utf-8")
sever_tcp::sever_tcp(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_server = new QTcpServer();
m_socket = new QTcpSocket();
connect(this->m_server, &QTcpServer::newConnection, this, &sever_tcp::Server_New_Connect);//将服务器端收到新连接的信号绑定槽函数
}
void sever_tcp::My_Server_Start()
{
if (ui.Server_Start_Bt->text() == "开启")
{
qDebug() << "启动服务器";
QHostAddress IP("192.168.1.53");//绑定服务器的IP地址
quint16 server_port = 8002;//设置监听端口
if (m_server->listen(IP, server_port))//判断监听端口是否正常打开
{
qDebug() << "开启监听";
ui.Server_Start_Bt->setText("关闭");
}
else
{
qDebug() << "监听失败";
ui.Server_Start_Bt->setText("开启");
}
return;
}
if (ui.Server_Start_Bt->text() == "关闭")
{
qDebug() << "关闭连接";
ui.Server_Start_Bt->setText("开启");
}
return;
}
void sever_tcp::Server_New_Connect()
{
qDebug() << "收到新连接";
m_socket = m_server->nextPendingConnection();//获取客户端连接
QObject::connect(m_socket, &QTcpSocket::readyRead, this, &sever_tcp::socket_Read_Data);//绑定客户发送消息信号,及客户端发送信息后,触发readyRead信号
QObject::connect(m_socket, &QTcpSocket::disconnected, this, &sever_tcp::socket_Disconnect);//绑定客户端断开请求信号,客户端断开连接后,触发disconnected信号
}
void sever_tcp::socket_Read_Data()
{
qDebug() << "收送消息";
QString buffer;
buffer = m_socket->readAll();//读取客户端发送的消息
ui.plainTextEdit->setPlainText(buffer);
}
void sever_tcp::socket_Disconnect()
{
qDebug() << "客户端断开连接";
}
void sever_tcp::My_Send_Info()
{
qDebug() << "发送消息";
QString buffer = ui.textEdit->toPlainText();
qint64 writeResult = m_socket->write(buffer.toLatin1()+'\n');//向客户端发送消息,如果客户端用readLine()读取消息,末尾一定要加上'\n'
bool BoolFlush = m_socket->flush();//刷新socket的缓存区
if (writeResult != -1 && BoolFlush == 1)
{
qDebug() << "发送消息成功";
}
}
sever_tcp::~sever_tcp()
{
m_server->close();
m_server->deleteLater();
}
3、客户端代码
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QtWidgets/QMainWindow>
#include "ui_client.h"
#include<QtNetwork/QtNetwork>
class client : public QMainWindow
{
Q_OBJECT
public:
client(QWidget *parent = 0);
~client();
QTcpSocket* m_socket=NULL;
private slots:
void connect_server();
void send_server();
void readyread();
private:
Ui::clientClass ui;
};
#endif // CLIENT_H
client.cpp
#include "client.h"
#include<iostream>
#include<stdio.h>
#include<qdebug.h>
#pragma execution_character_set("utf-8")
client::client(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_socket = new QTcpSocket;
//绑定接收到服务器消息信号,服务器发送消息后触发readyRead信号
connect(this->m_socket, SIGNAL(readyRead()), this, SLOT(readyread()));
//connect(m_socket, &QTcpSocket::readyRead, this, &client::readyread);
qDebug() << "你好";
}
void client::readyread()
{
QString buffer;
buffer = m_socket->readAll();//读取服务器数据
qDebug() << buffer;
qDebug() << "read_server";
}
void client::connect_server()
{
m_socket->connectToHost("192.168.1.53", 8002);//向服务器发起连接请求
qDebug() << "connect_server";
}
void client::send_server()
{
m_socket->write("你好服务器\n");//向服务器发送消息
qDebug() << "send_server";
}
client::~client()
{
}