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, &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->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