Socket-server and client description

Source: Internet
Author: User
Tags htons

Server description # include <errno. h> // handle some error messages # include <stdio. h> // standard input and output # include <sys/types. h> // Basic System Data Type # include <netinet/in. h> // Internet address family defines the address family like sockaddr_in # include <sys/socket. h> // socket # include <unistd. h> // the header file of the system-defined symbolic constant, which contains the function prototypes of many UNIX system services, such as read and write Functions # include <netdb. h> // netdb. h defines network-related structures, variable types, macros, and functions. For example, struct hostent * gethostbyaddr (const void * addr, size_t len, int type); # include <arpa/inet. h> // defines inet_addr # include <fcntl. h> // fcntl. h defines many macros and open, fcntl function prototype close, etc. # include <stdlib. h> // standard library # include <string. h> // stringint main (int argc, const char * argv []) {int sfp, nfp; // socket descriptor struct sockaddr_in s_add, c_add; // The server and client addresses unsigned int sin_size; unsigned short portnum = 3000; // bind the port number printf ("Hello, Welcome to my server! \ R \ n "); sfp = socket (AF_INET, SOCK_STREAM, 0); // socket descriptor if (-1 = sfp) // if it fails {printf ("socket fail! \ R \ n "); return-1;} printf (" socket OK! \ R \ n "); bzero (& s_add, sizeof (struct sockaddr_in); // clear 0 s_add.sin_family = AF_INET; s_add.sin_addr.s_addr = htonl (INADDR_ANY ); // any address s_add.sin_port = htons (portnum); // port number if (-1 = bind (sfp, (struct sockaddr *) (& s_add ), sizeof (struct sockaddr) // if binding fails {printf ("bind fail! \ R \ n "); return-1;} printf (" bind OK! \ R \ n "); if (-1 = listen (sfp, 5) // if listen fails {printf (" listen fail! \ R \ n "); return-1;} printf (" listen OK \ r \ n "); while (1) {sin_size = sizeof (struct sockaddr_in ); nfp = accept (sfp, (struct sockaddr *) (& c_add), & sin_size); // continuously accept // If accpet succeeds, some client information is in c_add if (-1 = nfp) // failed {printf ("accept fail! \ R \ n "); return-1;} printf (" accept OK! \ R \ nServer start get connect from % # x: % # x \ r \ n ", ntohl (c_add.sin_addr.s_addr), ntohs (c_add.sin_port )); // print the client address and port if (-1 = write (nfp, "hello, welcome to my server \ r \ n", 32 )) // write data {printf ("write fail! \ R \ n "); return-1;} printf (" write OK! \ R \ n "); close (nfp); // close} close (sfp); // close return 0;} client description # include <errno. h> # include <stdio. h> # include <sys/types. h> # include <netinet/in. h> # include <sys/socket. h> # include <unistd. h> # include <netdb. h> # include <arpa/inet. h> # include <fcntl. h> # include <stdlib. h> # include <string. h> int main () {int cfd; // socket descriptor ssize_t recbytes; // accept byte length char buffer [1024] = {0 }; // receive the byte cache area struct sockaddr_in s_add; // The connected address un Signed short portnum = 3000; // connection port printf ("Hello, welcome to client! \ R \ n "); cfd = socket (AF_INET, SOCK_STREAM, 0); if (-1 = cfd) {printf (" socket fail! \ R \ n "); return-1;} printf (" socket OK! \ R \ n "); bzero (& s_add, sizeof (struct sockaddr_in); // clear s_add.sin_family = AF_INET; s_add.sin_addr.s_addr = inet_addr (" 192.168.1.101 "); s_add.sin_port = htons (portnum); printf ("s_addr = % # x, port: % # x \ r \ n", s_add.sin_addr.s_addr, s_add.sin_port ); if (-1 = connect (cfd, (struct sockaddr *) (& s_add), sizeof (struct sockaddr) // start to connect {printf ("connect fail! \ R \ n "); return-1;} printf (" connect OK! \ R \ n "); ssize_t num = 1024; if (-1 = (recbytes = read (cfd, buffer, num ))) // If read fails {printf ("read data fail! \ R \ n "); return-1;} printf (" read OK \ r \ nREC: \ r \ n "); buffer [recbytes] = '\ 0 '; printf ("% s \ r \ n", buffer); getchar (); close (cfd); return 0 ;}

Related Article

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.