//客户端,
#include "tcpcli.h"
#include "ui_tcpcli.h"
TcpCli::TcpCli(QWidget *parent) :
QWidget(parent),
ui(new Ui::TcpCli)
{
ui->setupUi(this);
//给客户端指针实例化对象
cli_sock = new QTcpSocket(this);
ui->discntBtn->setEnabled(false);
// ui->lineEdit->setEnabled(false);
ui->pushButton->setEnabled(false);
connect(cli_sock,&QTcpSocket :: connected,this,&TcpCli :: connected_slot);
connect(cli_sock,&QTcpSocket :: readyRead,this,&TcpCli :: readyRd_slot);
connect(cli_sock,&QTcpSocket :: disconnected,this,&TcpCli :: disconnected_slot);
}
TcpCli::~TcpCli()
{
delete ui;
}
//重写键盘事件
void TcpCli::keyPressEvent(QKeyEvent *event)
{
if(event->text() == "\r")
{
if(connectState != 1)
{
QMessageBox :: critical(this,"错误","未连接服务器");
return;
}
if(ui->lineEdit->text() == "")
{
QMessageBox :: warning(this,"警告","输入不能为空");
return;
}
QString msg = usr_name + ":" + ui->lineEdit->text();
cli_sock->write(msg.toLocal8Bit());
ui->lineEdit->clear();
}
}
//连接服务器
void TcpCli::on_connectBtn_clicked()
{
//获取ui界面的信息
usr_name = ui->usrNameEdit->text();
QString hostName = ui->ipEdit->text();
quint16 port = ui->portEdit->text().toUInt();
cli_sock->connectToHost(hostName,port);
}
void TcpCli::connected_slot()
{
ui->ipEdit->setEnabled(false);
ui->portEdit->setEnabled(false);
ui->pushButton->setEnabled(true);
ui->connectBtn->setEnabled(false);
ui->usrNameEdit->setEnabled(false);
ui->discntBtn->setEnabled(true);
QMessageBox :: information(this,"信息","连接服务器成功");
connectState = 1;
//向服务器发送信息
QString msg = usr_name + ":进入聊天室";
cli_sock->write(msg.toLocal8Bit());
}
//关于readyRead信号对应槽函数的实现
void TcpCli::readyRd_slot()
{
//读取该客户端中的数据
QByteArray msg = cli_sock->readAll();
ui->listWidget->addItem(QString :: fromLocal8Bit(msg));
}
//
void TcpCli::on_pushButton_clicked()
{
QString msg = usr_name + ":"+ ui->lineEdit->text();
cli_sock->write(msg.toLocal8Bit());
//将消息编辑框中的内容清空
ui->lineEdit->clear();
}
//断开连接按钮对应的槽函数
void TcpCli::on_discntBtn_clicked()
{
//准备要发送的信息
QString sndmsg = usr_name + ":离开聊天室";
cli_sock->write(sndmsg.toLocal8Bit());
cli_sock->disconnectFromHost();
ui->ipEdit->setEnabled(true);
ui->portEdit->setEnabled(true);
ui->usrNameEdit->setEnabled(true);
ui->connectBtn->setEnabled(true);
ui->discntBtn->setEnabled(false);
ui->pushButton->setEnabled(false);
connectState = 0;
}
void TcpCli::disconnected_slot()
{
QMessageBox :: information(this,"退出","断开成功");
}
//服务器,源文件
#include "tcpser.h"
#include "ui_tcpser.h"
TcpSer::TcpSer(QWidget *parent) :
QWidget(parent),
ui(new Ui::TcpSer)
{
ui->setupUi(this);
//给服务器指针实例化对象
server = new QTcpServer(this);
speech = new QTextToSpeech(this);
}
TcpSer::~TcpSer()
{
delete ui;
}
//启动服务器按钮对应的槽函数
void TcpSer::on_pushButton_clicked()
{
//获取ui界面上的端口号
quint16 port = ui->lineEdit->text().toUInt();
if(!server->listen(QHostAddress :: Any,port))
{
QMessageBox :: critical(this,"错误","服务器启动失败",QMessageBox :: Ok,QMessageBox :: Ok);
return;
}
else
{
QMessageBox :: information(this,"信息","启动成功");
connect(server,&QTcpServer :: newConnection,this,&TcpSer :: newCnt_slot);
}
}
//自定义处理newConnection信号的槽函数实现
void TcpSer::newCnt_slot()
{
speech->setVoice(QVoice());
speech->say("咳咳咳");
QTcpSocket *sock = server->nextPendingConnection();
cli_list.push_back(sock);
connect(sock,&QTcpSocket :: readyRead,this,&TcpSer :: rdyRead_slot);
}
//
void TcpSer::rdyRead_slot()
{
//删除客户端链表中的无效客户端套接字
for(int i = 0;i < cli_list.size();i++)
{
//判断套接字的状态
if(0 == cli_list[i]->state())
{
cli_list.removeAt(i);
}
}
for(int i = 0;i < cli_list.count();i++)
{
if(cli_list[i]->bytesAvailable() != 0)
{
QByteArray msg = cli_list[i]->readAll();
//将数据展示到ui界面上
ui->listWidget->addItem(QString :: fromLocal8Bit(msg));
//将接收到的信息发送给所有客户端
for(int j = 0;j < cli_list.count();j++)
{
cli_list[j]->write(msg);
}
}
}
}
运行效果:服务器端,开启后