C-socklib.c

Source: Internet
Author: User
Tags htons

/*SOCKLIB.C * This file contains functions used lots when writing Internet client/server programs. The both main function here is: *-------int Make_server_socket (portnum) return a server socket OR-1 if error *-------I NT Make_server_socket_q (portnum,backlog) *-------int connect_to_server (char * hostname,int portnum) return a connected s Ocket or-1 If error*/#include<stdio.h>#include<unistd.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/inch.h>#include<netdb.h>#include<time.h>#include<strings.h>#defineHostlen 256#defineBACKLOG 1intMake_server_socket_q (int,int); intMake_server_socket (intportnum) {     returnmake_server_socket_q (portnum,backlog);} intMake_server_socket_q (intPortnum,intbacklog) {     structSockaddr_in saddr;/*build our address here*/     structHostent * HP;/* This was part of our*/     CharHostname[hostlen];/*Address*/     intsock_id;/*The socket*/sock_id= Socket (Pf_inet,sock_stream,0);/*get a socket*/     if(sock_id = =-1)         return-1; /*Build address Andbind it to socket*/bzero ((void*) &saddr,sizeof(SADDR));/*clear out struct*/gethostname (Hostname,hostlen); /*where am I?*/HP= gethostbyname (hostname);/*get Infoabout host fill in host part*/bcopy ((void*) HP-H_ADDR, (void*) &saddr.sin_addr,hp->h_length); Saddr.sin_port= Htons (Portnum);/*fill in Socket port*/saddr.sin_family= Af_inet;/*fill in addr family*/     if(Bind (sock_id, (structSOCKADDR *) &saddr,sizeof(SADDR)) !=0)         return-1; /*arrange for incoming calls*/     if(Listen (sock_id,backlog)! =0)         return-1; returnsock_id;} intConnect_to_server (Char* Host,intportnum) {     intsock; structSockaddr_in Servadd;/* the number to call*/     structHostent *hp;/*used to get number*/     /*step 1:get a socket*/sock= Socket (Af_inet,sock_stream,0); if(Sock = =-1)         return-1; /*Step 2:connect to server*/bzero (&servadd,sizeof(Servadd));/*Zero The address*/HP= gethostbyname (host);/*Lookup Host ' s IP #*/     if(HP = =NULL)return-1; Bcopy (HP->h_addr, (structSOCKADDR *) &servadd.sin_addr,hp->h_length); Servadd.sin_port= Htons (Portnum);/*fill in Port number*/servadd.sin_family= Af_inet;/*fill in socket type*/     if(Connect (sock,structSOCKADDR *) &servadd,sizeof(Servadd)) !=0)         return-1; returnsock;}

C-socklib.c

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.