client
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, client(new QTcpSocket(this))
, time(QTime::currentTime())
{
ui->setupUi(this);
ui->discon_btn->setEnabled(false);
ui->msg_edit->setEnabled(false);
ui->send_btn->setEnabled(false);
ui->user_edit->setText("huang");
ui->ip_edit->setText("127.0.0.1");
ui->port_edit->setText("9999");
connect(client,&QTcpSocket::connected,this,&Widget::connect_slot);
connect(client,&QTcpSocket::readyRead,this,&Widget::recv_slot);
connect(client,&QTcpSocket::disconnected,this,&Widget::disconnect_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_con_btn_clicked()
{
username = ui->user_edit->text();
client->connectToHost(ui->ip_edit->text(),ui->port_edit->text().toUInt());
ui->discon_btn->setEnabled(true);
ui->msg_edit->setEnabled(true);
ui->send_btn->setEnabled(true);
ui->user_edit->setEnabled(false);
ui->ip_edit->setEnabled(false);
ui->port_edit->setEnabled(false);
}
void Widget::connect_slot()
{
QString msg = username+"进入聊天室";
client->write(msg.toLocal8Bit());
}
void Widget::on_send_btn_clicked()
{
QString msg = ui->msg_edit->text();
msg = username +"\n"+ msg;
client->write(msg.toLocal8Bit());
ui->msg_edit->clear();
}
void Widget::recv_slot()
{
QByteArray msg = client->readAll();
QString m = QString::fromLocal8Bit(msg);
//收到信息格式为 username \n msg
QStringList list = m.split("\n");
time = QTime::currentTime();
QString t = time.toString("HH:mm:ss");
m = t +":"+ m;
//判断姓名和当前相同设置到右边显示
if(list.at(0) == username)
{
QListWidgetItem* item = new QListWidgetItem(m);
item->setTextAlignment(Qt::AlignRight);
ui->listWidget->addItem(item);
}else
{
ui->listWidget->addItem(m);
}
}
//点击断开连接,调用disconnectFromHost
void Widget::on_discon_btn_clicked()
{
QString msg = username+"离开聊天室";
client->write(msg.toLocal8Bit());
client->disconnectFromHost();
}
//调用disconnectFromHost后执行的槽函数
void Widget::disconnect_slot()
{
ui->discon_btn->setEnabled(false);
ui->msg_edit->setEnabled(false);
ui->send_btn->setEnabled(false);
ui->user_edit->setEnabled(true);
ui->ip_edit->setEnabled(true);
ui->port_edit->setEnabled(true);
}
//main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QTime>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_con_btn_clicked();
void connect_slot();
void on_send_btn_clicked();
void recv_slot();
void on_discon_btn_clicked();
void disconnect_slot();
private:
Ui::Widget *ui;
QString username;
QTcpSocket *client;
QTime time;
int itemNum;
};
#endif // WIDGET_H
server
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, server(new QTcpServer(this))
, time(QTime::currentTime())
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_run_btn_clicked()
{
if(ui->port_edit->text().toUInt()==0)
{
QMessageBox::information(this,"","请输入端口号",QMessageBox::Ok);
return;
}
bool ok = server->listen(QHostAddress::Any,ui->port_edit->text().toUInt());
if(ok)
{
QMessageBox::information(this,"","开启成功",QMessageBox::Ok);
connect(server,&QTcpServer::newConnection,this,&Widget::connect_slot);
}else
{
QMessageBox::information(this,"","开启失败",QMessageBox::Ok);
return;
}
}
void Widget::connect_slot()
{
QTcpSocket* client = server->nextPendingConnection();
inlinelist.push_back(client);
connect(client,&QTcpSocket::readyRead,this,&Widget::tran_slot);
}
void Widget::tran_slot()
{
for(int i = 0;i<inlinelist.count();i++)
{
if(inlinelist.at(i)->state()==0)
{
inlinelist.removeAt(i);
}
}
for(int i=0;i<inlinelist.count();i++)
{
if(inlinelist.at(i)->bytesAvailable()!=0)
{
//记录信息发送时间
time.currentTime();
QString t = time.toString("HH:mm:ss");
//读取消息
QByteArray msg = inlinelist.at(i)->readAll();
QString m = QString::fromLocal8Bit(msg);
//拼接时间和信息
m = "("+t+")"+":" + m;
ui->listWidget->addItem(m);
for(int j = 0;j<inlinelist.count();j++)
{
inlinelist.at(j)->write(msg);
}
}
}
}
//main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QMessageBox>
#include <QList>
#include <QTcpSocket>
#include <QTime>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_run_btn_clicked();
void connect_slot();
void tran_slot();
private:
Ui::Widget *ui;
QTcpServer *server;
QList<QTcpSocket*> inlinelist;
QTime time;
};
#endif // WIDGET_H