Linux实现can通信时延小,Linux下的通信时延测试程序

今天段老师在网络软件设计课上布置了一个题目。

要求是windows环境,现在在linux环境下实现。

运行C/S模式的2个程序,使用UDP协议,发送10次,计算平均时延。

服务器程序如下:

#include // for functions for socket

#include // for struct sockaddr_in

#include

#include // for memset

#include // for perror

#include // for errno

#define BUFLEN 100

int main(void)

{

int listenfd;

char buf[BUFLEN];

socklen_t len;

struct sockaddr_in serv, cli;

if ((listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

{ // use udp

perror("socket");

exit(EXIT_FAILURE);

}

memset(&serv, 0, sizeof(serv)); // clear

serv.sin_family = AF_INET; // use IPv4

// Listen any ip address and use network

// byte order

serv.sin_addr.s_addr = htonl(INADDR_ANY);

serv.sin_port = htons(9877); // Listen port 9877

if (bind(listenfd, (struct sockaddr*)&serv, sizeof(serv)) < 0)

{ // bind the socket to the server address

perror("bind");

exit(EXIT_FAILURE);

}

for ( ; ; )

{

len = sizeof(cli);

if ((recvfrom(listenfd, buf, BUFLEN, 0,

(struct sockaddr*)&cli, &len)) < 0)

{ // recvfrom the listenfd

// put the message into buf

// no flags (4th argument 0)

// save the client address and length

if (errno == EINTR || errno == EAGAIN)

continue;

perror("recvfrom");

exit(EXIT_FAILURE);

}

if ((sendto(listenfd, "Got it!", 10, 0,

(struct sockaddr*)&cli, len)) < 0)

{ // send the message back

// send message "Got it!" back

if (errno == EINTR || errno == EAGAIN)

continue;

perror("sendto");

exit(EXIT_FAILURE);

}

}

return 0;

}

客户端程序如下:

#include // for functions for socket

#include // for inet_pton

#include // for struct sockaddr_in

#include

#include // for memset

#include // for perror

#include // for errno

#include

#define SENDTIMES 10

#define BUFLEN 100

int main(int argc, char* argv[])

{

struct sockaddr_in serv;

int sockfd;

int i;

clock_t start, finish;

double duration, total = 0;

char buf[BUFLEN];

if (argc != 2)

{

fprintf(stderr, "Usage: ./udpcli \n");

exit(EXIT_FAILURE);

}

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

{ // use udp

perror("socket");

exit(EXIT_FAILURE);

}

memset(&serv, 0, sizeof(serv));

serv.sin_family = AF_INET;

serv.sin_port = htons(9877);

if ((inet_pton(AF_INET, argv[1],

&serv.sin_addr)) == 0)

{ // read the string to the structrue

perror("inet_pton");

exit(EXIT_FAILURE);

}

again:

if ((connect(sockfd,

(struct sockaddr*)&serv, sizeof(serv))) < 0)

{ // this connect is for catch the

// error ICMP packets

if (errno == EINTR || errno == EAGAIN)

goto again;

perror("connect");

exit(EXIT_FAILURE);

}

for (i = 0; i < SENDTIMES; ++i)

{

printf("Send %d messages.\n", i + 1);

start = clock();

again2:

if ((sendto(sockfd, "A message!", 20, 0,

(struct sockaddr*)&serv, sizeof(serv))) < 0)

{

if (errno == EINTR)

goto again2;

perror("sendto");

exit(EXIT_FAILURE);

}

again3:

if ((recvfrom(sockfd, buf, BUFLEN, 0,

NULL, NULL)) < 0)

{

if (errno == EINTR)

goto again3;

perror("recvfrom");

exit(EXIT_FAILURE);

}

finish = clock();

duration = finish - start;

printf("Spend time: %fms\n", duration);

total += duration;

}

printf("\nAverage time: %fms\n", total / SENDTIMES);

return 0;

}

运行结果为:

25ec6a4852dbd3aa97c288eaf5814382.pngrmark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGRjeHMwMDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"

style="display: block;" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值