想着练习一下QT,做一个简单的小程序。

突然就想到做一个聊天工具,这个实现起来很简单,没有复杂的关系。

主要实现客户端和服务器简单交互,可以发送字母、数字、汉字。

界面设计比较简单,后续再有功能再作更新。

写了一个简单的登录界面,用户名和密码是死的,因为没有添加数据库。

主要实现是server创建socket对象后,一直处于监听状态,等待客户端的连接请求,

连接成功后,实现简易的数据读写,(先启动server哦!)

登录界面如下:mainwindows

聊天界面如下:mainwin_1

整体界面简单,稍微看一下代码就能明白!

server端的代码:

mainwin_1.h  +  mainwindows.h:

//mainwindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <mainwin_1.h>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);MainWin_1 *mainwin;   //UI of chat~MainWindow();private slots:void on_pushButton_clicked();//loginvoid on_pushButton_2_clicked();//cancelprivate:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H//  mainwin_1.h--------------------------------------------------------
#ifndef MAINWIN_1_H
#define MAINWIN_1_H#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>namespace Ui {
class MainWin_1;
}class MainWin_1 : public QMainWindow
{Q_OBJECTpublic:explicit MainWin_1(QWidget *parent = 0);~MainWin_1();private slots:void on_pushButton_clicked();  //listen in client, send the newsvoid server_NEW_Connect();     //accept the client signal and connectvoid socket_Read_Data();       //read the client newsvoid socket_Disconnect();      //disconnectprivate:void my_connect();private:Ui::MainWin_1 *ui;QTcpServer *server; //create server objectQTcpSocket *socket; //create clirnt object
};#endif // MAINWIN_1_H

main.cpp  +  mainwin_1.cpp  +  mainwindows.cpp:

//main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "QTextCodec"int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;QTextCodec::setCodecForTr( QTextCodec::codecForName("GBK"));QTextCodec::setCodecForCStrings( QTextCodec::codecForName("GBK") );//w.setWindowIcon(QIcon(":/QQ.png"));w.show();return a.exec();
}//mainwin_1.cpp----------------------------------------------------------
#include "mainwin_1.h"
#include "ui_mainwin_1.h"
#include "QMessageBox"
#include "QDebug"
#include "QString"
#include "QDateTime"
#include "QTextCodec"MainWin_1::MainWin_1(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWin_1)
{ui->setupUi(this);setWindowTitle(tr("QQ"));server = new QTcpServer();connect(server, SIGNAL(newConnection()), this, SLOT(server_NEW_Connect()));my_connect();
}MainWin_1::~MainWin_1()
{server->close();server->deleteLater();delete ui;
}void MainWin_1::my_connect()
{QHostAddress IP("172.28.197.253");  //ip is your IPquint16 port = 8889;if(server->listen(IP, port)){}else{QMessageBox::warning(this, "warning", tr("listen failed!"), QMessageBox::No);}
}void MainWin_1::on_pushButton_clicked()
{QString sendmessage;//QString recive_message;//send_message = ui->lineEdit->text();//QTextCodec* codec=QTextCodec::codecForName("UTF-8");QTextCodec* codec=QTextCodec::codecForName("GBK");QString timelist = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");sendmessage = ui->textEdit_2->toPlainText();QByteArray sendmessage_ = codec->fromUnicode(sendmessage);qint16 writeResult = socket->write(sendmessage_); //send newsbool BoolFlush = socket->flush();if(writeResult != -1 && BoolFlush == 1){if(writeResult == 0){QMessageBox::warning(this, "warning", tr("read date result is 0!"), QMessageBox::Yes, QMessageBox::No);}}timelist = "server: " + timelist;//QString sendmessage_ = ui->textEdit_2->toPlainText().toLatin1();//sendmessage = codec->toUnicode(sendmessage_.toLatin1().data());//QString sendmessage_ = codec->toUnicode(sendmessage);ui->textEdit->append(timelist);ui->textEdit->append(ui->textEdit_2->toPlainText());ui->textEdit_2->clear();
}void MainWin_1::server_NEW_Connect()
{socket = server->nextPendingConnection();QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_Read_Data()));QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_Disconnect()));
}void MainWin_1::socket_Read_Data()
{QByteArray buffer;buffer = socket->readAll(); //read news of client//QTextCodec* codec=QTextCodec::codecForName("GBK");QString timelist = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); //view of timetimelist = "client: " + timelist;//QString buffer_ = QString(buffer);//QString buffer_ = codec->toUnicode(buffer);ui->textEdit->append(timelist);ui->textEdit->append(buffer);//ui->textEdit->append(buffer_);
}void MainWin_1::socket_Disconnect()
{}//mainwindows.cpp-----------------------------------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qmessagebox.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);setWindowTitle(tr("QQ"));//title of login
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{QString name = ui->lineEdit->text();     //userQString passw = ui->lineEdit_2->text();  //passwordif((name == "caojian" && passw == "123") || (name == "laowang" && passw == "321")){mainwin = new MainWin_1;mainwin->show();MainWindow::close();  //close the login}else{QMessageBox::warning(this, "login failed", "user or password is error!");return;}
}void MainWindow::on_pushButton_2_clicked()
{QApplication::exit();//logout
}

client端的代码如下:

mainwin_1.h  + mainwindows.h:

//mainwin.h
#ifndef MAINW_1_H
#define MAINW_1_H#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>namespace Ui {
class MainW_1;
}class MainW_1 : public QMainWindow
{Q_OBJECTpublic:explicit MainW_1(QWidget *parent = 0);~MainW_1();private slots:void on_pushButton_clicked();void socket_Read_Date();void socket_Disconnected();private:void my_connect();private:Ui::MainW_1 *ui;QTcpSocket *socket;
};#endif // MAINW_1_Hmainwindows.h---------------------------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <mainw_1.h>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);MainW_1 *mainwin;~MainWindow();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H

main.cpp + mainwin_1.cpp + mianwindows.cpp:

//main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "QTextCodec"int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;QTextCodec::setCodecForTr( QTextCodec::codecForName("GBK"));QTextCodec::setCodecForCStrings( QTextCodec::codecForName("GBK"));w.show();return a.exec();
}//mainwin_1.cpp-----------------------------------------------------
#include "mainw_1.h"
#include "ui_mainw_1.h"
#include "QMessageBox"
#include "QDebug"
#include "QString"
#include "QDateTime"
#include "QTextCodec"MainW_1::MainW_1(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainW_1)
{ui->setupUi(this);setWindowTitle(tr("Q_Q"));socket = new QTcpSocket();connect(socket, SIGNAL(readyRead()), this, SLOT(socket_Read_Date()));my_connect();
}MainW_1::~MainW_1()
{delete this->socket;delete ui;
}void MainW_1::my_connect()
{socket->abort();socket->connectToHost("172.28.197.253", 8889);  //ip is your IPif(!socket->waitForConnected(50000)){qDebug()<<tr("listen server failed!");QMessageBox::warning(this, "warning", tr("listen server failed!"), QMessageBox::Yes, QMessageBox::No);}else{qDebug()<<tr("listen server succeed!");}
}void MainW_1::on_pushButton_clicked()
{QString sendmessage;QTextCodec* codec=QTextCodec::codecForName("GBK");sendmessage = ui->textEdit_2->toPlainText();QByteArray sendmessage_ = codec->fromUnicode(sendmessage);QMessageBox::warning(this, "warning", tr("11111111111"), QMessageBox::Yes, QMessageBox::No);qint16 writeResult = socket->write(sendmessage_); //send newsbool BoolFlush = socket->flush();if(writeResult != -1 && BoolFlush == 1){if(writeResult == 0){QMessageBox::warning(this, "warning", tr("read result is 0!"), QMessageBox::Yes, QMessageBox::No);}}QString timelist = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");timelist = "client: " + timelist;//sendmessage = ui->textEdit_2->toPlainText().toLatin1();ui->textEdit->append(timelist);ui->textEdit->append(ui->textEdit_2->toPlainText());ui->textEdit_2->clear();
}void MainW_1::socket_Read_Date()
{QByteArray buffer;buffer = socket->readAll();  //accept news//QString buffer_ = QString(buffer);QString timelist = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); //view timetimelist = "server: " + timelist;ui->textEdit->append(timelist);ui->textEdit->append(buffer);
}void MainW_1::socket_Disconnected()
{}//mainwindows.cpp-----------------------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qmessagebox.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);setWindowTitle(tr("Q_Q"));
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{QString name = ui->lineEdit->text();QString password = ui->lineEdit_2->text();if((name == "caojian" && password == "123") || (name == "laowang" && password == "321")){mainwin = new MainW_1;mainwin->show();MainWindow::close();}else{QMessageBox::warning(this, "login failed", "name or password is error!");return;}
}void MainWindow::on_pushButton_2_clicked()
{QApplication::exit();
}

以上是全部代码!加油。

QT之简易的聊天工具的实现(Socket通信)相关推荐

  1. python异步socket接收_Python简易聊天工具-基于异步Socket通信

    #-*- coding: UTF-8 -*- from asyncore import dispatcher from asynchat import async_chat import asynco ...

  2. java socket实现的简易的聊天工具demo

    这是一个使用原生java socket实现的简易的聊天工具demo, 界面使用java swing 界面如下: ---------- 项目目录结构 代码: https://gitee.com/kunl ...

  3. 基于WebServices简易网络聊天工具的设计与实现

    基于WebServices简易网络聊天工具的设计与实现 Copyright 朱向洋 Sunsea ALL Right Reserved 一.项目内容 本次课程实现一个类似QQ的网络聊天软件的功能:服务 ...

  4. Android开发笔记(一百一十一)聊天室中的Socket通信

    Socket通信 基本概念 对于程序开发来说,网络通信的基础就是Socket,但因为是基础,所以用起来不容易,今天我们就来谈谈Socket通信.计算机网络有个大名鼎鼎的TCP/IP协议,普通用户在电脑 ...

  5. QT和linux实现简易远程聊天工具

    1.实现功能: 打开客户端有一个登陆窗口,有登陆,注册两种功能(bug肯定很多),登陆成功会在左边显示在线用户数量和列表,点击对应的用户名即可给该用户发送消息,客户端还可以记录聊天记录,如果将服务器运 ...

  6. iOS一个简单聊天工具的实现

    Top 简易的聊天工具 1.1 问题 Socket的英文原义是孔或者插座的意思,通常也称作套接字,用于描述IP地址和端口,是一个通信链的句柄,本案例使用第三方Socket编程框架AsyncSocket ...

  7. 基于JAVA的TCP网络QQ聊天工具系统

    目 录 1 功能设计 1 1.1功能概述 1 1.2功能模块图 1 2 逻辑设计 2 3 界面设计 4 3.1注册界面: 4 3.2登录界面 5 3.3好友列表页面 5 3.4好友聊天页面 6 3.5 ...

  8. 基于TCP的QQ聊天工具

    ###前言: 基于JAVA语言开发的一款网络聊天工具,通过Socket实现TCP编程,使用多线程实现了多客户端的连接.模仿腾讯QQ的界面,功能较为简单,但是使用了最基本的网络编程技术,如socket. ...

  9. Qt的Socket通信

    Qt的Socket通信 文章目录 Qt的Socket通信 1 TCP/IP 2. UDP 3. TCP/IP 和 UDP的区别 Qt中提供的所有的Socket类都是非阻塞的. Qt中常用的用于sock ...

最新文章

  1. 基于visual Studio2013解决C语言竞赛题之1081shell排序
  2. office自定义安装选项_如何自定义Office 2013中功能区上的现有选项卡
  3. 教你做前端表单文本框必填
  4. Pipe——高性能IO(二)
  5. Linux内核之XArray
  6. linux无缝升级的版本,Angular 1 和 Angular 2 集成:无缝升级的方法
  7. android内存溢出错误,Android Studio 生成 JavaDoc 空指针异常|文档编码出错|内存溢出...
  8. vue项目text-overflow:ellipsis;在生产环境上不显示...的问题
  9. Rust : 求出一个字符串数组中最长的公共连续子序列
  10. TransCAD实用技术梗概
  11. JDK动态代理与CGLIB动态代理
  12. WORD文件拼版生成PDF方法
  13. 电子名片+在线商城=?现在居然可以用名片卖货了
  14. 如何将pdf文件转换成word格式
  15. 批量生成奖状的简单程序
  16. 苹果手机怎么在照片上添加文字_怎么给手机照片添加文字?没想到方法这么容易,1分钟就能学会...
  17. Xposed框架初次见面-开发自己的Xposed插件
  18. 如果面试时直接怒怼面试官
  19. 猎豹移动推出直播产品Live.me风靡美国
  20. 亿道信息丨手持PDA丨三防加固手机丨高频RFID丨电商仓库高效管理

热门文章

  1. 美剧路西法第一季第二季
  2. 2021中国大学生程序设计竞赛(CCPC),烤仔与你不见不散!
  3. 四校联合第一次周赛G、H题解
  4. 集成Google登录并获取个人性别等信息
  5. python还款程序_使用Python3 编写简单信用卡管理程序
  6. 张拉膜结构的安全性如何?
  7. 大四刚毕业应届生说自己已经有三年的工作经验。
  8. HarmonyOS数据存储之首选项(含源码)
  9. Vision引擎中环境地形技术信息
  10. 程序员用哪种输入法好呢