webbench源碼剖析

來源:互聯網
上載者:User

標籤:struct   參數傳遞   網路位元組順序   cli   inet_addr   else   wan   bytes   lis   

webbench主要有兩個檔案組成。

socket.c

用來建立一個socket

/* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $ * * This module has been modified by Radim Kolar for OS/2 emx *//***********************************************************************  module:       socket.c  program:      popclient  SCCS ID:      @(#)socket.c    1.5  4/1/94  programmer:   Virginia Tech Computing Center  compiler:     DEC RISC C compiler (Ultrix 4.1)  environment:  DEC Ultrix 4.3   description:  UNIX sockets code. ***********************************************************************/ #include <sys/types.h>#include <sys/socket.h>#include <fcntl.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/time.h>#include <string.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>int Socket(const char *host, int clientPort)  {/* ** 用指定主機名稱和連接埠號碼建立一個socket並返回** host 主機名稱** clientPort 用戶端連接埠號碼*/    int sock;    unsigned long inaddr;    struct sockaddr_in ad;    /*    struct sockaddr_in     {        short            sin_family;     // 協議簇類型 AF_INET->ipv4 , AF_INET6->ipv6        unsigned short   sin_port;       // 連接埠號碼 htons(3490)  htons == host to net short                                          // 將主機位元組順序轉變成網路位元組順序        struct in_addr   sin_addr;       // ip地址        char             sin_zero[8];    // 8 bytes zero this if you want to    };    struct in_addr    {        unsign long s_addr; //用inet_pton()返回的無符號長整形ip地址填充    };    */    struct hostent *hp;    /*    struct hostent     {          char *h_name;          char **h_aliases;          int h_addrtype;          int h_length;          char **h_addr_list;      };      */    // 初始化地址     memset(&ad, 0, sizeof(ad));    ad.sin_family = AF_INET;    // 嘗試把主機名稱轉化為數字     // 因為輸入的有可能是ip地址,也有可能是網域名稱    // 所以要分開考慮    inaddr = inet_addr(host);         //嘗試驗分十進位轉為無符號長整形    if (inaddr != INADDR_NONE)        //INADDR_NONE 代表無效的ip地址         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));  //如果輸入的是ip地址,直接寫入sockaddr_in    else    {        // 如果取得輸入的是網域名稱        hp = gethostbyname(host); //返回對應於給定主機名稱的包含主機名稱字和地址資訊的hostent結構指標        if (hp == NULL)            return -1;        memcpy(&ad.sin_addr, hp->h_addr, hp->h_length); //    }    ad.sin_port = htons(clientPort);  //填充本機使用連接埠    // 建立 socket     sock = socket(AF_INET, SOCK_STREAM, 0);    // 出錯    if (sock < 0)        return sock;    // 建立連結     // 程式員把類型、ip地址、連接埠填充sockaddr_in結構體,    // 然後強制轉換成sockaddr,作為參數傳遞給系統調用函數    if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)              return -1;    return sock;}

 

webbench源碼剖析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.