GetSockOpt () setsockopt () ioctlsocket () fcntl () usage

Source: Internet
Author: User
Tags define function flock true true unsupported
getsockopt ()

Briefly:
Gets a set of interface options.

#include <winsock.h>

int PASCAL FAR getsockopt (SOCKET s, int level, int optname,
Char far* optval, int far* optlen);

S: A descriptive word that identifies the socket interface.
Level: The hierarchy of option definitions. The level of support is only Sol_socket and ipproto_tcp.
Optname: The set of interface options to get.
Optval: Pointer to a buffer that holds the value of the option you have obtained.
Optlen: Pointer to the length value of the optval buffer.

Comments:
The getsockopt () function is used to get the current value of the option for any type, any state set interface, and to deposit the result in optval. There are options at different protocol layers, but often at the highest "set interface" level, setting options affects the operation of the socket, such as the blocking of the operation, the way the packet is chosen, and the transmission of out-of-band data.
The value of the selected option is placed in the optval buffer. The number of Optlen pointed at initially contains the length of the buffer, which is set to the length of the actual value when the call returns. For the So_linger option, the equivalent size of the linger structure is the size of an integer for the other options.
If a setsockopt () call is not made, getsockopt () returns the system default value.
GetSockOpt () supports the following options. Where the "type" column indicates the value that optval points to. Only the Tcp_nodelay option uses the IPPROTO_TCP layer, and the remaining options use the Sol_socket layer.

Option type meaning
The So_acceptconn BOOL socket interface is being monitored with listen ().
The So_broadcast BOOL sleeve interface is set to transmit broadcast information.
So_debug BOOL allows debugging.
If So_dontliner BOOL is true, the So_linger option is prohibited.
So_dontroute BOOL is forbidden to choose the path.
So_error int gets the error status and clears it.
So_keepalive BOOL sends "keep activity" information.
So_linger struct Linger far* returns the current linger options.
So_oobinline BOOL receives Out-of-band data in the normal data stream.
SO_RCVBUF int receive buffer size.
The SO_REUSEADDR BOOL sleeve interface can be bundled with an address already in use.
SO_SNDBUF int send buffer size.
So_type int Sleeve interface type (such as Sock_stream).
Tcp_nodelay BOOL prohibits the sending of merged Nagle algorithms.

The BSD options not supported by GetSockOpt () are:

Option name Type meaning
So_rcvlowat int receives a low-level watermark.
So_rcvtimeo int receives timeout.
So_sndlowat int sends a low-level watermark.
So_sndtimeo int sends timeout.
Ip_options Gets the selected item in the IP header.
tcp_maxseg int Gets the length of the TCP maximum segment.
Calling GetSockOpt () with an unsupported option will return a wsaenoprotoopt error code (available WSAGetLastError ()).

return value:
If no error occurs, getsockopt () returns 0. Otherwise, the SOCKET_ERROR error is returned, and the application can obtain the appropriate error code by WSAGetLastError ().


Error code:
Wsanotinitialised: You should first successfully invoke WSAStartup () before using this API.
The Wsaenetdown:windows sleeve interface implementation detects the failure of the network sub system.
Wsaefault:optlen parameter is illegal.
Wsaeinprogress: A blocked Windows Socket interface call is running.
Wsaenoprotoopt: Unknown or unsupported option. Where the Sock_stream type's socket does not support the So_broadcast option, the SOCK_DGRAM-type socket interface does not support So_acceptconn, So_dontlinger, So_keepalive, So_linger, and so _oobinline option.
Wsaenotsock: The descriptor is not a nested interface.

See:
SetSockOpt (), WSAAsyncSelect (), socket ().

setsockopt ()

Briefly:
Sets the options for the socket interface.

#include <winsock.h>

int PASCAL FAR setsockopt (SOCKET s, int level, int optname,
const char far* optval, int optlen);

S: Identifies a descriptive word for a set of interfaces.
Level: The hierarchy of option definitions; currently supports only sol_socket and ipproto_tcp levels.
Optname: Options you want to set.
Optval: Pointer to the buffer that holds the option value.
The length of the optlen:optval buffer.

Comments:
The setsockopt () function is used for setting option values for any type, any state set interface. Although there are options on different protocol tiers, this function defines only the highest options on the "set interface" level. Options affect the operation of the socket interface, such as whether the expedited data is received in the normal data stream, whether the broadcast data can be sent from the socket, and so on.
Options for two sets of interfaces: One is a Boolean option that allows or disables one attribute, and the other is shaping or structural options. Allows a boolean option to point the optval to a number other than 0, and to prevent an option optval point to an integer equal to zero. For Boolean options, Optlen should equal sizeof (int), and for other options, optval points to the number of plastic or structure that contains the desired option, while Optlen is the number of plastic or the length of the structure. The So_linger option is used to control actions that have queued data to be sent on the socket and that the closesocket () call has been executed. See the effect of the So_linger option on closesocket () semantics in the closesocket () function. The application sets the appropriate operational attributes by creating a linger structure:
struct Linger {
int L_onoff;
int L_linger;
};
To allow So_linger, the application should set the L_onoff to Non-zero, set L_linger to zero or the required time-out value in seconds, and then call SetSockOpt (). To allow So_dontlinger (that is, so_linger), L_onoff should be set to zero, and then call SetSockOpt ().
By default, a set of interfaces cannot be bundled with a local address that is already in use (see Bind ()). However, there are times when you need to "reuse" addresses. Since each connection is uniquely identified by a combination of local and remote addresses, two sets of interfaces with an address bundle are not a hindrance as long as the remote address is different. To inform the Windows Sleeve interface implementation do not bind an address to another set of interfaces because it is already being used by a nested interface, the application can set the SO_REUSEADDR option before bind () calls. Note that this option is interpreted only when the bind () call is made, so it is not necessary (but harmless) to set this option on a socket that does not share an address, or if bind () has no effect on this or other socket interfaces.
An application can open the So_keepalive option to enable the Windows Socket interface implementation to allow the "keep active" package to be used in the case of a TCP connection. A Windows Sleeve interface implementation is not required to support "keep active", but if supported, the specific semantics will be relevant to the implementation and should comply with the specification of section 4.2.3.6 in the RFC1122 "Internet host requirements-communication layer". If the connection is invalidated due to "keep active", any calls to the set of interfaces in progress will be returned as Wsaenetreset errors, and any subsequent calls will be returned with a wsaenotconn error.
The Tcp_nodelay option prohibits the Nagle algorithm. The Nagle algorithm reduces the number of bits and pieces of small packets sent by the host by storing the unacknowledged data in a buffer until a packet is sent together. But for some applications, this algorithm will reduce system performance. So tcp_nodelay can be used to turn this algorithm off. Application writers set the Tcp_nodelay option only if they know exactly what it's doing and need it, because the setting has a significant negative impact on network performance. Tcp_nodelay is the only option that uses the IPPROTO_TCP layer, and all other options use the Sol_socket layer.
If the So_debug option is set, the Windows socket vendor is encouraged, but not required, to provide the appropriate debugging information for the output. However, the mechanism for generating debugging information and the form of debugging information are beyond the scope of this specification.
SetSockOpt () supports the following options. Where "type" indicates the type of data that optval refers to.
Option type meaning
So_broadcast BOOL allows the socket interface to transmit broadcast information.
So_debug BOOL Records debugging information.
So_dontliner BOOL do not block the shutdown operation because the data is not sent. Setting this option is equivalent to placing the So_linger l_onoff element to zero.
So_dontroute BOOL is forbidden to choose the path; direct transmission.
So_keepalive BOOL sends the "Keep active" package.
So_linger struct Linger far* If there is no data sent when it is closed, stay.
So_oobinline BOOL receives Out-of-band data in a regular data stream.
SO_RCVBUF int determines buffer size for receive. {The size of the final buffer is also limited by TCP/IP-related parameter configuration for Linux systems}
SO_REUSEADDR BOOL allows a nested interface and an address bundle that is already in use (see Bind ()).
SO_SNDBUF int Specifies the size of the send buffer. {The size of the final buffer is also limited by TCP/IP-related parameter configuration for Linux systems}
Tcp_nodelay BOOL prohibits the sending of merged Nagle algorithms.

The BSD options not supported by setsockopt () are:
Option name Type meaning
So_acceptconn BOOL Sleeve interface in monitoring.
So_error int gets the error status and clears it.
So_rcvlowat int receives a low-level watermark.
So_rcvtimeo int receives timeout.
So_sndlowat int sends a low-level watermark.
So_sndtimeo int sends timeout.
So_type int Sleeve interface type.
Ip_options sets options in the IP header.

return value:
If no error occurs, setsockopt () returns 0. Otherwise, the SOCKET_ERROR error is returned, and the application can obtain the appropriate error code by WSAGetLastError ().

Error code:
Wsanotinitialised: You should first successfully invoke WSAStartup () before using this API.
The Wsaenetdown:windows sleeve interface implementation detects the failure of the network sub system.
Wsaefault:optval is not a valid part of the process address space.
Wsaeinprogress: A blocked Windows Socket interface call is running.
Wsaeinval:level value is illegal, or information in optval is illegal.
Wsaenetreset: The connection timed out after the so_keepalive setting.
Wsaenoprotoopt: Unknown or unsupported option. Where the Sock_stream type's socket does not support the So_broadcast option, the Sock_dgram type's sleeve interface does not support So_dontlinger, so_keepalive, So_linger, and so_oobinline options.
Wsaenotconn: The connection is reset when the so_keepalive is set.
Wsaenotsock: The descriptor is not a nested interface.

See:
Bind (), getsockopt (), ioctlsocket (), socket (), WSAAsyncSelect ().

ioctlsocket ()

Briefly:
The mode of the control socket interface.

#include <winsock.h>

int PASCAL FAR ioctlsocket (SOCKET s, long cmd, u_long far* ARGP);

S: A descriptive word that identifies the socket interface.
CMD: The operation command on the socket s.
ARGP: A pointer to the parameters that are with the cmd command.

Comments:
This function can be used for any set of interfaces in any state. It is used to obtain the operation parameters related to the socket interface, and is independent of the specific protocol or communication subsystem. The following commands are supported:
Fionbio: The non-blocking mode of the set interface S is allowed or disallowed. ARGP points to an unsigned long integer. If non-blocking mode is allowed Non-zero, such as blocking non-blocking mode is zero. When you create a nested interface, it is in blocking mode (that is, non-blocking mode is blocked). This is consistent with the BSD socket interface. The Wsaasynselect () function automatically sets the socket interface to non-blocking mode. If a wsaasynselect () operation has been performed on a set of interfaces, any attempt to reset the set interface to blocking mode with ioctlsocket () will fail wsaeinval. In order to reset the socket to blocking mode, the application must first be wsaasynselect () called (ievent parameter 0) to Wsaasynselect ().
Fionread: Determines the amount of data that the sleeve interface s automatically reads. ARGP points to an unsigned long integer that contains the return value of ioctlsocket (). If S is the Socket_stream type, Fionread returns the amount of all the data received in one recv (). This is usually the same amount of data queued in the socket. If S is a sock_dgram type, the Fionread returns the first datagram size queued on the set interface.
Siocatmark: Is it true that all out-of-band data has been read in. This command applies only to the Sock_stream type of socket, and the set of interfaces has been set to receive Out-of-band data (so_oobinline) online. If no out-of-band data is waiting to be read, the operation returns True true. Otherwise, false false is returned, and the next recv () or recvfrom () operation retrieves some or all of the data before "tag". The application can use the Siocatmark action to determine if there is data left. If there is regular data before the "emergency" (out-of-band) data, the data is received sequentially (note that the recv () and recvfrom () operations do not confuse regular and out-of-band data in a single call. ARGP points to a bool, where ioctlsocket () is stored in the return value.

Compatibility:
This function is a subset of the Berkeley set interface function IOCTL (). There is no command equivalent to Fioasync, Siocatmark is the only command supported by the nested interface hierarchy.

return value:
After success, ioctlsocket () returns 0. Otherwise, the SOCKET_ERROR error is returned, and the application can obtain the appropriate error code by WSAGetLastError ().


Error code:
Wsanotinitialised: You should first successfully invoke WSAStartup () before using this API.
The Wsaenetdown:windows sleeve interface implementation detects the failure of the network sub system.
Wsaeinval:cmd is an illegal command, or the ARGP argument does not apply to the cmd command, or the command
Does not apply to this type of socket interface.
Wsaeinprogress: A blocked Windows Socket interface call is running.
Wsaenotsock: The descriptor is not a nested interface.

See:
Socket (), setsockopt (), getsockopt (), WSAAsyncSelect ().
FcntlTable header file #include <fcntl.h>
#include <fcntl.h>
define function int fcntl (int fd, int cmd);
int fcntl (int fd,int cmd,long arg);
int fcntl (int fd,int cmd,struct flock * lock);
Fcntl () is used to manipulate some of the features of a file descriptor. Parameter FD represents the file descriptor to be set, and the parameter cmd represents the instruction to be manipulated.
There are several situations:
F_DUPFD is used to find the smallest and still unused file descriptor that is greater than or equal to the parameter arg, and to copy the file descriptor for the parameter FD. Successful execution returns the newly copied file descriptor. Please refer to dup2 (). F_GETFD obtain CLOSE-ON-EXEC flag. If the flag has a fd_cloexec bit of 0, the file will not close when the exec ()-related function is invoked.
F_SETFD set close-on-exec flag. The flag is determined by the fd_cloexec bit of the parameter arg.
F_GETFL obtains the file description Word status flag, which is marked as the parameter of the open () flags.
F_SETFL Sets the file description state flag, parameter ARG is the new flag, but only the O_append, O_nonblock and o_async bits are allowed to change, and other bit changes will not be affected.
F_getlk gets the state of the file lock.
F_SETLK sets the state of the file lock. At this point the L_type value of the FLCOK structure must be f_rdlck, F_wrlck, or F_unlck. If the lock cannot be established, return-1, the error code is eacces or Eagain.
The f_setlkw f_setlk function is the same, but when a lock cannot be established, this call waits until the lock action succeeds. If the signal is interrupted while waiting for the lock, it returns 1 immediately, with the error code EINTR. The parameter lock pointer is a flock structure pointer, defined as
struct FLCOK
{
short int l_type;
short int l_whence;
off_t L_start;
off_t L_len;
pid_t L_pid;
};
There are three states of L_type:
F_rdlck to establish a lock for reading
F_wrlck to create a lock for writing
F_unlck the lock that was established prior to deleting
There are also three ways of L_whence:
Seek_set starts at the beginning of the file as a lock.
Seek_cur the location of the current file read/write location as the starting position of the lock
Seek_end at the end of the file as the starting position of the lock.
The return value succeeds with a return of 0, and if there is an error, return-1, and the reason for the error is in errno.

Set/Read socket SENDBUF len:

       #include <sys/types.h>
       # Include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
int main ()
{

        int s = 0;
                int optval;
                         socklen_t optlen = sizeof (int);
                                 if ((s = socket (af_inet,sock_ stream,0) <0) perror ("socket");
                getsockopt (S, SOL _socket, So_sndbuf, &optval, &optlen);
                printf ("Optlen =% d/n, optval =%d/n ", Optlen, optval);

int len=164*1024;
SetSockOpt (S,sol_socket,so_sndbuf, (char *) &len,sizeof (len));


GetSockOpt (S, Sol_socket, So_sndbuf, &optval, &optlen);
printf ("Optlen =%d/n, optval =%d/n", Optlen, optval);
Close (s);
return 0;
}

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.