源码的下载地址以及socket环境搭建请看这篇博文

http://blog.csdn.net/qq78442761/article/details/60601945

这里我们先看看服务端的tcp搭建

如下图所示:

下面我们来看看源码:

我们在mainwindow.h中可以看到如下:

//socket读取相关函数
public slots:void slotCreateServer();void updateServer(QString mes,char*, int length);

在mianwindow.cpp中

//socket读取相关函数
void MainWindow::slotCreateServer()
{server=new Server(this,port);connect(server,SIGNAL(updateServer(QString,char*,int)),this,SLOT(updateServer(QString,char*,int)));ui->CreateSocktpushButton->setEnabled(false);ui->CMDplainTextEdit->appendPlainText(CurrTime::currentDateTime()+"socket打开成功,端口号为:"+QString::number(port,10));
}

我们现在来看下server.h

#ifndef SERVER_H
#define SERVER_H#include <QTcpServer>
#include <QObject>
#include "tcpclientsocket.h"class Server : public QTcpServer
{Q_OBJECT
public:Server(QObject *parent=0,int port=0);QList<TcpClientSocket*> tcpClientSocketList;
signals:void updateServer(QString,char*,int);
public slots:void updateClients(QString, char *, int);void slotDisconnected(int);
protected:void incomingConnection(int socketDescriptor);
};#endif // SERVER_H

server.cpp

#include "server.h"Server::Server(QObject *parent,int port):QTcpServer(parent)
{listen(QHostAddress::Any,port);
}void Server::incomingConnection(int socketDescriptor)
{TcpClientSocket *tcpClientSocket=new TcpClientSocket(this);connect(tcpClientSocket,SIGNAL(updateClients(QString,char*,int)),this,SLOT(updateClients(QString,char*,int)));connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));tcpClientSocket->setSocketDescriptor(socketDescriptor);tcpClientSocketList.append(tcpClientSocket);
}void Server::updateClients(QString msg, char *temp2, int length)
{emit updateServer(msg,temp2,length);for(int i=0;i<tcpClientSocketList.count();i++){QTcpSocket *item = tcpClientSocketList.at(i);if(item->write(msg.toLocal8Bit(),length)!=length){continue;}}
}void Server::slotDisconnected(int descriptor)
{for(int i=0;i<tcpClientSocketList.count();i++){QTcpSocket *item = tcpClientSocketList.at(i);if(item->socketDescriptor()==descriptor){tcpClientSocketList.removeAt(i);return;}}return;
}

在这里要一个clientscoket

下面来看看这个tcpclientsocke.h

#ifndef TCPCLIENTSOCKET_H
#define TCPCLIENTSOCKET_H#include <QTcpSocket>
#include <QObject>
#include "code.h"
#include <string.h>
#include <math.h>class TcpClientSocket : public QTcpSocket
{Q_OBJECT
public:TcpClientSocket(QObject *parent=0);
signals:void updateClients(QString,char *,int);void disconnected(int);
protected slots:void dataReceived();void slotDisconnected();
};#endif // TCPCLIENTSOCKET_H

tcpclientsocke.cpp

#include "tcpclientsocket.h"
#include <QDebug>TcpClientSocket::TcpClientSocket(QObject *parent)
{connect(this,SIGNAL(readyRead()),this,SLOT(dataReceived()));connect(this,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
}void TcpClientSocket::dataReceived()
{while(bytesAvailable()>0){int length = bytesAvailable();char buf[1024];read(buf,length);//解密char temp1[16];char temp2[8];for(int i=0;i<16;i++)temp1[i]=buf[i];for(int i=0;i<8;i++)temp2[i]=buf[16+i];char key1[] = "qwertyui";char key2[] = "asdfghjk";char key3[] = "zxcvbnm";Code    des1(key1);Code    des2(key2);Code    des3(key3);des3.DevryptName(temp1,temp1);des2.EncryptName(temp1,temp1);des1.DevryptName(temp1,temp1);QString msg=QString::fromUtf8(temp1);qDebug()<<"解密后用户名"<<msg;des3.DevryptData(temp2,temp2);des2.EncryptData(temp2,temp2);des1.DevryptData(temp2,temp2);qDebug()<<"解密后数据为"<<temp2[0];qDebug()<<"解密后数据为"<<(int)temp2[7];emit updateClients(msg,temp2,length);}
}void TcpClientSocket::slotDisconnected()
{emit disconnected(this->socketDescriptor());
}

在mainwindow.cpp里面

对updateServer的定义

//socket写入日志文件栏
void MainWindow::updateServer(QString mes, char*temp2, int length)
{ui->CMDplainTextEdit->appendPlainText(CurrTime::currentDateTime()+"接受到用户发来的数据,用户名为:"+mes);Mysql->InsertUser(mes,temp2);
}

下面是客户端:

客户端中widget.cpp里面的构造函数

中先设置

//socket写入日志文件栏
void MainWindow::updateServer(QString mes, char*temp2, int length)
{ui->CMDplainTextEdit->appendPlainText(CurrTime::currentDateTime()+"接受到用户发来的数据,用户名为:"+mes);Mysql->InsertUser(mes,temp2);
}
void Widget::slotEnter()
{QString ip="192.168.164.100";if(!serverIp->setAddress(ip)){QMessageBox::information(this,"error","server ip address error!");return;}tcpSocket=new QTcpSocket(this);connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));tcpSocket->connectToHost(*serverIp,port);
}
void Widget::slotSend()
{Encry3Name();char SendAll[24];for(int i=0;i<16;i++)SendAll[i]=*UserNameData++;Encry3Data();for(int i=16;i<24;i++)SendAll[i]=GradeMoney[i-16];//    char temp1[16];
//    char temp2[8];
//    for(int i=0;i<16;i++)
//        temp1[i]=SendAll[i];
//    for(int i=0;i<8;i++)
//        temp2[i]=SendAll[16+i];
//    char key1[] = "qwertyui";
//    char key2[] = "asdfghjk";
//    char key3[] = "zxcvbnm";
//    Code    des1(key1);
//    Code    des2(key2);
//    Code    des3(key3);
//    des3.DevryptName(temp1,temp1);
//    des2.EncryptName(temp1,temp1);
//    des1.DevryptName(temp1,temp1);
//    QString d9=QString::fromUtf8(temp1);
//    qDebug()<<"解密后用户名"<<d9;//    des3.DevryptData(temp2,temp2);
//    des2.EncryptData(temp2,temp2);
//    des1.DevryptData(temp2,temp2);
//    qDebug()<<"解密后数据为"<<temp2[0];
//    qDebug()<<"解密后数据为"<<(int)temp2[7];tcpSocket->write(SendAll,24);}

代码里面带有加密的内容,在此不解释将会在加密解密处介绍,是不是用qt封装socket超级简单呢!!!!

大三软件工程小项目-小技术集合-tcp服务器搭建及客户端相关推荐

  1. 大三软件工程小项目-小技术集合总结

    大三软件工程小项目-小技术集 此篇文章是给出了此小项目用到的技术的总结: 方便自己有忘记的知识点后方便查阅. 也方便有需要的博友看. 下面是各个计算的链接 大家点击了进能进去 大三软件工程小项目-小技 ...

  2. 小程序登录本地服务器,微信小程序实现用户登录模块服务器搭建

    我选用的是node.js来搭建服务器,没有安装的小伙伴可以参考我的node.js其他博客. 服务器安装与配置 初始化项目,将会自动创建package.json配置文件. npm init -y 安装E ...

  3. 【正点原子FPGA连载】 第三十二章基于lwip的TCP服务器性能测试实验 摘自【正点原子】DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南

    第三十二章基于lwip的TCP服务器性能测试实验 上一章的lwip Echo Server实验让我们对lwip有一个基本的了解,而Echo Server是基于TCP协议的.TCP协议是为了在不可靠的互 ...

  4. 大三软件工程小项目-小技术集合-Qt状态栏设置

    服务端源码下载地址为: http://download.csdn.net/detail/qq78442761/9768662 状态栏是什么地方呢? 如下图所示: 那么在Qt里面,这个状态栏该怎么编写呢 ...

  5. 自我评估,职业规划及对计算机专业的理解(大三软件工程学生的第一篇博客)

    自我定位职业规划及对计算机邻域的理解 2020-01-09 自我定位: 本人就读于浙工大(仅次于985,211大学)软件工程专业,大三在读,对于学校教学计划掌握程度在80%,能够利用库或API独立完成 ...

  6. 实验三 软件工程结对项目

    Deadline:2018-4-4 10:00,以博客发表日期为准 评分标准: 按时交 - 有分(满分30分,代码和博客各15分),检查项目包括: -  未提交项目源码到Github上,代码部分不得分 ...

  7. 小项目/小游戏Demo合集

    几个以前写过的小项目,比较low,拿出来献献丑. 项目我都已经上传在我的博客上,可以免费下载到. PC端2048小游戏 用C#写的,界面大概是这样 基本逻辑如下: 用picturebox构建一个4*4 ...

  8. Python实战项目23个实战小项目小程序简单

    Python项目23个小项目简单 项目类别 部分项目运行截图 含代码注释 项目类别 51商城 AI智能联系人管理(双击即启动) BBS问答社区 DIY字符画(双击即启动) Excel数据分析师(双击即 ...

  9. 从零开始开发物联网项目(1)——mqtt服务器搭建

    去年开发了一个物联网的项目,入了很多坑,花了很多时间,不过最后终于做出了一个初代版本,也算完成了项目.为了避免自己遗忘,也为了让有兴趣学习物联网的同学少入点坑,我整理了一下,写成一个系列教程.通过这个 ...

最新文章

  1. 棋盘格检测--Automatic camera and range sensor calibration using a single shot
  2. Redis几个认识误区
  3. Spring容器中Bean的作用域
  4. ShellExecuteA function
  5. 计算机论文指导书,计算机毕业论文指导书.doc
  6. Java JVM 汇编代码入门 GitChat链接
  7. 浏览器兼容之JavaScript篇——已在IE、FF、Chrome测试
  8. python--pexpect
  9. 一文搞懂RSOP偏振态旋转
  10. dirname,basename的用法与用途
  11. 解决:能提交和更新,但SVN查看log时提示找不到路径'svn/XXXX'
  12. 2011年 7月6日の朝会文章 手塚 治虫
  13. 【二分答案】【NOIP模拟10-21】的士碰撞
  14. 《长城保护总体规划》出台 为长城保护提供遵循依据
  15. 《Java多线程编程核心技术》学习笔记(1)
  16. 850pro测试软件,新极速霸主诞生 三星850 PRO首发评测
  17. 微信朋友圈马赛克图片 —— 抓包破解
  18. mysq8窗口(开窗)及新特性函数
  19. IP地址、路由器、数据分片、地址管理、子网掩码、路由选择、公网与私网
  20. 有什么可以赚钱的副业?日入300元就靠这几个副业!

热门文章

  1. NVIDIA将全面支持笔记本混合显卡技术
  2. 最新编程语言排名:Python超Java、JS保持领头羊
  3. 前端转行大数据?没必要
  4. 老粉丝来:再来一波免费送书
  5. 混口饭吃,谈不上喜欢
  6. 求绝对值 c鱼眼_初一上学期,绝对值的相关计算,提优篇
  7. 面试了一个2年程序员,竟然只会curd,网友神回复!
  8. thymeleaf 判断checkbox是否选中_一日看尽长安花——复选框(CheckBox)数据清洗和统计的SAS实现...
  9. ​她回顾过去的学习生活,印象最深刻的并非是收获荣耀的高光时刻, 而是在“看文献、做科研、写论文”循环中推进的每一步...
  10. P3954 [NOIP2017 普及组] 成绩(python3实现)