Linux C Socket Package

Source: Internet
Author: User

/************************************************************************** * Linux C Socket Package * Statement: * The same Code of course no need to knock many times, once enough, sealed up, when to use, * when to come here to take. * 2015-7-4 Sunny Shenzhen Nanshan Ping Shan village Zengjianfeng ************************************************************ *************/                    \\\\\\\\-*-Directory-*-////////|One, cat zengjf_socket.h|second, cat zengjf_socket.c \\\\\\\\\\\\\\//////////////One, cat zengjf_socket.h #ifndef __zengjf_socket_h__#define__zengjf_socket_h__#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<unistd.h>#defineHandle_error (msg) {perror (msg); exit (exit_failure);}/** * Client Socket * Parameter Description: * 1.IP: Server IP address, dotted decimal IP String * 2.port: Server port number, string form * 3.SRVADDR: Connect to server-side sockaddr_in*/        intSocketclient (Char* IP,Char* Port,structsockaddr_in*srvaddr); /** * Server-side Socket * Parameter Description: * 1.port: Listening port number, is the form of String * 2.srvaddr: Server sockaddr_in * * * Server-side socket can monitor 30 connections by default*/        intSocketserver (Char* Port,structsockaddr_in*srvaddr); #endif //__zengjf_socket_h__second, cat zengjf_socket.c #include"zengjf_socket.h"        intSocketclient (Char* IP,Char* Port,structsockaddr_in*srvaddr) {            intSFD; //Create a socket        if(SFD = socket (af_inet, Sock_stream,0)) == -1) {Handle_error ("can not create socketclient."); }            //set the server socket addresssrvaddr->sin_family =af_inet; Srvaddr->sin_port =htons (Atoi (port)); Inet_aton (IP,& (srvaddr->sin_addr)); //Connect        if(Connect (SFD, (structSOCKADDR *) Srvaddr,sizeof(structSOCKADDR)) = =-1) {Handle_error ("can not connect to server."); }            returnSFD; }        intSocketserver (Char* Port,structsockaddr_in*srvaddr) {            intsrv_fd; //The default listen Count        intListencount = -; //Create a socket        if(SRV_FD = socket (af_inet, Sock_stream,0)) == -1) {Handle_error ("can not create socketserver."); }            //reuse the socket address        intSockopt =1; SetSockOpt (SRV_FD, Sol_socket, SO_REUSEADDR,&sockopt,sizeof(sockopt)); //set the socket addresssrvaddr->sin_family =af_inet; Srvaddr->sin_port =htons (Atoi (port)); Srvaddr-&GT;SIN_ADDR.S_ADDR =htonl (Inaddr_any); //bind the socket with the address        if(Bind (SRV_FD, (structSOCKADDR *) Srvaddr,sizeof(structSOCKADDR)) = =-1) {Handle_error ("can not bind the address."); }            //Listening//default was        if(Listen (SRV_FD, listencount) = =-1) {Handle_error ("Can not be listening to the port."); }            returnsrv_fd; }

Linux C Socket Package

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.