1 Star 2 Fork 0

朱鸿/DNS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
朱鸿 提交于 2022-06-30 20:41 . update
//难点1每次回应报文有好几千,怎么每次都解析出正确的内容#
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#include <cstring>
#include <netinet/in.h>
#include <ctype.h>
#include <iostream>
#include <netdb.h>
#include <ifaddrs.h>
#include <malloc.h>
#include <errno.h>
#include <stdint.h>
#include "ThreadPool.h"
#include "name.h"
#include "Lru.h"
#include "RateLimiter.h"
using namespace std;
int main(int argc,char *argv[])
{
if (argc < 2) {
cout<<"please input dns port!"<<endl;
cout<<"case : program_name port"<<endl;
return 1;
}
//获取dns端口号
int dns_port = 0;
int dns_port_off = 1;
for (int i = 0; i < strlen(argv[1]); i++) {
dns_port = *(argv[1] + i ) - '0' + dns_port * 10;
}
//socket绑定
int socketfd = socket(AF_INET,SOCK_DGRAM,0);
perror("socket");
//socket绑定的本地地址
struct sockaddr_in local_dns_server_address;
local_dns_server_address.sin_port = htons(dns_port);
local_dns_server_address.sin_addr.s_addr = htonl(INADDR_ANY);
local_dns_server_address.sin_family = AF_INET;
bind(socketfd, (struct sockaddr *)&local_dns_server_address, sizeof(struct sockaddr));
perror("bind");
//创建线程池
ThreadPool<name> * pool = NULL;
try{
pool = new ThreadPool<name>;
} catch(...) {
printf("线程池内存分配错误!\n");
return 1;
}
socklen_t len = sizeof(struct sockaddr);
Lru_2 *mainlru = new Lru_2(200, 20);
RateLimiter r(100000);//10wqps
//开始工作
while (true) {
name *newName = new name(dns_port_off++, dns_port);
newName->mainSocketfd = socketfd;
newName->lru = mainlru;
newName->r_ptr = &r;
int count = recvfrom(socketfd,newName->buf,sizeof(newName->buf),0,(struct sockaddr *)&(newName->clientAddress),&len);
newName->count = count;
pool->list_append(newName);
}
close(socketfd);
delete pool;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zhu-hong202257887/DNS-Server.git
[email protected]:zhu-hong202257887/DNS-Server.git
zhu-hong202257887
DNS-Server
DNS
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385