[000] socket communication-a simple example of server and client implementation

Source: Internet
Author: User
Tags htons

I learned scanf and printf in C language. In fact, understanding socket is similar to the two input and output, but it is just information transmission.

1. Default function call sequence of the TCP Server:

According to the above call order, we can write the simplest "Hello World" server program.

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 # include <unistd. h> 5 # include <ARPA/inet. h> 6 # include <sys/socket. h> 7 8 void error_handling (char * message); 9 10 void error_handling (char * message) {11 fputs (message, stderr); 12 fputc ('\ n ', stderr); 13 exit (1); 14} 15 16 int main (INT argc, char ** argv) 17 {18 int serv_sock; 19 int clnt_sock; 20 21 // set a server address serv_addr and a client address clnt_addr22 Struct sockaddr_in serv_addr; 23 struct sockaddr_in records; 24 socklen_t records = sizeof (bytes); 25 26 memset (& serv_addr, 0, sizeof (serv_addr); 27 memset (& serv_addr, 0, sizeof (serv_addr); 28 char message [] = "Hello World"; 29 30 if (argc! = 2) {31 printf ("Usage: % S <port> \ n", argv [0]); 32 exit (1 ); 33} 34 35 // create a stream protocol for the Internet (TCP) socket36 serv_sock = socket (pf_inet, sock_stream, 0); 37 If (serv_sock =-1) {38 error_handling ("socket () error"); 39} 40 41 serv_addr.sin_family = af_inet; 42 serv_addr.sin_addr.s_addr = htonl (inaddr_any ); 43 serv_addr.sin_port = htons (atoi (argv [1]); 44 45 // associate the socket with the socket address structure 46 If (BIND (serv_sock, (struct sockaddr *) & serv_addr, sizeof (serv_addr) =-1) {47 error_handling ("BIND () error "); 48} 49 50 // server_socket is used to listen to 51 if (Listen (serv_sock, 5) =-1) {52 error_handling ("lisen () error "); 53} 54 55 // accept connection request 56 clnt_sock = accept (serv_sock, (struct sockaddr *) & clnt_addr, & clnt_addr_size); 57 if (clnt_sock =-1) {58 error_handling ("accept () error"); 59} 60 61 write (clnt_sock, message, sizeof (Message); 62 close (clnt_sock); 63 close (serv_sock ); 64 65 return 0; 66}

2. The client corresponds to the server. The call sequence is as follows:

The corresponding "Hello World" client program is:

1 hello_client.c 2 ------ 3 # include <stdio. h> 4 # include <stdlib. h> 5 # include <string. h> 6 # include <unistd. h> 7 # include <ARPA/inet. h> 8 # include <sys/socket. h> 9 10 void error_handling (char * message); 11 12 Void error_handling (char * message) {13 fputs (message, stderr); 14 fputc ('\ n ', stderr); 15 exit (1); 16} 17 18 int main (INT argc, char ** argv) {19 int sock; 20 struct sockaddr_in serv_addr; 21 char mescript E [30]; 22 int str_len; 23 memset (& serv_addr, 0, sizeof (serv_addr); 24 25 if (argc! = 3) 26 {27 printf ("Usage:/% S <ip> <port> \ n", argv [0]); 28 exit (1 ); 29} 30 31 // create a stream protocol for the Internet (TCP) socket32 sock = socket (pf_inet, sock_stream, 0); 33 If (sock =-1) {34 error_handling ("socket () error"); 35} 36 37 // set a socket address structure client_addr, representing the client Internet address, port 38 serv_addr.sin_family = af_inet; 39 serv_addr.sin_addr.s_addr = inet_addr (argv [1]); 40 serv_addr.sin_port = htons (atoi (argv [2]); 41 42 // link the socket and socket address structures 43 If (connect (sock, (struct sockaddr *) & serv_addr, sizeof (serv_addr) =-1) {44 error_handling ("Connect () error"); 45} 46 47 str_len = read (sock, message, sizeof (Message)-1 ); 48 if (str_len =-1) {49 error_handling ("Read () error"); 50} 51 52 printf ("Message from server: % s \ n ", message); 53 close (sock); 54 return 0; 55}

Start running the server:

# GCC hello_server.c-O hserver #./hserver 9010 ...... start to suspend

Start running the client:

#gcc hello_client.c -o hclient #./hclient 127.0.0.1 9010Message from server: Hello world

 

[000] socket communication-a simple example of server and client implementation

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.