Webbench Source Code Analysis

Source: Internet
Author: User

Webbench mainly consists of two files.

Socket.c

Used to create a socket

/*$Id: socket.c 1.1 1995/01/01 07:11:14 Cthuang EXP $ * * This module have been modified by Radim Kolar for OS/2 emx *//*********************************************************************** module:socket.c Program:popclien T SCCS ID: @ (#) socket.c 1.5 4/1/94 programmer:virginia Tech Computing Center compiler:dec RISC c Compi Ler (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/inch.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>intSocket (Const Char*host,intclientport) {/** * Establish a socket with the specified host name and port number and return * * Host hostname * * ClientPort client port number*/    intsock; unsignedLonginaddr; structsockaddr_in AD; /*struct SOCKADDR_IN {short sin_family;       Protocol cluster type Af_inet->ipv4, Af_inet6->ipv6 unsigned short sin_port; Port number htons (3490) htons = = host to net short//convert host byte order to network byte order struct       IN_ADDR sin_addr;    IP address char sin_zero[8];    8 bytes Zero if you want to};    struct IN_ADDR {unsign long s_addr;//Inet_pton () returns unsigned long shaping IP address padding}; */    structHostent *HP; /*struct Hostent {char *h_name;          Char **h_aliases;          int h_addrtype;          int h_length;      Char **h_addr_list;      }; */    //Initialize Addressmemset (&ad,0,sizeof(AD)); Ad.sin_family=af_inet; //Try to convert the hostname to a number//because the input may be an IP address, or it may be a domain name//so think about it separately .INADDR = inet_addr (host);//try dotted decimal to unsigned long shaping    if(inaddr! = Inaddr_none)//Inaddr_none represents an invalid IP addressmemcpy (&ad.sin_addr, &AMP;INADDR,sizeof(INADDR));//If an IP address is entered, write directly to the sockaddr_in    Else    {        //If you get the input domain nameHP = gethostbyname (host);//returns a hostent structure pointer that contains host name and address information for a given host name        if(HP = =NULL)return-1; memcpy (&ad.sin_addr, HP-&GT;H_ADDR, hp->h_length);//} ad.sin_port= Htons (ClientPort);//populating native use ports//Creating SocketsSock = socket (af_inet, Sock_stream,0); //Error    if(Sock <0)        returnsock; //Establish links//the programmer populates the SOCKADDR_IN structure with the type, IP address, Port ,//then cast to sockaddr, passed as a parameter to the system call function    if(Connect (sock,structSOCKADDR *) &ad,sizeof(AD)) <0)              return-1; returnsock;}

Webbench Source Code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.