"Linux Advanced Programming" (14th) TCP Advanced Applications 3

Source: Internet
Author: User

Control socket file Descriptor Properties 1.set/getsockopt () modify socket properties

int getsockopt (int __fd, int __level, int __optname, void *__ Restrict __optval, socklen_t *__restrict __optlen): Gets the properties of a socket. Success 0, Failure-1

int setsockopt (int __fd, int __level, int __optname, __ const void *__optval, socklen_t __optlen) : Set a Socket property

Parameter 1: Socket descriptor.

Parameter 2: Specify the classification of socket attributes, identifying a protocol level

#define Sol_socket   1    // Universal socket Option #define ipproto_ip   0    //IP option #define Ipproto_tcp  6    //TCP option

Parameter 3: Specify the parameters of the control, with different options in the case of parameter 2

The options for Sol_socket are as follows:

The IPPROTO_IP options are as follows:

The IPPROTO_TCP options are as follows:

Parameter 4: Socket option value, converted according to the data type of the option name

Parameter 5: Buffer size, returns the length of the value that is reversed.

Example: Listing basic information for a socket object

#include <stdio.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<sys/types.h>#include<netinet/inch.h>#include<netinet/tcp.h>#include<sys/socket.h>#include<sys/wait.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/time.h>intMainintargcChar*argv[]) {    intrcvbuf_size; intsndbuf_size; intType =0;    socklen_t size; intsock_fd; structtimeval Set_time, Ret_time; if(SOCK_FD = socket (af_inet, Sock_stream,0)) == -1) {perror ("Socket");    Exit (Exit_failure); }    //get send buffer sizeSize =sizeof(sndbuf_size); GetSockOpt (SOCK_FD, Sol_socket, So_sndbuf,&sndbuf_size, &size); printf ("sndbuf_size =%d\n", sndbuf_size); //get receive buffer sizeSize =sizeof(rcvbuf_size); GetSockOpt (SOCK_FD, Sol_socket, So_rcvbuf,&rcvbuf_size, &size); printf ("rcvbuf_size =%d\n", rcvbuf_size); //Get socket TypeSize =sizeof(type); GetSockOpt (SOCK_FD, Sol_socket, So_type,&type, &size); printf ("type =%d\n", type); //Get Send timeout valueSize =sizeof(structtimeval); GetSockOpt (SOCK_FD, Sol_socket, So_sndtimeo,&ret_time, &size); printf ("Default:time Out is%ds and%dus\n", Ret_time.tv_sec, ret_time.tv_usec); //To Modify the timeout valueSet_time.tv_sec =Ten; Set_time.tv_usec= -; SetSockOpt (SOCK_FD, Sol_socket, So_sndtimeo,&set_time, size); GetSockOpt (SOCK_FD, Sol_socket, So_sndtimeo,&ret_time, &size); printf ("After modify:time off is %ds and%dus\n", Ret_time.tv_sec, ret_time.tv_usec); //read TTL value    intTTL =0; Size=sizeof(TTL); GetSockOpt (SOCK_FD, Ipproto_ip, Ip_ttl,&ttl, &size); printf ("The default IP ttl is%d\n", TTL); //Read TCP_MAXSEG value    intMaxseg =0; Size=sizeof(MAXSEG); GetSockOpt (SOCK_FD, Ipproto_tcp, Tcp_maxseg,&AMP;MAXSEG, &size); printf ("The TCP Max seg is%d\n", maxseg);}

Effect

2.FCNTL Control Socket

1. Control socket is non-blocking mode

int flags; if 0 0 ) {    perror ("fcntl");    | = O_nonblock; if 0 ) {    perror ("fcntl");    Exit (exit_failure);}

2. Set the socket to signal-driven. Sigio signal generated when the socket status changes

int flags; if 0 0 ) {    perror ("fcntl");    | = O_async; if 0 ) {    perror ("fcntl");    Exit (exit_failure);}

3. Use the F_setown option to set the owner of the socket and receive Sigio and Sigurg signals.

FCNTL (socket, F_setown, getpid ());

4. Get a socket owner by using the F_getown option

FCNTL (socket, F_getown, getpid ());

3.ioctl Control File Descriptor

You can perform special processing on the socket file descriptor. Need #include<stropts.h>

int IOCTL (int fildes, int request, ... /*arg*/)

Common options:

File related actions:

Socket related Operations

Network Interface Configuration Control

ARP Cache operation

RARP Cache Control

The third parameter has a specialized data structure for the struct ifreq to provide specific operations

"Linux Advanced Programming" (14th) TCP Advanced Applications 3

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.