计算机网络12——IM聊天系统——项目分析和架构搭建

1、IM——聊天系统主要功能
(1)注册

根据:昵称,手机号,密码
(2)登录

根据:手机号,密码
(3)添加好友

根据:昵称
(4)聊天

根据:昵称
(5)下线

根据:id
2、面向对象编程分析系统框架
(1)客户端QT

1、ui界面

2、核心处理类(处理收到的数据、组织要发送的数据)

3、中介者类(不做数据处理,单纯转发)

4、网络类(收发数据、初始化网络、关闭网络)
(2)服务端VS

1、数据库类

2、核心处理类(处理服务端收到的数据、组织要发送的数据)

3、中介者类(不做数据处理,单纯转发)

4、网络类(收发数据、初始化网络、关闭网络)
(3)分类介绍

1、中介者类

中介者类的作用:给程序留出接口,方便以后功能扩展

[点击并拖拽以移动]

2、网络类

使用TCP和UDP协议
父类:定义接口——纯虚函数
子类:实现函数:UDP子类、TCPClient子类、TCPServer子类

3、中介者类:架构和函数跟网络类一样
3、系统架构搭建

1、INet类

(1)构造和析构函数只声明没定义,可以直接在头文件里定义

INet() {}

(2)父类的析构函数必须是虚函数

virtual ~INet() {}

(3)初始化网络函数initNet为纯虚函数,使用时定义哪个子类的对象,则使用哪个子类

virtual bool initNet() = 0;

[点击并拖拽以移动]

(4)发送数据函数(data:发送数据内容;len:发送数据长度;to:发给谁;)

to在UDP协议下是ip,在TCP协议下是socket

virtual bool sendData(char* data, int len, unsigned long to = 0) = 0;

当使用UDP协议发送时,sendto(sock,buf,len,0,addr,size)函数中buf、len、addr是变量

当使用TCP协议发送时,send(sock,buf,len,0)函数中sock,buf,len为变量

buf为要发送的数据,为char*类型

len为要发送数据的长度,为int类型

UDP中的addr中的ip为变量,可以是char或ulong类型,TCP中的sock为uint类型,因此,取unsigned long类型
代码

1、INet.h

#pragma once

class INet
{
public:
    INet() {}
    virtual ~INet() {}

    //初始化网络
    virtual bool initNet() = 0;

    //接收数据
    virtual void recvData() = 0;

    //发送数据
    virtual bool sendData(char* data, int len, unsigned long to = 0) = 0;

    //关闭网络
    virtual void unitNet() = 0;
};

2、Udp.h

#pragma once
#include"INet.h"

class Udp :public INet
{
public:
    Udp();
    ~Udp();

    //初始化网络
    virtual bool initNet();

    //接收数据
    virtual void recvData();

    //发送数据
    virtual bool sendData(char* data, int len, unsigned long to);

    //关闭网络
    virtual void unitNet();

};

3、Udp.cpp

#include"Udp.h"
#include "../mediator/UdpMediator.h"

Udp::Udp()
{
    
}

Udp::~Udp()
{

}

bool Udp::initNet()
{
    
    return true;
}

void Udp::recvData()
{
    
}

bool Udp::sendData(char* data, int len, unsigned long to)
{
    
    return true;
}

void Udp::unitNet()
{
    
}

4、TcpClient.h

#pragma once
#include"INet.h"

class TcpClient :public INet
{
public:
    TcpClient();
    ~TcpClient();

    //初始化网络
    virtual bool initNet();

    //接收数据
    virtual void recvData();

    //发送数据
    virtual bool sendData(char* data, int len, unsigned long = 0);

    //关闭网络
    virtual void unitNet();


};

5、TcpClient.cpp

#include"TcpClient.h"

TcpClient::TcpClient()
{
    
}

TcpClient::~TcpClient()
{
}

//初始化网络
bool TcpClient::initNet()
{

    return true;
}

void TcpClient::recvData()
{
    
}

bool TcpClient::sendData(char* data, int len, unsigned long to)
{
    
    return true;
}

void TcpClient::unitNet()
{
    
}

6、TcpServer.h

#pragma once
#include"INet.h"

class TcpServer :public INet
{
public:
    TcpServer();
    ~TcpServer();

    //初始化网络
    virtual bool initNet();

    //接收数据
    virtual void recvData();

    //发送数据
    virtual bool sendData(char* data, int len, unsigned long to);

    //关闭网络
    virtual void unitNet();
};

7、TcpServer.cpp

#include"TcpServer.h"

TcpServer::TcpServer()
{
    
}

TcpServer::~TcpServer()
{
}

//创建一个接受连接的线程
bool TcpServer::initNet()
{
    

    return true;
}

void TcpServer::recvData()
{
    
}

bool TcpServer::sendData(char* data, int len, unsigned long to)
{
    
    return true;
}

void TcpServer::unitNet()
{
    
}

8、INetMediator.h

#pragma once
#include<iostream>
using namespace std;

//先声明有INet类,先用这个类名去定义指针,以后会编译到这里
class INet;

class INetMediator
{
public:
    INetMediator() {}
    virtual ~INetMediator() {}

    //打开网络
    virtual bool openNet() = 0;

    //发送数据
    virtual bool sendData(char* data, int len, unsigned long to = 0) = 0;

    //接收数据
    virtual void recvData(char* data, int len, unsigned long from) = 0;

    //关闭网络
    virtual void closeNet() = 0;
};

9、UdpMediator.h

#pragma once
#include"INetMediator.h"

class UdpMediator :public INetMediator
{
public:
    UdpMediator();
    ~UdpMediator();

    //打开网络
    virtual bool openNet();

    //发送数据(data:发送数据内容;len:发送数据长度;to:发给谁;)
    virtual bool sendData(char* data, int len, unsigned long to);

    //接收数据
    virtual void recvData(char* data, int len, unsigned long from);

    //关闭网络
    virtual void closeNet();
};

10、UdpMediator.cpp

#include"UdpMediator.h"

UdpMediator::UdpMediator()
{
    
}

UdpMediator::~UdpMediator()
{
    
}

bool UdpMediator::openNet()
{
    return true;
}

void UdpMediator::recvData(char* data, int len, unsigned long from)
{
    
}

bool UdpMediator::sendData(char* data, int len, unsigned long to)
{
    return true;
}

void UdpMediator::closeNet()
{

}

11、TcpClientMediator.h

#pragma once
#include"INetMediator.h"

class TcpClientMediator :public INetMediator
{
public:
    TcpClientMediator();
    ~TcpClientMediator();

    //打开网络
    virtual bool openNet();

    //发送数据
    virtual bool sendData(char* data, int len, unsigned long to = 0);

    //接收数据
    virtual void recvData(char* data, int len, unsigned long from);

    //关闭网络
    virtual void closeNet();
};

12、TcpClientMediator.cpp

#include"TcpClientMediator.h"

TcpClientMediator::TcpClientMediator()
{
    
}

TcpClientMediator::~TcpClientMediator()
{
    
}

bool TcpClientMediator::openNet()
{
    return true;
}

void TcpClientMediator::recvData(char* data, int len, unsigned long from)
{
    
}

bool TcpClientMediator::sendData(char* data, int len, unsigned long to)
{
    return true;
}

void TcpClientMediator::closeNet()
{

}

13、TcpServerMediator.h

#pragma once
#include"INetMediator.h"

class TcpServerMediator :public INetMediator
{
public:
    TcpServerMediator();
    ~TcpServerMediator();

    //打开网络
    virtual bool openNet();

    //发送数据(data:发送数据内容;len:发送数据长度;to:发给谁;)
    virtual bool sendData(char* data, int len, unsigned long to = 0);

    //接收数据
    virtual void recvData(char* data, int len, unsigned long from);

    //关闭网络
    virtual void closeNet();
};

14、TcpServerMediator.cpp

#include"TcpServerMediator.h"

TcpServerMediator::TcpServerMediator()
{
    
}

TcpServerMediator::~TcpServerMediator()
{
    
}

bool TcpServerMediator::openNet()
{
    return true;
}

bool TcpServerMediator::sendData(char* data, int len, unsigned long to)
{
    return true;
}

void TcpServerMediator::recvData(char* data, int len, unsigned long from)
{
    
}

void TcpServerMediator::closeNet()
{
    
}

15、源文件

#include<iostream>
using namespace std;
#include"net/TcpClient.h"
#include"net/TcpServer.h"
#include"net/Udp.h"
#include"mediator/TcpClientMediator.h"
#include"mediator/TcpServerMediator.h"
#include"mediator/UdpMediator.h"

int main()
{

    INet* p1 = new Udp;
    INet* p2 = new TcpServer;
    INet* p3 = new TcpClient;
    INetMediator* p4 = new UdpMediator;
    INetMediator* p5 = new TcpServerMediator;
    INetMediator* p6 = new TcpClientMediator;

    delete p1;
    delete p2;
    delete p3;
    delete p4;
    delete p5;
    delete p6;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值