目录
gitee(内有详细代码)
图解
MessageRoute.hpp
UdpClient.hpp
UdpServer.hpp
Main.hpp
运行结果(本地通信)
如何分开对话显示?
gitee(内有详细代码)
chat_room · zihuixie/Linux_Learning - 码云 - 开源中国https://gitee.com/zihuixie/linux_-learning/tree/master/chat_room
图解
MessageRoute.hpp
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/socket.h>
#include <functional>
#include "InetAddr.hpp"
#include "ThreadPool.hpp"
#include "LockGuard.hpp"
// 通信模块:实现消息的转发
//只要有人发消息,就把该消息转发给所有的在线用户
using task_t =std::function<void()>;
class MessageRoute
{
private:
// 判断地址是否已保存数组中
bool IsExists(const InetAddr &addr)
{
for (auto a : _online_user)
{
if (a == addr)
return true;
}
return false;
}
public:
MessageRoute()
{
pthread_mutex_init(&_mutex,nullptr);
}
~MessageRoute()
{
pthread_mutex_destroy(&_mutex);
}
// 添加用户
void Adduser(const InetAddr &addr)
{
LockGuard lock(&_mutex);//保护数组临界资源
// 已存在则不添加
if (IsExists(addr))
return;
_online_user.push_back(addr);
}
// 删除用户
void Deluser(const InetAddr &addr)
{
LockGuard lock(&_mutex);//保护数组临界资源
// 不存在该用户,不需要删除
if (!IsExists(addr))
return;
// 用迭代器删除
for (auto iter = _online_user.begin(); iter != _online_user.end(); iter++)
{
if (*iter == addr)
{
_online_user.erase(iter);
break;
}
}
}
//转发消息
void RouteHelper(int sockfd,std::string message,InetAddr who)
{
LockGuard lock(&_mutex);
for(auto user:_online_user)
{
//设置要发送的消息
std::string send_message ="\n["+who.Ip()+":"+std::to_string(who.Port())+"]# "+message+"\n";
struct sockaddr_in clientaddr=user.Addr();
::sendto(sockfd,send_message.c_str(),send_message.size(),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr));
}
}
//通信模块
//哪个套接字,发了什么消息,谁发的
void Route(int sockfd,std::string message,InetAddr who)
{
Adduser(who);
//该用户要退出,删除该用户
if(message=="Q" || message=="QUIT")
Deluser(who);
task_t t=std::bind(&MessageRoute::RouteHelper,this,sockfd,message,who);
//让线程池来转发
ThreadPool<task_t>::GetInstance()->Enqueue(t);
}
private:
pthread_mutex_t _mutex;
std::vector<InetAddr> _online_user; // 用数组存储在线用户(用地址代表用户)
};
UdpClient.hpp
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Thread.hpp"
using namespace ThreadModule;
void Usage(std::string proc)
{
std::cout << "Usage:\n\t" << proc << " serverip serverport\n"
<< std::endl;
}
int InitClient(std::string &serverip, uint16_t serverport, struct sockaddr_in *server)
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
std::cerr << "socket error" << std::endl;
return -1;
}
// 初始化地址
memset(server, 0, sizeof(struct sockaddr_in));
server->sin_family = AF_INET;
server->sin_port = htons(serverport);
server->sin_addr.s_addr = inet_addr(serverip.c_str());
return sockfd;
}
// 接收消息
void recvmessage(int sockfd, std::string name)
{
while (true)
{
// 发送方的地址
struct sockaddr_in peer;
socklen_t len = sizeof(peer);
char buffer[1024];
// 获取接收方的地址
ssize_t n = recvfrom(sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&peer, &len);
if (n > 0)
{
buffer[n] = 0;
//向标准错误中输出(标准错误也是个文件描述符)
fprintf(stderr, "[%s]%s\n", name.c_str(), buffer);
}
}
}
void sendmessage(int sockfd, struct sockaddr_in &server, std::string name)
{
std::string message;
while (true)
{
printf("%s | Enter# ", name.c_str());
fflush(stdout);
std::getline(std::cin, message);
// 传接收方的地址
sendto(sockfd, message.c_str(), sizeof(message), 0, (struct sockaddr *)&server, sizeof(server));
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
Usage(argv[0]);
exit(1);
}
//填充地址
struct sockaddr_in serveraddr;
std::string serverip = argv[1];
uint16_t serverport = std::stoi(argv[2]);
int sockfd = InitClient(serverip, serverport, &serveraddr);
if (sockfd == -1)
{
return 1;
}
//定义函数并绑定参数
func_t r = std::bind(&recvmessage, sockfd, std::placeholders::_1);
func_t s = std::bind(&sendmessage, sockfd, serveraddr, std::placeholders::_1);
//创建两个线程:发消息 && 收消息
//就可以同时实现收消息 && 发消息 -- 全双工
//如果收消息 和 发消息 都由一个线程实现 -- 半双工
Thread Recver(r, "recver");
Thread Sender(s, "sender");
//启动线程
Recver.Start();
Sender.Start();
Recver.Join();
Sender.Join();
return 0;
}
UdpServer.hpp
#pragma once
#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <functional>
#include "InetAddr.hpp"
#include "Log.hpp"
enum
{
SOCKET_ERROR = 1, // 套接字创建失败
BIND_ERROR, // 绑定失败
USAGE_ERROR // 用法错误
};
const static int defaultfd = -1; // 默认描述符的值为-1
// 接收任务
using hander_message_t = std::function<void(int sockfd, const std::string message, const InetAddr who)>;
class UdpServer
{
public:
// 外界需要传端口号
UdpServer(uint16_t port, hander_message_t hander_message)
: _port(port), _sockfd(defaultfd), _isrunning(false), _hander_message(hander_message)
{
}
void InitServer()
{
// 创建套接字
_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (_sockfd < 0) // 创建失败
{
LOG(FATAL, "socket error, %s, %d\n", strerror(errno), errno);
exit(SOCKET_ERROR);
}
LOG(INFO, "socket create success, sockfd: %d\n", _sockfd);
// 填 sockaddr_in
struct sockaddr_in local; // 在栈上开辟空间
bzero(&local, sizeof(local)); // 将内存设置为全0
local.sin_family = AF_INET; // 协议族(用哪个协议)
local.sin_port = htons(_port); // 端口号,转为网络序列(大端)
local.sin_addr.s_addr = INADDR_ANY; // IP地址(任意地址)
int n = bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
// 绑定失败
if (n < 0)
{
LOG(FATAL, "bind error,%s,%d\n", strerror(errno), errno);
exit(BIND_ERROR);
}
LOG(INFO, "socket bind success\n");
}
// 启动服务器,服务器只接收信息和做应答
void Start()
{
_isrunning = true;
while (true)
{
char message[1024];
struct sockaddr_in peer; // 发送方的地址
socklen_t len = sizeof(peer);
// 接收数据报,返回值为接收到的字节数
ssize_t n = recvfrom(_sockfd, message, sizeof(message) - 1, 0, (struct sockaddr *)&peer, &len);
if (n > 0)
{
message[n] = 0;
InetAddr addr(peer); // 发送方的IP和端口号
LOG(DEBUG, "get message from [%s:%d]:%s\n", addr.Ip().c_str(), addr.Port(), message);
//转发消息
_hander_message(_sockfd,message,addr);
}
_isrunning = false;
}
}
~UdpServer()
{
}
private:
int _sockfd;
uint16_t _port;
bool _isrunning;
hander_message_t _hander_message;
};
Main.hpp
#include<iostream>
#include<memory>
#include"UdpServer.hpp"
#include"MessageRoute.hpp"
void Usage(std::string proc)
{
std::cout<<"Usage:\n\t"<<proc<<" local_port\n"<<std::endl;
}
int main(int argc,char* argv[])
{
if(argc!=2)
{
Usage(argv[0]);
exit(USAGE_ERROR);
}
EnableScreen();
MessageRoute route;//创建一个实例
uint16_t port=std::stoi(argv[1]);
std::unique_ptr<UdpServer> usvr=std::make_unique<UdpServer>(port,std::bind(&MessageRoute::Route,&route,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));
usvr->InitServer();
usvr->Start();
return 0;
}
运行结果(本地通信)
如何分开对话显示?
在启动客户端时,打开两个对话,输入命令 ./udpclient 127.0.0.1 8080 2>/dev/pts/2