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

582 篇文章 121 订阅
36 篇文章 23 订阅

源码的下载地址以及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超级简单呢!!!!




  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值