server.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include <QDateTime>
#define PORT 8080
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void newClientHander(); //建立tcp连接的槽函数
void clientInfoSlot(); //收到客户端信息显示 槽函数
void on_pushButton_clicked(); //发送信息的按钮 槽函数
private:
Ui::Widget *ui;
QTcpServer *server;
QTcpSocket *socket;
};
#endif // WIDGET_H
server.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("服务器server");
server = new QTcpServer;
server->listen(QHostAddress::AnyIPv4,PORT);
//客户端发起连接,server发出一个信号
connect(server,&QTcpServer::newConnection,this,&Widget::newClientHander);
}
Widget::~Widget()
{
delete ui;
}
//建立tcp连接,显示客户端ip和端口号
void Widget::newClientHander()
{
//建立TCP连接
socket = server->nextPendingConnection();
//获取ip 和 端口号
socket->peerAddress();
socket->peerPort();
//显示客户端ip和端口号
ui->iplineEidt ->setText(socket->peerAddress().toString());
ui->portlineEdit->setText(QString::number(socket->peerPort()));
//服务器收到客户端发送的信息,socket发出readyread信号
connect(socket,&QTcpSocket::readyRead,this,&Widget::clientInfoSlot);
}
//显示收到的信息
void Widget::clientInfoSlot()
{
QByteArray array = socket->readAll(); //接收消息
QHostAddress clientaddr = socket->peerAddress(); //获得IP
int port = socket->peerPort(); //获得端口号
QDateTime datetime = QDateTime::currentDateTime();
QString sendMessage = tr("recv from :") + clientaddr.toString() + tr(" : ") \
+ QString::number(port) + tr(" ") + datetime.toString("yyyy-M-dd hh:mm:ss") + tr("\n");
sendMessage += array;
ui->textEdit->append(sendMessage); //将接收到的内容加入到listwidget
}
//服务器发送信息
void Widget::on_pushButton_clicked()
{
QByteArray ba;
ba.append(ui->lineEdit ->text()); //把QString类型转换成QByteArray类型
if (socket)
{
// socket存在,进行数据写入
socket->write(ba);
// 清空单行文本框
ui->lineEdit->clear();
}
else
{
qDebug() << "Error: Socket is not valid!";
}
}
client.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QDebug>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void slot_recvmessage(); //接收来自服务器的消息的槽
void on_connectButton_clicked(); //连接服务器
void on_pushButton_send_clicked(); //发送信息 槽函数
private:
Ui::Widget *ui;
bool isconnetion; //判断是否连接到服务器的标志位
QTcpSocket *TCP_sendMesSocket; //发送消息套接字
};
#endif // WIDGET_H
client.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <QDateTime>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("TCP客户端");
//初始化sendMesSocket
TCP_sendMesSocket = new QTcpSocket();
//连接客户端成功,socket对象会发出一个信号
connect(TCP_sendMesSocket,&QTcpSocket::connected,[this]()
{
QMessageBox::information(this,"连接提示","连接服务器");
});
//连接失败
connect(TCP_sendMesSocket,&QTcpSocket::disconnected,this,[=]()
{
TCP_sendMesSocket->close();
TCP_sendMesSocket->deleteLater();
QMessageBox::warning(this,"连接提示","连接断开");
});
//有新数据到达时的readyread()信号连接到slot_recvmessage()
connect(TCP_sendMesSocket,SIGNAL(readyRead()),this,SLOT(slot_recvmessage()));
}
Widget::~Widget()
{
delete ui;
}
//10.16.249.166
//点击连接 服务器
void Widget::on_connectButton_clicked()
{
//获取ip地址 和 端口号
QString IP = ui->iplineEdit -> text();
QString port = ui ->portlineEdit ->text();
//连接服务器
TCP_sendMesSocket->connectToHost(QHostAddress(IP),port.toShort());
ui->textEdit->append(tr("与服务器连接成功:") + QDateTime::currentDateTime().toString("yyyy-M-dd hh:mm:ss"));
isconnetion = true;
}
//给服务器发送信息
void Widget::on_pushButton_send_clicked()
{
if(this->isconnetion)
{
QString sendMessage = ui->lineEdit->text(); //从单行文本框获得要发送消息
if(!sendMessage.isEmpty())
{
//发送消息到服务器
QByteArray ba;
ba.append(ui->lineEdit->text()); //把QString类型转换成QByteArray类型
// ui->lineEdit->text(); //QString类型
TCP_sendMesSocket->write(ba);
//本地显示发送的消息
QString localDispalyMessage = tr("send to server: ") + sendMessage \
+ QDateTime::currentDateTime().toString(" yyyy-M-dd hh:mm:ss") + tr("\n");
ui->textEdit->append(localDispalyMessage);
}
else
QMessageBox::warning(this,"错误","消息不能为空!",QMessageBox::Ok);
}
else
QMessageBox::warning(this,"错误","未连接到服务器!",QMessageBox::Ok);
ui->lineEdit->clear();
}
//接收来自服务器的消息的槽函数
void Widget::slot_recvmessage()
{
//接收来自服务器的消息
QByteArray byteArray = this->TCP_sendMesSocket->readAll();
QString recvMessage = tr("recv from server: ") + byteArray + QDateTime::currentDateTime().toString(" yyyy-M-dd hh:mm:ss") + tr("\n");
ui->textEdit->append(recvMessage);
}