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

要求是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 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 <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 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 <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);
    total += duration;
  }
  printf("\nAverage time: %fms\n", total / SENDTIMES);
  return 0;
}

运行结果为:

<img src="https://img-blog.csdn.net/20150312120444160?wate%3Ca%20href=" http:="" www.ahlinux.com="" start="" cmd="" 9034.html"="" target="_blank" class="keylink" style="margin: 0px; padding: 3px; border: 1px solid rgb(204, 204, 204); max-width: 660px;">rmark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGRjeHMwMDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="display: block;" />

  • 本文来自:Linux教程网

Linux下的通信时延测试程序相关推荐

  1. linux下TCP通信简单实例

    linux下TCP通信简单实例 基于TCP(面向连接)的socket编程,分为服务器端和客户端 服务器端的流程如下: (1)创建套接字(socket) (2)将套接字绑定到一个本地地址和端口上(bin ...

  2. Linux下进程通信的八种方法

    Linux下进程通信的八种方法:管道(pipe),命名管道(FIFO),内存映射(mapped memeory),消息队列(message queue),共享内存(shared memory),信号量 ...

  3. 【流媒体服务器Mediasoup】 NodeJs与C++信令通信详解及Linux下管道通信的详解(五)

    目录 前言 匿名管道进程间通信 进程间管道 的创建与图解 MediaSoup中的管道创建 MediaSoup Channel的创建 NodeJs和 C++ 管道通信的过程 MediaSoup 消息确认 ...

  4. Linux下进程通信知识点学习笔记(一)

    4种主要事件导致进程创建: 系统的初始化: 执行了正在运行的进程所调用的进程创建系统调用: 用户请求创建一个进程: 一个批处理作业的初始化: 进程的终止: 正常退出: 出错退: 严重错误: 被其他进程 ...

  5. linux下串口通信程序,关于Linux下串口通信的一点心得

    1. 打开串口 与其他的关于设备编程的方法一样,在 Linux 下,操作.控制串口也是通过操作起设备文件进行的.在 Linux 下,串口的设备文件是 /dev/ttyS0 或 /dev/ttyS1 等 ...

  6. Linux 下socket通信终极指南(附TCP、UDP完整代码)

    linux下用socket通信,有TCP.UDP两种协议,网上的很多教程把两个混在了一起,或者只讲其中一种.现在我把自己这两天研究的成果汇总下来,写了一个完整的,适合初学者参考,也方便自己以后查阅. ...

  7. Linux下Socket通信中非阻塞connect、select、recv 和 recvfrom、send和sendto大致讲解,附带非租塞connect代码、MSG_NOSIGNAL

    linux中send函数MSG_NOSIGNAL异常消息 在服务器端用ctrl+c 来结束服务器接收进程来模拟服务器宕机的情况,结束服务 socket 进程之后,服务端自然关闭进程,可是 client ...

  8. linux下串口通信详解,Linux操作系统下的串口通信学习笔记

    http://www.diybl.com/ 2008-7-5 网络 点击: [ 评论 ] - - 文章搜索:     [点击打包该文章] [本站开通在线QQ讨论群] CBAUDEX (不属于POSIX ...

  9. linux通信管道破裂,Linux下进程通信之管道

    每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把 ...

最新文章

  1. 测试MindMotion MM32F3277 MicroPython -2021-11-20新增PWM版本
  2. Mac 技术篇-pip下载速度慢解决办法,pip秒速下载,阿里云镜像配置
  3. SqlServer中怎样从Excel中导入数据
  4. 华大 MCU 之五 SPI 从机 DMA 模式 配置(不能正常接收问题处理)
  5. 敏捷软件开发 12 原则
  6. java 内存泄露 书籍_java虚拟机内存溢出和泄漏实例
  7. 看视频课程的正确方法
  8. 熊猫python小课靠谱吗_新的投资风向标在哪里?
  9. 数据平台建设的几种方案
  10. 三万字带你了解那些年面过的Java八股文
  11. java上下文的作用_Spring中的应用程序上下文有什么作用? - java
  12. 计算机itunes无法安装,itunes无法安装怎么办 itunes不能安装解决方法
  13. homebre mysql 启动_Mysql闪退问题图文解决办法
  14. MySQL查询数据---单表查询
  15. 融云2.8.8简单的陌生人聊天
  16. Windows 10出现0xc0000225错误代码如何解决?
  17. 28 爬虫 - re 正则 匹配中文
  18. 马德里的Uber司机
  19. 中科大和华师大计算机,今天就是你最后的机会,2019华师软件工程跨考经验
  20. GBA程序开发入门1

热门文章

  1. 编程珠玑——取样总结
  2. linux空格键命令,linux 命令(快捷键)
  3. 夫琅禾费单缝衍射matlab分析,夫琅禾费单缝衍射光强分布MATLAB分析毕业设计论文...
  4. 夫琅禾费单缝衍射matlab分析,夫琅禾费单缝衍射光强分析与探讨
  5. Bcftools的下载与安装
  6. Vue 计算属性和ref的使用方法
  7. 深圳神目信息:打造AI+物联网开发中台, 赋能万物智联
  8. 浙江python信息技术教材_人工智能、Python…浙江省三到九年级将使用信息技术新修订教材...
  9. 为什么现在很多年轻人愿意到北上广深打拼,即使过得异常艰苦,远离亲人,仍然义无反顾?
  10. 推荐系统基础(2):个性化推荐系统简述