代码拉取完成,页面将自动刷新
同步操作将从 openKylin/cybersectookits 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/time.h>
#include<string.h>
#include<time.h>
#include"struct.h"
#define MAX_BUF_SIZE 1500
#define INTERVAL 1000
#define ll long long
#define INF 0x3f3f3f3f
#define SENDSIZE 48 //设置包大小和发送间隔以控制流量
int main(int argc, char* argv[])
{
if(argc != 3)
{
printf("Usage: ./%s IP port_number",argv[0]);
exit(1);
}
char sendbuf[MAX_BUF_SIZE];
char recvbuf[MAX_BUF_SIZE];
int sockfd;
int scale_size = 30;
int test_num = 1000;
ll min_time[50];
struct timeval sendtimeval,send_interval;
struct sockaddr_in hostaddr;
struct timeval recvtime;
long time,receive_time;
long firsttime,sencondtime;
struct udp_packet udp_data;
int len = sizeof(struct sockaddr_in);
memset(min_time,INF,sizeof(min_time));
hostaddr.sin_family = AF_INET;
hostaddr.sin_port = htons(atoi(argv[2]));
hostaddr.sin_addr.s_addr = inet_addr(argv[1]);
send_interval.tv_sec = 0;
send_interval.tv_usec = INTERVAL;
if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0){
printf("Creat Socket Failed !\n");
exit(1);
}
else
printf("Create Socket Success !\n");
udp_data.seq = 0;
for(int i=1;i<=scale_size;i++){
for(int j=0;j<test_num;j++){
send_interval.tv_sec = 0;
send_interval.tv_usec = INTERVAL;
int temp = select(0, NULL, NULL, NULL, &send_interval);
if(temp == -1){
continue;
}
// sleep(1);
gettimeofday(&sendtimeval,0);
printf("%u %u\n",sendtimeval.tv_sec,sendtimeval.tv_usec);
udp_data.sendtime = 1000000 * sendtimeval.tv_sec + sendtimeval.tv_usec;
memset(sendbuf,0x00,MAX_BUF_SIZE);
memcpy(sendbuf,&udp_data,sizeof(struct udp_packet));//!!
printf("packetsize is : %d\n",SENDSIZE);
if ((sendto(sockfd,sendbuf,i*SENDSIZE,0,(struct sockaddr*)&hostaddr,sizeof(hostaddr))) < 0 ){
printf("Fail to send the udp packet \n");
}
else{
printf("seq is: %ld \n",udp_data.seq);
printf("local send time is: %ld\n",udp_data.sendtime);
}
int ret = recvfrom(sockfd,recvbuf,MAX_BUF_SIZE,0,(struct sockaddr *)&hostaddr,&len);
if(ret > 0){
printf("Received data from server!\n", recvbuf);
}
gettimeofday(&recvtime,0);
receive_time = 1000000*recvtime.tv_sec + recvtime.tv_usec;
time = receive_time - udp_data.sendtime;
printf("Received interval: %ld\n\n", time);
if( time < min_time[i]) min_time[i] = time;
// min_time[i] = min(time, min_time[i]);
udp_data.seq++;
}
}
close(sockfd);
return 0;
}
##server.c
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<errno.h>
#include"struct.h"
#define MAX_BUF_SIZE 1500
#define SENDSIZE 48
int main(int argc,char* argv[])
{
if (argc != 2)
{
printf("./%s portnumber",argv[0]);
exit(1);
}
int sockfd;
long localtime;
long time;
long x;
FILE *fp;
struct sockaddr_in localaddr,remoteaddr;
struct timeval recvtime;
char recvbuf[MAX_BUF_SIZE];
char * sendbuf;
struct udp_packet udp_data;
memset(&localaddr,0x00,sizeof(struct sockaddr_in));
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(atoi(argv[1]));
localaddr.sin_addr.s_addr = htonl(INADDR_ANY);//本机IP
if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
printf("Create socket failed !\n");
exit(1);
}
else
printf("Create socket success !\n");
if ((bind(sockfd,(struct sockaddr*)&localaddr,sizeof(localaddr))) < 0)
{
printf("Bind port failed !\n");
exit(1);
}
else
printf("Bind port success !\n");
if ((fp = fopen("./recvdata.txt","w+")) == NULL )
{
perror("fopen error");
exit(1);
}
else
printf("Success to create the receive file!\n");
int remoteaddr_len = sizeof(struct sockaddr);
while(1)
{
if (recvfrom(sockfd,recvbuf,MAX_BUF_SIZE,0,(struct sockaddr*)&remoteaddr,&remoteaddr_len) < 0)
{
printf("Fail to receive data!\n");
}
else
{
printf("receiving... \n");
struct udp_packet* t_data = (struct udp_packet*)recvbuf;
gettimeofday(&recvtime,0);
localtime = 1000000*recvtime.tv_sec + recvtime.tv_usec;
x=t_data -> sendtime;
time=localtime-x;
// printf("Returing1...\n");
fprintf(fp,"%ld %ld %ld %ld\n", t_data -> seq, localtime, t_data -> sendtime,time);//输出到txt文件方便统计
// printf("Returing2...\n");
sendbuf = "Send Message Success!";
int ret = sendto(sockfd,sendbuf,SENDSIZE,0,(struct sockaddr*)&remoteaddr,sizeof(remoteaddr));
if(ret < 0){
printf("Failed to return client message!");
}else{
printf("Return Success!\n\n");
}
}
}
fclose(fp);
close(sockfd);
return(0);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。