The user interface for the Linux socket. This BSD-compatible socket is a unified interface between the user process and the Kernel network protocol stack, each protocol module belonging to a different protocol family, such as Pf_inet, Pf_ipx, Pf_packet, and socket types such as Byte stream (SOCK_STREAM) or datagram (SOCK_DG RAM). Refer to Socket (2) for protocol family and socket types. NAME
Socket-linux sockets
General overview
#include <sys/socket.h>
Mysocket = socket (int socket_family, int socket_type, int protocol) ;
Describe
This manual page describes the user interface for Linux sockets. This BSD-compatible socket is a unified interface between the user process and the Kernel network protocol stack, and each protocol module belongs to a different protocol family , such as pf_inet, pf_ipx, pf_packet , and c4> socket type , such as byte Stream (sock_stream) or datagram (SOCK_DGRAM). for protocol families and socket types, refer to socket(2).
Socket Layer function
Users send and receive packets through these socket functions, as well as other socket operations. See their respective hand albums for detailed instructions.
Socket (2) Create sockets,
Connect (2) Establishing a connection to a remote socket address
Bind (2) Bind the socket with a local socket address (assigning a local protocol address to the socket)
Listen (2) Notify socket to accept new connection
Accept (2) Get new descriptive word for new completed connection
Socketpair (2) Returns an anonymous socket for two connections (implemented only in some local families, such as Pf_unix)
Send (2),
SendTo (2), and sendmsg(2) Send data via sockets, while recv(2), recvfrom(2), recvmsg(2) receive data from the socket. Poll (2) and
Select (2) Wait for data to arrive or be ready to receive data. In addition, standard I/O operations such as write(2), Writev(2), sendfile(2), read(2), and Readv(2) It can also be used to read in (receive) and write out (send) data.
getsockname (2) to obtain a local socket address
Getpeername (2) to obtain a remote socket address. getsockopt (2) and setsockopt(2) are used to set or get socket or protocol options. IOCTL (2) can also be used to set or read some other options.
Close (2) Close the socket. shutdown (2) Turn off part of a full-duplex socket connection.
The socket does not support searching, nor does it support calling Pread(2) or pwrite(2) for operations other than 0 positions. Can be used fcntl(2). Set the O_nonblock flag to implement a non-blocking I/O operation on the socket O_nonblock is inherited from accept, and all the previously blocked operations return Eagain. Connect (2) The einprogress error is returned in this case. Users can wait for various events via poll(2) or select(2).
I/O events
|
Event |
Polling flag |
Event occurred
|
Read |
Pollin |
New data arrives.
|
Read |
Pollin |
Establish a successful connection (for connection-oriented sockets)
|
Read |
Pollhup |
The other end of the socket makes a disconnect request.
|
Read |
Pollhup |
(Connection-oriented protocol only) socket is disconnected when writing. Send Sigpipe at the same time .
|
Write |
Pollout |
The socket has sufficient send buffers to write new data.
|
Read/write |
pollin| Pollout |
End of Connect(2) issued.
|
Read/write |
Pollerr |
Generates an asynchronous error.
|
Read/write |
Pollhup |
The other side has closed the connection one way.
|
Exception |
Pollpri |
Emergency data arrives. Then send Sigurg.
|
Another Poll/select method is to let the kernel use SIGIO signals to notify the application. You have to use fcntl(2) to set the fasync flag of the socket file descriptor and use the sigaction(2). Set a valid signal processing handle to the SIGIO signal. See the signals discussion below.
Socket options
The socket option can be set with setsockopt(2) and getsockopt(2) reads the socket options for all sockets with the socket level set to Sol_socket :
-
-
So_keepalive
-
-
allows the ability to send keep-alive messages on a connection-oriented socket. is a Boolean integer.
-
-
So_oobinline
-
-
If this option is turned on, out-of-band (Out-of-band) data can be placed directly into the receiving data stream. Otherwise, only the
Msg_oob flag is opened on receive to receive out-of-band data.
-
-
So_rcvlowat and
So_sndlowat
-
-
declares the user (
So_rcvlowat) that is starting to protocol (
So_sndlowat) or is receiving data. The minimum number of bytes in the buffer before data is passed. In Linux these two values are immutable, fixed to 1 Bytes. You can use
getsockopt to read their values;
setsockopt always returns
enoprotoopt.
-
-
So_rcvtimeo and
So_sndtimeo
-
-
the time-out setting is sent and received, and the time-out times are wrong. Specified by the protocol in Linux, cannot be read and written. Their functions can be simulated by
alarm(2) or
Setitimer(2).
-
-
So_bsdcompat
-
-
allows BSD bug-to-bug compatibility. This item can only be used in the UDP protocol module and will be canceled in the future. If allowed, ICMP errors received by the UDP socket will not be transmitted to the user program. The BSD bug-to-bug compatibility is also allowed in Linux 2.0 for the original sockets (the header is randomly changed, the broadcast identity is omitted), but this is canceled in Linux 2.2. The way to modify the user program is better.
-
-
So_passcred
-
-
allow or turn off the receipt of
scm_credentials control messages. See
Unix(7) For more information.
-
-
So_peercred
-
-
returns the authentication of the external process that is connected to this socket. Only useful in
Pf_unix sockets. See
UNIX(7). The parameter is
ucred structure. Only in
getsockopt . are valid.
-
-
So_bindtodevice
-
-
bind this socket to a specific device, such as LQETH0RQ, as the specified interface name. If the name is an empty string or if the item is 0 long, the socket device binding is canceled. The past option is a string with a variable length of NULL 0 ending interface name with a maximum length of
IFN Amsiz. If a socket is bound to an interface, only packets received by this particular interface can be processed by this socket.
-
-
So_debug
-
-
allows socket debugging. Valid only for processes that have
cap_net_admin functionality or that have a valid user ID of 0.
-
-
So_reuseaddr
-
-
indicates that the acknowledgment rule used in a
bind(2) call to the address provided to it should allow the local address to be reused. For
pf_inet sockets, this means that the socket can be bound unless an active listening socket interface is already in use Bind to this address. If this listening socket and a specified port are bound to
inaddr_any , it can no longer bind to this port on any local address.
-
-
So_type
-
-
returning a socket type, such as
Sock_stream, by an integer can only be read by
getsockopt .
-
-
So_dontroute
-
-
It is not sent through the gateway and can only be sent to a directly connected host. The same effect can be achieved by setting the
msg_dontroute flag on the
send(2) operation of the socket. Its value is the identity of a Boolean integer.
-
-
So_broadcast
-
-
Sets or gets the broadcast identity. When this option is selected, the datagram socket receives the packets sent to the broadcast address and can send packets to the broadcast address. This option is not valid for stream-oriented sockets.
-
-
So_sndbuf
-
-
Sets or gets the maximum number of bytes for the socket send buffer. Its default value is set by
Wmem_default sysctl, and the maximum allowable value is set by
Wmem_max sysctl.
-
-
So_rcvbuf
-
-
Sets or gets the maximum number of bytes for the socket receive buffer. Its default value is set by
Rmem_default sysctl, and the maximum allowable value is set by
Rmem_max sysctl.
-
-
So_linger
-
-
Sets or gets the value of the
so_linger option. The parameter is the
LINGER structure.
-
struct Linger { int l_onoff; /* Delay status (ON/off) */ int l_linger; /* Delay How long * *};
-
If you select this option,
close(2) or
shutdown(2) will not return until the queued messages in all sockets are successfully sent or the delay time is reached. Otherwise, the call returns immediately. The closing operation will be performed in the background. If the socket is a part of
exit(2), it is always delayed in the background when it is closed.
-
So_priority
-
Sets the protocol definition precedence for all packages sent in this socket. This value is used by Linux to arrange network queues: packets with higher priority can be processed first, based on the selected device queuing rules. For
IP(7), the domain of the IP service type (TOS) of the output package is also set.
-
So_error
-
gets and clears unresolved socket errors. Valid only at
getsockopt. is an integer value.
Signals
When a join-oriented socket is written to a closed (by a local or remote terminal), the sigpipe signal is sent to the write process and returned Epipe if the Write command declares msg_nosignal this signal is not emitted when it is identified.
If requested with the Fiocsetown fcntl or siocspgrp ioctl, the SIGIO is emitted when an I/O event occurs so that we can use the POLL > (2) or select(2) to find out which socket the event occurred. Another option (in Linux 2.2) is to set up a real-time signal with the f_setsig fcntl: The real-time signal handler is called when it also receives a file description in the si_fd area of its siginfo_t For more information see fcntl(2)
In some environments (for example, multiple processes accessing a single socket), something that raises SIGIO may have disappeared while the process was responding to the signal. If so, the process should wait again, because Linux will re-send this signal later.
Sysctls
Network system Control (SYSCTL) information can be accessed through the file under directory /proc/sys/net/core/* or by using the sysctl(2) system to access the kernel sockets.
-
Rmem_default
-
Indicates the default number of bytes for the socket receive buffer.
-
Rmem_max
-
Indicates the maximum number of bytes in the socket receive buffer that the user can set by using the
so_rcvbuf socket option.
-
Wmem_default
-
Indicates the default number of bytes for the socket send buffer.
-
Wmem_max
-
Indicates the maximum number of bytes in the send buffer, and the user can set its value by using the
so_sndbuf option of the socket.
-
message_cost and
Message_burst
-
Set the token bucket filter to save a certain number of external network events in the bucket as a warning message.
-
Netdev_max_backlog
-
The maximum number of packets in the global input queue.
-
Optmem_max
The
-
maximum length of the auxiliary and user-controlled data for each socket, such as iovecs.
IOCTLs
The above IO control values can be visited via the ioctl(2):
-
-
error = IOCTL ( ip_socket ,
Ioctl_type , &value_result );
-
siocgstamp
-
Returns the structure of the
Timeval type, including when the last packet sent to the user is received The inter-stamp. is used to measure the exact RTT (round trip time).
struct Timeval . For structural instructions, see
Setitimer (2)
-
siocspgrp
-
When an asynchronous IO operation ends or an emergency data is received, it is used to Set the process or process group to send
SIGIO or
Sigurg signals to it (they), with the parameter pointing to
pid_t . A pointer to the type. If the parameter is positive, the signal is sent to the corresponding process. If the parameter is negative, the signal is sent to all processes of the process group to which the absolute ID of this parameter belongs. If it does not have the
Cap_kill feature or its valid UID is not 0, the process can only select itself or its own process group to receive the signal.
-
fioasync
-
Change the
o_async flag to open or close the asynchronous IO mode of the socket. Asynchronous IO mode refers to the issue of a
SIGIO signal or a signal set with
F_setsig when a new I/O event occurs. The
-
-
parameter is an integer boolean amount.
-
siocgpgrp
-
Gets the current receive
SIGIO or
Sigurg signal A process or process group, or 0 if none of the two signals are set.
Valid FCNTL:
-
Fiocgetown
-
Same as the SIOCGPGRP in IO control.
-
Fiocsetown
-
same as the siocspgrp in IO control.
Attention
Linux assumes that half of the Send/receive buffers are used to handle the kernel structure, so the system controls twice times the buffer that the network can access.
Defects
Config_filter does not introduce so_attach_filter and so_detach_filter socket options. A description of this interface is available in the Libpcap library
VERSIONS version
The So_bindtodevice is introduced in the Linux 2.0.30. so_passcred is a new option introduced in Linux 2.2. Sysctl is in Linux 2.2. New concepts introduced in the
Author
This manual page is written by Andi Kleen.
See Also
Socket (2), IP(7), setsockopt(2), getsockopt(2), packet(7), DDP(7)
Socket Chinese man page function