Qt: 基于QTcpSocket的聊天程序
来源:互联网 发布:韩国网络剧奇迹百度云 编辑:程序博客网 时间:2024/05/16 08:28
下载地址:http://download.csdn.net/detail/rl529014/9660330 C++、Qt学习交流群: 302558294(欢迎你的加入)
效果图:
原理:
用到的库:QTcpServer,QTcpServer,QHostAddress,QtNetwork。
源码:
服务器端:
//mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtNetwork>#include <QTcpServer>#include <QTcpSocket>#include <QTimer>#include <QByteArray>namespace Ui{class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();public: void init();private slots: void sendMessage(); //发送消息 void onReciveData(); //接收数据 void newListen(); //建立tcp监听事件 void acceptConnection(); //接收客户端连接 void showError(QAbstractSocket::SocketError); //错误输出private: Ui::MainWindow *ui;private: QTcpSocket *tcpSocket; QTcpServer *tcpServer; // QTimer *timer; QByteArray mChat;};#endif // MAINWINDOW_H
//mainwindow.cpp
#include <QObject>#include <QString>#include <QTextEdit>#include <QHostAddress>#include <QByteArray>#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow){ ui->setupUi(this); init(); setWindowTitle(QString::fromLocal8Bit("服务器端")); connect(ui->sendBtn,SIGNAL(clicked(bool)),SLOT(sendMessage()));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::init(){ // timer = new QTimer; tcpServer = new QTcpServer; tcpSocket = new QTcpSocket; newListen(); connect(tcpServer,SIGNAL(newConnection()),SLOT(acceptConnection())); connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),SLOT(showError(QAbstractSocket::SocketError)));}void MainWindow::sendMessage() //发送数据{ QString textEdit = ui->lineEdit->text(); QString strData =QString::fromLocal8Bit("Time: ") + QTime::currentTime().toString() + "\n" + textEdit.toLocal8Bit() + "\n"; QByteArray sendMessage = strData.toLocal8Bit(); mChat += ("Send " + sendMessage); ui->textEdit->setText(mChat); tcpSocket->write(sendMessage);}void MainWindow::onReciveData() //读取数据{ QString data = tcpSocket->readAll(); qDebug()<<data; mChat +=("Recv " + data); ui->textEdit->setText(mChat);}void MainWindow::newListen(){ if(!tcpServer->listen(QHostAddress::Any,6666)) { qDebug()<<tcpServer->errorString(); tcpServer->close(); }}void MainWindow::acceptConnection(){ tcpSocket = tcpServer->nextPendingConnection(); connect(tcpSocket,SIGNAL(readyRead()),SLOT(onReciveData()));}void MainWindow::showError(QAbstractSocket::SocketError){ qDebug()<<tcpSocket->errorString(); tcpSocket->close();}
客户端:
//mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtNetwork>#include <QTcpServer>#include <QTcpSocket>#include <QTimer>#include <QAbstractSocket>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();public: void init(); void newTcpConnect();private slots: void onReciveData(); void onSendMessage(); void onShowError(QAbstractSocket::SocketError);private: QTcpSocket *tcpSocket; QByteArray mChat;private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); setWindowTitle(QString::fromLocal8Bit("客户端")); init(); connect(ui->sendBtn,SIGNAL(clicked(bool)),SLOT(onSendMessage()));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::init(){ tcpSocket = new QTcpSocket; newTcpConnect(); connect(tcpSocket,SIGNAL(readyRead()),SLOT(onReciveData()));}void MainWindow::newTcpConnect(){ tcpSocket->abort(); tcpSocket->connectToHost("127.0.0.1",6666);}void MainWindow::onReciveData(){ QString data = tcpSocket->readAll(); qDebug()<<data; mChat += ("Recv " + data); ui->textEdit->setText(mChat);}void MainWindow::onSendMessage(){ QString textEdit = ui->lineEdit->text(); QString strData =QString::fromLocal8Bit("Time: ") + QTime::currentTime().toString() + "\n" + textEdit.toLocal8Bit() +"\n"; QByteArray sendMessage = strData.toLocal8Bit(); mChat+= ("Send " + sendMessage); ui->textEdit->setText(mChat); tcpSocket->write(sendMessage);}void MainWindow::onShowError(QAbstractSocket::SocketError){ qDebug()<<tcpSocket->errorString(); tcpSocket->close();}
下载地址:http://download.csdn.net/detail/rl529014/9660330
C++、Qt学习交流群: 302558294(欢迎你的加入)
4 0
- Qt: 基于QTcpSocket的聊天程序
- QT类似于QQ的即时聊天程序用开发QTcpServer,QTcpSocket简单开发
- QT类似于QQ的即时聊天程序用开发QTcpServer,QTcpSocket简单开发
- QTcpServer、QTcpSocket、QUdpSocket在聊天程序上的应用
- 基于silverlight的聊天程序
- 基于UDP的聊天程序
- 基于UDP的聊天程序
- 基于QT的网络聊天系统
- 基于Qt的局域网聊天软件
- 基于QT的网络聊天系统
- 基于Linux和Qt的聊天软件
- 基于Qt的QQ局域网聊天
- QT界面之美——基于TCP聊天程序
- Qt编写的局域网聊天程序
- 一个基于TCP的聊天程序
- 基于UDP的聊天程序-JAVA
- 基于TCP 的多线程 聊天程序
- 基于多点通信的PtoP聊天程序
- Android 数据库操作
- 【Linux】centOS 启动流程图
- 如何在 CentOS / RHEL 上设置 SSH 免密码登录
- 什么是编译性语言、解释性语言和脚本语言
- VC++ 创建进程并等待其退出
- Qt: 基于QTcpSocket的聊天程序
- Android Studio 2.2下AIDL完全配置解析入手。
- rails 练习9 --整理文章列表上的连接
- mysql表空间
- Apache之Cannot load modules/mod_access_compat.so into server
- 用PCA(主成分分析法)进行信号滤波
- Java中Synchronized的用法
- 关于django migrations的使用
- Cocoapods踩坑记录