热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

QT开发的即时通讯软件(基于TCP)

这是本人在学习QT和计算机网络的时候自己开发的TCP网络通讯软件,包含服务端和客户端两个工程,亲测可用,我自己也经常用它们做网络的测试&#

这是本人在学习QT和计算机网络的时候自己开发的TCP网络通讯软件,包含服务端和客户端两个工程,亲测可用,我自己也经常用它们做网络的测试,注释详细,欢迎参考,先上图,源码附在下面,也可以直接在这下载:https://download.csdn.net/download/qq_18108083/10798425  赚点积分,嘿嘿,谢谢大家~

一、server端

(1).mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include
#include
#include //保证正常显示中文
#include
#include
#include
#include
#include //图片
#include
#include
#include //线程实验
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();QImage *image;QTimer *timer;//线程实验myThread *thread;int pb;
private:Ui::MainWindow *ui;QTcpServer *tserver;QTcpSocket *tsocket;private slots:void listenStartSlot(); //开始侦听槽void newConnectSlot(); //收到新连接void receiveTcpSlot(); //槽函数用于对接受的数据进行处理void sendTcpSlot(); //发送tcp信号槽(发送按钮触发)void tcpDisconnect(); //tcp连接中断//线程实验void closeThreadSlot();
};#endif // MAINWINDOW_H

(2).mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);tserver =new QTcpServer; //创建新TCP服务器QObject::connect(ui->listenButton,SIGNAL(clicked(bool)),this,SLOT(listenStartSlot())); //开始侦听QObject::connect(this->tserver,SIGNAL(newConnection()),this,SLOT(newConnectSlot())); //侦听到新连接QObject::connect(ui->sendButton,SIGNAL(clicked(bool)),this,SLOT(sendTcpSlot())); //发送数据//线程实验QObject::connect(ui->closeThreadButton,SIGNAL(clicked(bool)),this,SLOT(closeThreadSlot()));//线程实验pb=0;thread=new myThread(pb);thread->start();
}MainWindow::~MainWindow()
{tserver->close();tserver->deleteLater();delete thread; //线程实验delete ui;
}
void MainWindow::listenStartSlot() //开始侦听槽
{if(ui->listenButton->text()&#61;&#61;"侦听"){quint16 port&#61;quint16(ui->listenPortLineEdit->text().toUInt());//获取监听端口qDebug()<<"port:"<listen(QHostAddress::Any,port)){QMessageBox::information(this,"提示","侦听设置出错");return;}else{QMessageBox::information(this,"提示","开始侦听");ui->listenButton->setText("取消侦听");}}else{if(tsocket->state()&#61;&#61;QAbstractSocket::ConnectedState){tsocket->disconnectFromHost(); //关闭连接}tserver->close(); //取消侦听ui->listenButton->setText("侦听");}}void MainWindow::newConnectSlot() //收到新连接
{tsocket&#61;tserver->nextPendingConnection(); //server让自己的socket与client的socket相连QObject::connect(this->tsocket,SIGNAL(disconnected()),this,SLOT(tcpDisconnect())); //套接字断开连接QObject::connect(this->tsocket,SIGNAL(readyRead()),this,SLOT(receiveTcpSlot())); //收到数据QMessageBox::information(this,"提示","侦听到新连接");}void MainWindow::receiveTcpSlot() //槽函数用于对接受的数据进行处理
{QByteArray receiveData; //因为传来的数据类型是未知的&#xff0c;用bytearray类型receiveData&#61;tsocket->readAll();ui->receiveContextTextEdit->append(QString::fromUtf8(receiveData)); //}
void MainWindow::sendTcpSlot() //发送tcp信号槽&#xff08;发送按钮触发&#xff09;
{//发送图片// qDebug()<<"sendPicture clicked"<write(dataArray);// qDebug("write len:%d",write_len);//添加判断连接QString sendContext&#61;ui->sendContextTextEdit->document()->toPlainText();//tsocket->write(sendContext.toUtf8()); //以Utf8形式的bytearray发送tsocket->flush();}
void MainWindow::tcpDisconnect() //tcp连接中断
{ui->listenButton->setText("侦听");QMessageBox::information(this,"提示","TCP连接中断");}
void MainWindow::closeThreadSlot()
{delete thread;}

二、client端

(1).mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include
#include //保证正常显示中文
#include
#include
#include
#include
#include
#include
#include
#include //数据流
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent &#61; 0);~MainWindow();private:Ui::MainWindow *ui;QTcpSocket *tsocket;private slots:void connectHostSlot(); //请求建立连接void receiveTcpSlot(); //槽函数用于对接受的数据进行处理void sendTcpSlot(); //发送tcp信号槽&#xff08;发送按钮触发&#xff09;void tcpDisconnect(); //tcp连接中断
};#endif // MAINWINDOW_H

(2)mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);tsocket &#61;new QTcpSocket; //新建一个TCP套接字QObject::connect(ui->connectButton,SIGNAL(clicked(bool)),this,SLOT(connectHostSlot())); //连接主机QObject::connect(ui->sendButton,SIGNAL(clicked(bool)),this,SLOT(sendTcpSlot())); //发送数据QObject::connect(tsocket,SIGNAL(readyRead()),this,SLOT(receiveTcpSlot())); //接收数据QObject::connect(tsocket,SIGNAL(disconnected()),this,SLOT(tcpDisconnect())); //tcp连接中断}MainWindow::~MainWindow()
{delete ui;delete this->tsocket; //清理内存
}
void MainWindow::connectHostSlot() //请求建立连接
{if(ui->connectButton->text()&#61;&#61;"连接"){QString hostip&#61;ui->hostIpLineEdit->text(); //获取主机ipquint16 hostport&#61;quint16(ui->hostPortLineEdit->text().toUInt()); //获取主机端口tsocket->abort(); //取消已有连接tsocket->connectToHost(hostip,hostport); //请求与服务器建立连接if(!tsocket->waitForConnected(30000)) //等待30s{QMessageBox::information(this,"提示","连接失败");return;}else{QMessageBox::information(this,"提示","连接成功");ui->connectButton->setText("断开连接");}}else{tsocket->disconnectFromHost(); //断开连接ui->connectButton->setText("连接");}
}
void MainWindow::receiveTcpSlot() //槽函数用于对接受的数据进行处理
{// QByteArray array;
// while(tsocket->waitForReadyRead(1)){ //连续接收 &#xff0c;知道接收完毕
// qDebug()<<"bytesAvailable"<// array.append((QByteArray)tsocket->readAll());
// }
//
// QBuffer buffer(&array);
// buffer.open(QIODevice::ReadOnly);
//
// QImageReader reader(&buffer,"JPG");
// QImage img &#61; reader.read();
//
// if(!img.isNull()){
// qDebug()<<"right"<// ui->imageLabel->setPixmap(QPixmap::fromImage(img).scaled(ui->imageLabel->size()));//按比例缩放
// } else {
// qDebug()<<"error"<// }QByteArray receiveData; //因为传来的数据类型是未知的&#xff0c;用bytearray类型receiveData&#61;tsocket->readAll();ui->receiveContextTextEdit->append(QString::fromUtf8(receiveData)); //转换成char *}
void MainWindow::sendTcpSlot() //发送tcp信号槽&#xff08;发送按钮触发&#xff09;
{//添加判断连接QString sendContext&#61;ui->sendContextTextEdit->document()->toPlainText();//
// tsocket->write(sendContext.toLatin1());tsocket->write(sendContext.toUtf8()); //将QString转换成QByteArray类型发送tsocket->flush();
}
void MainWindow::tcpDisconnect() //tcp连接中断
{ui->connectButton->setText("连接");QMessageBox::information(this,"提示","TCP连接中断");
}

 


推荐阅读
author-avatar
小--瑜Gg
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有