Linux Programming-Socket options (15th)

Source: Internet
Author: User
Tags keep alive

Now you can improve the client program so that it can connect to any host that has a name, this time not connecting to the sample server, but connecting to a standard service, so that you 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. Customers can connect to this service to view the current date and time of the server. Here is the client program to complete this job getdate.c
Writing a program 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, if not 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);}
You can use getdate to get the date and time of any known host.

If 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 you run 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. The program getdate attempts to connect to the first address in the address list returned by the specified host and, if successful, reads the information returned by the daytime service-a service that represents the Unix date and time.
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, which are defined as follows:
#include <sys/socket.h>int setsockopt (int socket, int level, int option_name, const void *option_value, size_t optio N_len);
Options can be set at different levels of the protocol hierarchy. If you want to set options at the socket level, you must set the levels 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 modified.
The socket level options that are defined in header file Sys/socket.h are as follows:
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 the close call returns
SetSockOpt returns 0 on success and 1 on failure.

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

Linux Programming-Socket options (15th)

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.