Simple Time Service Program

Source: Internet
Author: User

This seriesArticleI am mainly studying some learning notes on "UNIX Network Programming (Volume 1: Socket network API.

Let's take a look at this simple client that gets time from the server.Program:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <error. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> int main (INT argc, char ** argv) {int sock_fd; struct sockaddr_in sock_addr; // check whether the input parameter is correct if (argc <2) {printf ("Usage: time_client <IPaddress>"); Return-1;} // create socket sock_fd = socket (af_inet, sock_stream, 0); If (sock_fd <0) {perror ("socket:"); Return-1 ;}// set the IP address and port number of the server to be connected (bzero (& sock_addr, sizeof (sock_addr); sock_addr.sin_family = af_inet; sock_addr.sin_port = htons (3000); inet_ton (af_inet, argv [1], & sock_addr.sin_addr); // connect the server if (connect (sock_fd, (struct sockaddr *) & sock_addr, sizeof (sock_addr) <0) {perror ("Connect:"); Return-1 ;}// obtain information from the server char buff [1024]; int read_count; while (read_count = read (sock_fd, buff, sizeof (buff)> 0) {buff [read_count] = '\ 0'; printf ("read string: % s \ n ");} return 0 ;}

1. Use the socket function to create an Internet (af_inet) byte stream (sock_stream) socket. This function returns a file descriptor, which will be used in the subsequent steps.

2. Connect to the server. First, let the program know the server address and port number, which requires a struct
The network socket address structure of sockaddr_in, because a few socket functions require that the last eight bytes of the internet socket address structure be set to zero (which functions can be used later ), so we need to use bzero
Set sockaddr_in to zero. Here, the IP address and port number cannot be entered in the structure according to the common form. For the port number, it must be converted from the host order to the network order. htons (host
To net
Short) for this function, the IP address needs to convert it from our common writing method to machine-aware Writing Method and convert it to the network order. Here we call inet_pton to implement this function, this function supports IPv6. After setting these basic information, you can use the connect function to connect to the server.

3. Read the time information. Because Linux abstracts all devices into files, you can read the socket information just like reading local files.

Then let's look at the server-side program:

The main difference between this program and the client program is as follows:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <time. h> # include <unistd. h> # include <error. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> int main (INT argc, char ** argv) {// create a socket int server_fd; server_fd = socket (af_inet, sock_stream, 0); If (server_fd <0) {perror ("socket:"); Return-1 ;}// set the Socket Structure struct sockaddr_in sock_addr; bzero (& sock_addr, sizeof (sock_addr); sock_addr.sin_family = af_inet; sock_addr.sin_port = htons (3000); listener = htonl (inaddr_any); // bind socket If (BIND (server_fd, (struct sockaddr *) & sock_addr, sizeof (sock_addr ))) <0) {perror ("bind"); Return-2;} // converts a socket to a listener socket If (Listen (server_fd, 1024) <0) {perror ("listen"); Return-3;} // receives the connection request and sends the int connect_fd; char buff [1024]; time_t current_time; while (1) {connect_fd = accept (server_fd, (struct sockaddr *) null, null); current_time = Time (null); snprintf (buff, sizeof (buff ), "% s \ n", ctime (¤ T _ time); write (connect_fd, buff, strlen (buff); close (connect_fd);} return 0 ;}

1. Bind. This function is used to bind the server's external service port to the socket, and specify the IP address as inaddr_any. By setting this parameter, you can set whether the server receives client connections from all network interfaces or from a specified network interface.

2. Listen. This function converts a socket into a listening socket, so that external connections from customers can be accepted by the kernel on the socket. Its second parameter is used to specify the maximum number of client connections that the system kernel can queue on this listener descriptor.

3. Accept. After the server process calls this function, it goes to sleep state until the kernel receives a connection request from a client.

4. Write time information. After receiving the client connection, you will get a file descriptor for this connection. You can write information to this descriptor.

The above is a simple implementation of the client and server of the time service. If there is anything wrong with it, please correct it.

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.