Linux Programming--Data Report (15th chapter)

Source: Internet
Author: User

15.5 Data Sheet In this chapter, we focus on how to write applications that maintain connectivity with customers. This is done using a connection-oriented TCP socket. but in some cases, it is not necessary to spend time in a program to establish and maintain a socket connection.
Earlier, the daytime service used in program getdate.c was a good example of creating a socket first, then establishing a connection, reading a response, reading a response, and finally closing the connection. In this process, a lot of operation steps are used just to get a date.
Daytime services can also be accessed using datagrams via UUDP. To access it, send a datagram to the service, and then get a datagram with a date and time in the response, which is a very simple process.
when a customer needs to send a short query request to the server and expects to receive a short response, the service provided by UDP is generally used. If the server is short enough to process a customer request, the server can provide the service in a way that handles a customer request. This allows the operating system to place requests entered by the client into the queue. This simplifies the writing of server programs.
Because UDP provides unreliable services, you may find that datagrams or responses are lost. If the data is important, you need to carefully write a UDP client to check for errors and retransmit them if necessary. In fact, UDP datagrams are very reliable in a local area network.
to access the services provided by UDP, you need to use sockets and close system calls as before, but you need to call SendTo and recvfrom with two datagram-specific systems instead of the read and write calls that were originally used on the socket.
The following is a modified version of GETDATE.C, which obtains data through the UDP datagram service.
Writing a program getdate2.c
/************************************************************************* > File name:getdate2.c > description:getdate2.c > Author:liubingbing > Created time:2015 July 26 Sunday 15:37 05 seconds > Other:ge tdate2.c ************************************************************************/#include <stdio.h># Include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h># Include <netdb.h>int main (int argc, char *argv[]) {char *host;int sockfd;int len, result;struct sockaddr_in address; struct hostent *hostinfo;struct servent *servinfo;char buffer[128];if (argc = = 1) host = "localhost"; elsehost = argv[1];/* Find the host address, if not found error */hostinfo = gethostbyname (host), if (!hostinfo) {fprintf (stderr, "No host:%s\n", host); exit (1);} /* Check for daytime service on host */servinfo = Getservbyname ("Daytime", "UDP"), if (!servinfo) {fprintf (stderr, "no daytime service\n "); exit (1);} printf ("Daytime port is%d\n", Ntohs (Servinfo->s_port));/* SOCket create a UDP socket */SOCKFD = socket (af_inet, SOCK_DGRAM, 0);/* Create address */address.sin_family = Af_inet;address.sin_port = Servin fo->s_port;address.sin_addr = * (struct in_addr *) *hostinfo->h_addr_list;len = sizeof (address);/* SENDTO (int  SOCKFD, void *buffer, size_t len, int flags, struct sockaddr *, socklen_t Tolen); * SendTo system call sends a datagram from the buffer buffer to the target server using the specified socket address */result = sendto (sockfd, buffer, 1, 0, struct sockaddr *) &address, L EN);/* Recvfrom (int sockfd, void *buffer, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); * The Recvfrom system calls on the socket to wait for a datagram coming from a specific address and puts it into buffer buffer */result = recvfrom (sockfd, buffer, sizeof (buffer), 0, (struct SOCKADDR *) &address, &len); Buffer[result] = ';p rintf ("read%d bytes:%s", result, buffer); close (SOCKFD); exit (0);}

Run the program, if the display: Connection refused, indicating that Linux does not have daytime service enabled, for Ubunu, it is generally necessary to install the following xinetd:

Then cd/etc/xinetd.d/
Then sudo vim daytime

you need to use Getservbyname to find the daytime service, but by requesting the UDP protocol to specify the datagram service, using a socket call with SOCK_DGRAM parameters to create a datagram socket, or building the destination address in the same way as before, But now you need to send a datagram instead of just reading the data from the socket.
Because a connection to a specified UDP service is not explicitly established, all must be done in some way until the server needs to receive a response. In this example, a datagram is sent to the server (where a byte of data is sent from the buffer that prepares to receive the response), which returns a response that contains the date and time.
The sendto system call sends a datagram from the buffer buffer to the target server using the specified socket address. Its prototype is as follows:
int sendto (int sockfd, Viod *buffer, size_t len, int flags, struct sockaddr *to, socklen_t Tolen);
In normal applications, the flags parameter is generally set to 0.
The recvfrom system call waits on the socket for a datagram coming from a specific address and puts it into the buffer buffer, and its prototype is as follows:
int recvfrom (int sockfd, void *buffer, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
Similarly, in normal applications, the flags parameter is generally set to 0.
Unless the socket is set to non-blocking using fcntl, the Recvfrom call will remain blocked and can be used in the same way as the front-facing connection server to determine if there is data reaching the socket using the select call and timeout settings. In addition, You can also use the alarm clock signal to interrupt a receive operation.
15.6 Summary In this chapter, another method of interprocess communication is described: Sockets. It enables the development of distributed client/server applications that actually run across the network. Some host database information functions are also briefly introduced. Several client/server sample programs have been developed to demonstrate network and multi-client processing methods.
Finally, a select system call is introduced that allows a program to wait for input and output activities to occur on multiple open file descriptors and sockets at the same time.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Programming--Data Report (15th chapter)

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.