1 Star 0 Fork 324

cosine_sine/cybersectookits

forked from openKylin/cybersectookits 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client and server.c 5.43 KB
一键复制 编辑 原始数据 按行查看 历史
vikol7 提交于 2023-05-19 10:49 . rename client.c to client and server.c.
#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);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cosinesine/cybersectookits.git
git@gitee.com:cosinesine/cybersectookits.git
cosinesine
cybersectookits
cybersectookits
master

搜索帮助