大三软件工程小项目-小技术集合-tcp服务器搭建及客户端
生活随笔
收集整理的這篇文章主要介紹了
大三软件工程小项目-小技术集合-tcp服务器搭建及客户端
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
源碼的下載地址以及socket環(huán)境搭建請(qǐng)看這篇博文
http://blog.csdn.net/qq78442761/article/details/60601945
這里我們先看看服務(wù)端的tcp搭建
如下圖所示:
下面我們來看看源碼:
我們?cè)趍ainwindow.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打開成功,端口號(hào)為:"+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_Hserver.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; }在這里要一個(gè)clientscoket
下面來看看這個(gè)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
在mainwindow.cpp里面
對(duì)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);}
總結(jié)
以上是生活随笔為你收集整理的大三软件工程小项目-小技术集合-tcp服务器搭建及客户端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WEB安全基础-XSS基础
- 下一篇: ajax视频播放,XMLHttpRequ