1. Advanced socket functions getsockopt and setsockopt
Int getsockopt (INT sockfd, int level, int optname, void * optval, socklen_t * optlen)
Int setsockopt (INT sockfd, int level, int optname, const void * optval, socklen_t * optlen)
LevelSpecifies the hierarchy of control sockets. Three values can be obtained:
1) sol_socket: General socket options.
2) ipproto_ip: IP Option.
3) ipproto_tcp: TCP option.
CorrespondingOptnameDetailed description
Optname specifies the control method (option name ).
Option Name Description Data Type
========================================================== ====================================
Sol_socket
So_broadcast allows sending broadcast data int
So_debug allows int debugging
So_dontroute
So_error get socket error int
So_keepalive
So_linger latency
So_oobinline put out-of-band data into normal data stream int
So_rcvbuf receive buffer size int
So_sndbuf sending buffer size int
So_rcvlowat lower limit int of the receiving buffer
So_sndlowat lower limit int of the sending Buffer
So_rcvtimeo receiving timeout struct timeval
So_sndtimeo sending timeout struct timeval
So_reuseraddr allows reuse of the local address and port int
So_type: Obtain the socket type Int.
So_bsdcompat is compatible with the BSD system int
Ipproto_ip
Ip_hdrincl contains the IP header int in the data package.
Ip_optinos IP header option int
Ip_tos service type
Ip_ttl time int
Ippro_tcp
Tcp_maxseg maximum TCP Data Segment Size int
Tcp_nodelay does not use the Nagle algorithm int
OptvalObtain or set socket options. Convert the Data Type of the Option name, sometimes socket options. on or off.
2. Advanced socket function IOCTL
Int IOCTL (int fd, int req ,...)
IOCTL can control all file descriptors. Here we will introduce the options for controlling sockets.
IOCTL control options
Whether the siocatmark has reached the out-of-band mark int
Fioasync asynchronous input/output flag int
Number of bytes readable in the fionread buffer int
Use man ioctl_list to view detailed options.
3. Advanced socket function fcntl
Status = fcntl (hand, option, mode );
In the above fcntl function call, each parameter is defined as follows:
Handle: The opened file handle.
Option: It can be either of the following values:
F_getfl: indicates the file read status value.
F_setfl: Specifies the File status.
(Note: Both f_getfl and f_setfl are defined in fcntl. h)
Mode: If optipn is f_getfl, the value of this parameter can be any value. If f_setfl is used, the parameter value can be:
O_wronly: Set the file to write-only.
O_rdwr: sets the file to read/write status.
O_rdonly: sets the file to read-only.
Status: the function assigns the call result to status. If the operation fails, the value of status is set to-1.
Instance used:
Instance 1: setsockopt
// Make the address reusable
Int optavl3 = 1;
Int optlen3 = sizeof (INT );
Getsockopt (S, sol_socket, so_reuseaddr, & optavl3, & optlen3 );
Instance 2: fcntl
// Set socket non-blocking
Void setnonblocking (INT sock)
...{
Int opts;
Opts = fcntl (sock, f_getfl );
If (OPTs <0)
...{
Perror ("fcntl (sock, getfl )");
Exit (1 );
}
Opts = opts | o_nonblock;
If (fcntl (sock, f_setfl, opts) <0)
...{
Perror ("fcntl (sock, setfl, opts )");
Exit (1 );
}
}