代码拉取完成,页面将自动刷新
//难点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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。