源码的下载地址以及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
#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);
}