QTcpSocket和QTcpServer实现聊天小工具
一、说明
说明:有一段时间没有使用socket编程了,这两天抽时间使用QtcpSocket和QTcpServer写了一个简单的聊天小工具。
二、效果
效果截图如下:
三、核心代码
/***************************ServerWidget.cpp*************************/
#include "ServerWidget.h"
#include "ui_ServerWidget.h"
ServerWidget::ServerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServerWidget)
{
ui->setupUi(this);
m_tcpServer = NULL;
m_tcpSocket = NULL;
m_tcpServer = new QTcpServer(this);
m_tcpServer->listen(QHostAddress::Any,8899);
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(dealConnectClient()));
}
ServerWidget::~ServerWidget()
{
if(m_tcpServer != NULL)
{
m_tcpServer->close();
m_tcpServer = NULL;
}
if(m_tcpSocket != NULL)
{
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
m_tcpSocket = NULL;
}
delete ui;
}
// 服务器和客户端建立连接
void ServerWidget::dealConnectClient()
{
if(NULL == m_tcpServer)
return;
m_tcpSocket = m_tcpServer->nextPendingConnection();
QString ip = m_tcpSocket->peerAddress().toString();
quint16 port = m_tcpSocket->peerPort();
QString tempText = QString("和客户端%1:%2连接成功!").arg(ip).arg(port);
ui->textEditRcv->setTextColor(QColor("cyan"));
ui->textEditRcv->append("server:"+tempText);
/// 这个信号与槽的连接必须放在这,放在构造函数中不行,因为在构造函数中时,m_tcpSocket=NULL连接不成功
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(dealReadData()));
}
// 服务器接收客户端数据
void ServerWidget::dealReadData()
{
if(NULL == m_tcpSocket)
return;
QByteArray readByteData = m_tcpSocket->readAll();
ui->textEditRcv->setTextColor(QColor("red"));
ui->textEditRcv->append("client:" + readByteData);
}
// 服务器写数据
void ServerWidget::on_pushButton_clicked()
{
if(NULL == m_tcpSocket)
return;
QString sendText = ui->textEditSend->toPlainText();
m_tcpSocket->write(sendText.toUtf8().data());
ui->textEditRcv->setTextColor(QColor("cyan"));
ui->textEditRcv->append("server:"+sendText);
}
// 服务器与客户端断开连接
void ServerWidget::on_pushButton_2_clicked()
{
if(NULL == m_tcpSocket)
return;
QString ip = m_tcpSocket->peerAddress().toString();
quint16 port = m_tcpSocket->peerPort();
QString tempText = QString("和客户端%1:%2断开连接成功!").arg(ip).arg(port);
ui->textEditRcv->setTextColor(QColor("cyan"));
ui->textEditRcv->append("server:"+tempText);
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
m_tcpSocket = NULL;
}
/***************************ClientWidget.cpp*************************/
#include "ClientWidget.h"
#include "ui_ClientWidget.h"
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
m_tcpSocket = NULL;
m_tcpSocket = new QTcpSocket(this);
connect(m_tcpSocket,SIGNAL(connected()),this,SLOT(dealConnectServer()));
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(dealReadData()));
}
ClientWidget::~ClientWidget()
{
if(m_tcpSocket != NULL)
{
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
m_tcpSocket = NULL;
}
delete ui;
}
// 断开连接
void ClientWidget::on_pushButton_clicked()
{
if(NULL == m_tcpSocket)
return;
ui->textEdit->setTextColor(QColor("cyan"));
ui->textEdit->append("client:和服务器断开连接成功!");
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
m_tcpSocket = NULL;
}
// 客户端写数据
void ClientWidget::on_pushButton_2_clicked()
{
if(NULL == m_tcpSocket)
return;
QString sendText = ui->textEdit_2->toPlainText();
m_tcpSocket->write(sendText.toUtf8().data());
ui->textEdit->setTextColor(QColor("cyan"));
ui->textEdit->append("client:"+sendText);
}
// 客户端连接服务器
void ClientWidget::on_pushButton_3_clicked()
{
if(NULL == m_tcpSocket)
return;
QString ip = ui->lineEdit->text();
QString port = ui->lineEdit_2->text();
m_tcpSocket->connectToHost(ip,port.toInt());
}
// 提示连接成功
void ClientWidget::dealConnectServer()
{
ui->textEdit->setTextColor(QColor("cyan"));
ui->textEdit->append("client:和服务器建立连接成功!");
}
// 客户端从socket中读取数据
void ClientWidget::dealReadData()
{
if(NULL == m_tcpSocket)
return;
QByteArray readText = m_tcpSocket->readAll();
ui->textEdit->setTextColor(QColor("red"));
ui->textEdit->append("server:"+readText);
}
源码下载地址:http://download.csdn.net/download/toby54king/10153873
本文为博主原创文章,未经博主允许请勿转载!作者:ISmileLi