Linux下的通訊時延測試程式

來源:互聯網
上載者:User

標籤:linux   時延   網路   

今天段老師在網路軟體設計課上布置了一個題目。

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

運行結果為:


Linux下的通訊時延測試程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.