Linux program design-socket options (Chapter 1), linux Program Design

Source: Internet
Author: User

Linux program design-socket options (Chapter 1), linux Program Design
Now we can improve the customer program so that it can be connected to any host with a name. This time, instead of connecting to the sample server, it is connected to a standard service, so that we can demonstrate the extraction operation of the port number.Now.
Most UNIX and some linux systems have a standard service named daytime, which provides the date and time of the system. the customer can connect to this service to view the current date and time of the server. the following is the customer program getdate. c
Compile the program getdate. c

/*************************************** * *********************************> File Name: getdate. c> Description: getdate. c> Author: Liubingbing> Created Time: Friday, January 1, July 24, 2015> Other: getdate. c *************************************** * ********************************/# include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <sys/socket. h> # include <netinet/in. h> # include <netdb. h> in T 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, an Error */hostinfo = gethostbyname (host); if (! Hostinfo) {fprintf (stderr, "no host: % s \ n", host); exit (1 );} /* check whether the daytime service exists on the host */servinfo = getservbyname ("daytime", "tcp"); if (! Servinfo) {fprintf (stderr, "no daytime service \ n"); exit (1);} printf ("daytime port is % d \ n ", ntohs (servinfo-> s_port);/* Create a socket */sockfd = socket (AF_INET, SOCK_STREAM, 0);/* construct the address to be used for the connect call */address. sin_family = AF_INET; address. sin_port = servinfo-> s_port; address. sin_addr = * (struct in_addr *) * hostinfo-> h_addr_list; len = sizeof (address);/* then establish a connection and obtain relevant information */result = connect (sockfd, (struct sockaddr *) & address, len); if (result =-1) {perror ("oops: getdate"); exit (1 );} result = read (sockfd, buffer, sizeof (buffer); buffer [result] = '\ 0'; printf ("read % d bytes: % s", result, buffer); close (sockfd); exit (0 );}
You can use getdate to obtain the date and time of any known host.

If the preceding information is displayed, it may be because the daytime service is not enabled on the connected computer.
When the program parses and runs this program, you can specify the host to be connected. the Port Number of the daytime service is determined by the network database function getservbyname. This function returns information related to the network service in a way similar to the host information returned. the getdate program tries to connect to the first address in the address list returned by the specified host. If it succeeds, it reads the information returned by the daytime service-a service that represents the UNIX date and time.
15.3.2 socket options can be used to control the behavior of socket connections. The number of these options is large. The setsockopt function is used to control these options. Its definition is as follows:
#include <sys/socket.h>int setsockopt(int socket, int level, int option_name, const void *option_value, size_t option_len);
You can set options at different levels of the protocol level. To set options at the socket level, you must set the level parameter to SOL_SOCKET.
The option_name parameter specifies the option to be set. The length of the option_calue parameter is option_len bytes. It is used to set the new value of the option. It is passed to the processing function of the underlying protocol and cannot be modified.
The socket-level options defined in the header file sys/socket. h are as follows:
Option description
SO_DEBUG open debugging information
SO_KEEPALIVE maintains the connection by defining the transmission persistence message.
SO_LINGER completes the transmission before the return of the close call.
Setsockopt returns 0 upon success, and-1 upon failure.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.