Linux Programming-Socket options (15th)

Source: Internet
Author: User
Tags keep alive

now it is possible to improve the client program so that it can connect to any host with no name, this time instead of connecting to the Demo sample server, but connecting to a standard service so that it can demonstrate the port number extraction operationThe.
Most UNIX and some Linux systems have a standard service daytime, which provides the date and time of the system. The customer is able to connect to this service to view the current date and time of the server. The following is the client program that finished the job getdate.c
Knock Code GETDATE.C
/************************************************************************* > File name:getdate.c > description:getdate.c > Author:liubingbing > Created time:2015 July 24 Friday 20:26 04 seconds > Other:get date.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 Hoste NT *hostinfo;struct servent *servinfo;char buffer[128];if (argc = 1) host = "localhost"; elsehost = argv[1];/* Find the address of the host, assuming no To, report an error */hostinfo = gethostbyname (host), if (!hostinfo) {fprintf (stderr, "No host:%s\n", host); exit (1);} /* Check the host for daytime service */servinfo = Getservbyname ("Daytime", "TCP"), if (!servinfo) {fprintf (stderr, "no daytime service\n" ); exit (1);} printf ("Daytime port is%d\n", Ntohs (Servinfo->s_port));/* CreateBuild a socket */SOCKFD = socket (af_inet, sock_stream, 0);/* construct connect call to use address */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 the connection and obtain the information */result = Connect (sockfd, (struct sockaddr *) &address, Len); if (result = =-1) {perror ("oops:getdate"); Exit (1);} result = Read (sockfd, buffer, sizeof), buffer[result] = ';p rintf ("read%d bytes:%s", result, buffer); Close (Soc KFD); exit (0);}
The ability to use GETDATE to get the date and time of any known host.

Assuming you see the above information, this may be because the computer you are connecting to does not have the daytime service enabled.
Program resolution when executing this program, you can specify the host to which you want to connect. 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 returning host information. Program GETDATE attempts to connect to the first address in the address list returned by the specified host, assuming it succeeds, it reads the information returned by the daytime service-a service that represents the date and time of the UNIX.
The 15.3.2 socket option allows you to control the behavior of socket connections with a number of options, where the SetSockOpt function controls these options, and its definition is as follows:
#include <sys/socket.h>int setsockopt (int socket, int level, int option_name, const void *option_value, size_t optio N_len);
The option can be set at different levels of the protocol hierarchy. If you want to set options at the socket level, you must set the degree parameter to Sol_socket.
The option_name parameter specifies the option to set, the length of the option_calue parameter is option_len bytes, which is used to set the new value of the option, which is passed to the handler of the underlying protocol and cannot be altered.
The socket level options that are defined in the header file sys/socket.h, such as the following:
Option description
So_debug Opening Debug information
So_keepalive maintains the connection by defining the transmission to keep alive messages
So_linger completion of transfer work before close call is returned
SetSockOpt returns 0 on success and 1 on failure.

Linux Programming-Socket options (15th)

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.