Linux下的通信时延测试程序
来源:互联网 发布:mac上使用的ios模拟器 编辑:程序博客网 时间:2024/04/30 12:04
今天段老师在网络软件设计课上布置了一个题目。
要求是windows环境,现在在linux环境下实现。
运行C/S模式的2个程序,使用UDP协议,发送10次,计算平均时延。
服务器程序如下:
#include <sys/socket.h> // for functions for socket#include <netinet/in.h> // for struct sockaddr_in#include <stdlib.h>#include <memory.h> // for memset#include <stdio.h> // for perror#include <errno.h> // for errno#define BUFLEN 100int 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 <sys/socket.h> // for functions for socket#include <arpa/inet.h> // for inet_pton#include <netinet/in.h> // for struct sockaddr_in#include <stdlib.h>#include <memory.h> // for memset#include <stdio.h> // for perror#include <errno.h> // for errno#include <time.h>#define SENDTIMES 10#define BUFLEN 100int 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 <IPADDR>\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 / CLOCKS_PER_SEC * 1000); total += duration; } printf("\nAverage time: %fms\n", total / SENDTIMES / CLOCKS_PER_SEC * 1000); return 0;}
1 0
- Linux下的通信时延测试程序
- Linux下的通信时延测试程序
- linux下的管道通信程序
- linux下的管道通信程序
- linux下的串口通信程序详解
- Bluez下的rfcomm层通信测试程序
- Bluez下的rfcomm层通信测试程序
- linux下测试程序运行的时间
- Linux 下HelloWorld程序的一些测试
- Linux下测试程序的运行时间
- linux下测试程序运行的时间
- Linux下测试程序的运行时间
- UNIX下TCP/IP通信测试程序
- Linux下两个程序进行socket通信的简单例子
- linux下基于TCP简单的socket通信程序
- Linux下两个程序进行socket通信的简单例子
- linux下简单的UDP对等通信程序
- Linux下基于QT的串口通信程序(Tiny6410)
- 快速开启和关闭Sql Server服务
- ViewPager滑动过程中page之间出现白条
- Android 属性动画(Property Animation) 源码解析 深入了解其内部实现
- strlen 和 sizeof()不同点
- Struts2入门示例教程
- Linux下的通信时延测试程序
- const 与#define区别
- malloc() & free()函数区别
- hdu 5178
- 关于卸载软件不成功,导致再次安装软件出错的问题
- 黑马程序员---C语言--流程控制和函数
- strcpy和memcpy的不同
- linux 修改虚拟内存
- ffplay调节音量大小