日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大三软件工程小项目-小技术集合-tcp服务器搭建及客户端 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

源碼的下載地址以及socket環(huán)境搭建請看這篇博文

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


這里我們先看看服務(wù)端的tcp搭建

如下圖所示:



下面我們來看看源碼:

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

//socket讀取相關(guān)函數(shù) public slots:void slotCreateServer();void updateServer(QString mes,char*, int length);

在mianwindow.cpp中

//socket讀取相關(guān)函數(shù) 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)); }

我們現(xiàn)在來看下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()<<"解密后數(shù)據(jù)為"<<temp2[0];qDebug()<<"解密后數(shù)據(jù)為"<<(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()+"接受到用戶發(fā)來的數(shù)據(jù),用戶名為:"+mes);Mysql->InsertUser(mes,temp2); }



下面是客戶端:

客戶端中widget.cpp里面的構(gòu)造函數(shù)

中先設(shè)置

//socket寫入日志文件欄 void MainWindow::updateServer(QString mes, char*temp2, int length) {ui->CMDplainTextEdit->appendPlainText(CurrTime::currentDateTime()+"接受到用戶發(fā)來的數(shù)據(jù),用戶名為:"+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()<<"解密后數(shù)據(jù)為"<<temp2[0]; // qDebug()<<"解密后數(shù)據(jù)為"<<(int)temp2[7];tcpSocket->write(SendAll,24);}




代碼里面帶有加密的內(nèi)容,在此不解釋將會在加密解密處介紹,是不是用qt封裝socket超級簡單呢!!!!




總結(jié)

以上是生活随笔為你收集整理的大三软件工程小项目-小技术集合-tcp服务器搭建及客户端的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。