1、server端创建QTcpServer对象,绑定ip和端口;
2、启动监听,接收客户端连接请求;
3、接收客户端连接请求后,获得此次连接的新套接字,建立了连接;
4、通过套接字进行相互通信;
5、关闭套接字;
1、创建套接字;
2、向服务器发起连接请求;
3、通过套接字和服务器进行通信;
4、关闭套接字;
QT += core gui network
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include "server.h"
#include "ui_server.h"
#include "client.h"
#include "ui_client.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_b_server_client_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("TCP");
this->setWindowIcon(QIcon(":/TCP.png"));
this->setFixedSize(600,400);
}
Widget::~Widget()
{
delete ui;
}
//同时打开服务器和客户端
void Widget::on_b_server_client_clicked()
{
server *server_win = new server;
server_win->show(); //打开服务器窗口
client *client_win = new client;
client_win->show(); //打开客户端窗口
this->close(); //关闭当前窗口
}
#ifndef SERVER_H
#define SERVER_H
#include "widget.h"
#include "ui_widget.h"
#include
namespace Ui {
class server;
}
class server : public QWidget
{
Q_OBJECT
public:
explicit server(QWidget *parent = 0);
~server();
private slots:
void on_b_send_clicked();
void new_connect();
void on_b_return_clicked();
private:
Ui::server *ui;
QTcpServer *myserver;
QTcpSocket *socket;
};
#endif // SERVER_H
#include "server.h"
#include "ui_server.h"
server::server(QWidget *parent) :
QWidget(parent),
ui(new Ui::server)
{
ui->setupUi(this);
this->setWindowTitle("服务器");
this->setWindowIcon(QIcon(":/服务器.png"));
this->setFixedSize(600,400);
myserver = new QTcpServer(this);
myserver->listen(QHostAddress::Any,8888);
connect(myserver,&QTcpServer::newConnection,this,&server::new_connect);
}
server::~server()
{
delete ui;
}
void server::on_b_send_clicked()
{
QString str = ui->lineEdit_send->text();
socket->write(str.toUtf8());
}
void server::new_connect()
{
//获取套接字
socket = myserver->nextPendingConnection();
ui->label_conn->setText("连接成功");
connect(socket,&QTcpSocket::readyRead,this,[=]()
{
QString data = socket->readAll();
ui->textEdit_receive->setText(data);
});
connect(socket,&QTcpSocket::disconnected,this,[=]()
{
ui->label_conn->setText("没有连接");
socket->close();
socket->deleteLater();
});
}
void server::on_b_return_clicked()
{
Widget *win = new Widget;
win->show(); //打开主界面窗口
this->close(); //关闭当前窗口
}
#ifndef CLIENT_H
#define CLIENT_H
#include
#include "widget.h"
#include "ui_widget.h"
namespace Ui {
class client;
}
class client : public QWidget
{
Q_OBJECT
public:
explicit client(QWidget *parent = 0);
~client();
private slots:
void readMessage();
void on_b_conn_clicked();
void on_b_disconn_clicked();
void on_b_send_clicked();
void on_b_all_clicked();
void on_b_clear_clicked();
void on_b_return_clicked();
private:
Ui::client *ui;
QTcpSocket *socket;
QList DataList;
QString message;
quint16 blockSize;
};
#endif // CLIENT_H
#include "client.h"
#include "ui_client.h"
client::client(QWidget *parent) :
QWidget(parent),
ui(new Ui::client)
{
ui->setupUi(this);
this->setWindowTitle("客户端");
this->setWindowIcon(QIcon(":/客户端.png"));
this->setFixedSize(600,400);
socket = new QTcpSocket(this);
ui->lineEdit_IP->setText("127.0.0.1");
ui->lineEdit_port->setText("8888");
connect(socket,&QTcpSocket::readyRead,this,&client::readMessage);
connect(socket,&QTcpSocket::disconnected,this,[=]()
{
ui->label_conn->setText("已断开");
});
connect(socket,&QTcpSocket::connected,this,[=]()
{
ui->label_conn->setText("已连接");
});
}
client::~client()
{
delete ui;
}
void client::readMessage()
{
QString message = socket->readAll();
DataList.push_back(message);
ui->textEdit_receive->setText(message);
}
void client::on_b_conn_clicked()
{
socket->connectToHost(ui->lineEdit_IP->text(),
ui->lineEdit_port->text().toInt());
}
void client::on_b_disconn_clicked()
{
socket->disconnectFromHost();
}
void client::on_b_send_clicked()
{
QString str = ui->lineEdit_send->text();
socket->write(str.toUtf8());
}
void client::on_b_all_clicked()
{
QString str;
for(auto mess:DataList)
{
str += mess + '\n';
}
QMessageBox::information(nullptr,"信息",str);
}
void client::on_b_clear_clicked()
{
DataList.clear();
QMessageBox::information(nullptr,"信息","已清空");
}
void client::on_b_return_clicked()
{
Widget *win = new Widget;
win->show(); //打开主界面窗口
this->close(); //关闭当前窗口
}
2、点击”打开服务器和客户端“按钮,进行连接和相互通信