NAME (名字)
udp - IPv4 上面的 UDP 协议.
SYNOPSIS (总览)
#include <sys/socket.h>
#include <netinet/in.h>
udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
DESCRIPTION(描述)
这是一个 RFC768 中的用户数据报协议的实现. 它实现无连接的, 不可靠的数据报信息包服务. 信息包可能在它们传输到达之前重新排序或者重复. UDP 通过生成和检查校验和来俘获传输错误.
当创建一个 UDP 套接字时, 它的本地和远程地址是不确定的. 可以使用带一个有效目的地址作为参数的 sendto(2) 或者 sendmsg(2) 立即发送数据报.如果套接字上调用了 connect(2) 时, 则设置默认的目的地址, 数据报可以使用 send(2) 或者 write(2) 发送而不需要指定目的地址. 也可以通过传递一个地址给 sendto(2) 或者 sendmsg(2) 来发送到其它目的地址. 为了接收信息包,套接字必须首先用 bind(2) 绑定一个本地地址, 如果没有这么做, 套接字层在第一个用户接收请求时将自动分配一个本地端口.
所有接收操作只返回一个信息包. 当信息包小于所传递的缓冲区时, 则只返回那些数据, 当信息包大于所传递的缓冲区时,则截断信息包并设置 MSG_TRUNC 标志.
IP 选项可以使用描述于 ip(7) 中的套接字选项发送或接收. 只有打开了合适的 sysctl 时, 内核才处理它们(不过即使关闭了它们, 仍然会传递给用户). 参见 ip(7).
如果设置了 MSG_DONTROUTE 标志,则发送时目的地址必须指向一个本地接口地址, 而且信息包只发到该接口.
当 UDP 的总长超过接口 MTU(Maximum Transmission Unit 最大传输单元)时, UDP 会对信息包进行分段. 一个更为网络友好的可选方法是使用 path MTU discovery(路径MTU发现), 它描述于 ip(7) 中的 IP_PMTU_DISCOVER 部分.
ADDRESS FORMAT (地址格式)
UDP 使用 IPv4 的 sockaddr_in 地址格式,其描述于 ip(7) 中.
ERROR HANDLING (错误处理)
所有重大错误都会以错误返回值的方式传递给用户, 即使套接字没有连接亦如此.这种处理方式有别于许多其它的 BSD 套接字实现方法, 除非套接字连接上, 否则那些方法不会传递任何错误, Linux 的处理方式遵循 RFC1122 的要求.
为了与以前的代码兼容,可以设置 SO_BSDCOMPAT SOL_SOCKET 选项令只在套接字已连接的情况下接收远程错误( EPROTO 和 EMSGSIZE) 除外. 最好是修复代码并适当地处理错误, 而不要打开该选项. 本地产生的错误总是传递.
当打开了 IP_RECVERR 选项时, 所有错误可以存储在套接字错误队列中, 并可以通过带 MSG_ERRQUEUE 标识设置的 recvmsg(2) 来接收.
ERRORS (错误)
所有列在 socket(7) 或 ip(7) 中的错误都可以在一个 UDP 套接字上收发时收到.
ECONNREFUSED 没有与目的地址相关联的接收者. 这可能由于在前面一个通过该套接字发送的信息包而引发.
VERSIONS(版本)
IP_RECVERR 是 Linux 2.2 中的新功能.
CREDITS(尾注)
本手册页的作者为 Andi Kleen.
SEE ALSO(另见)
ip(7), socket(7), raw(7).
RFC768: 用户数据报协议.
RFC1122: 主机需求
RFC1191: 描述 path MTU discovery (路径MTU查找).
#p#
NAME
udp - User Datagram Protocol for IPv4
SYNOPSIS
#include <sys/socket.h>
#include <netinet/in.h>
udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
DESCRIPTION
This is an implemention of the User Datagram Protocol described in RFC768. It implements a connectionless, unreliable datagram packet service. Packets may be reordered or duplicated before they arrive. UDP generates and checks checksums to catch transmission errors.
When a UDP socket is created, its local and remote addresses are unspecified. Datagrams can be sent immediately using sendto(2) or sendmsg(2) with a valid destination address as an argument. When connect(2) is called on the socket the default destination address is set and datagrams can now be sent using send(2) or write(2) without specifying an destination address. It is still possible to send to other destinations by passing an address to sendto(2) or sendmsg(2). In order to receive packets the socket can be bound to an local address first by using bind(2). Otherwise the socket layer will automatically assign a free local port out of the range defined by net.ipv4.ip_local_port_range and bind the socket to INADDR_ANY.
All receive operations return only one packet. When the packet is smaller than the passed buffer only that much data is returned, when it is bigger the packet is truncated and the MSG_TRUNC flag is set. MSG_WAITALL is not supported.
IP options may be sent or received using the socket options described in ip(7). They are only processed by the kernel when the appropriate sysctl is enabled (but still passed to the user even when it is turned off). See ip(7).
When the MSG_DONTROUTE flag is set on sending the destination address must refer to an local interface address and the packet is only sent to that interface.
UDP fragments a packet when its total length exceeds the interface MTU (Maximum Transmission Unit). A more network friendly alternative is to use path MTU discovery as described in the IP_MTU_DISCOVER section of ip(7).
ADDRESS FORMAT
UDP uses the IPv4 sockaddr_in address format described in ip(7).
ERROR HANDLING
All fatal errors will be passed to the user as an error return even when the socket is not connected. This includes asynchronous errors received from the network. You may get an error for an earlier packet that was sent on the same socket. This behaviour differs from many other BSD socket implementations which don't pass any errors unless the socket is connected. Linux's behaviour is mandated by RFC1122.
For compatibility with legacy code it is possible to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors only when the socket has been connected (except for EPROTO and EMSGSIZE). It is better to fix the code to handle errors properly than to enable this option. Locally generated errors are always passed.
When the IP_RECVERR option is enabled all errors are stored in the socket error queue and can be received by recvmsg(2) with the MSG_ERRQUEUE flag set.
IOCTLS
These ioctls can be accessed using ioctl(2). The correct syntax is:
-
int value; error = ioctl(tcp_socket, ioctl_type, &value);
- SIOCINQ
- Gets a pointer to an integer as argument. Returns the size of the next pending datagram in the integer in bytes, or 0 when no datagram is pending.
- SIOCOUTQ
- Returns the number of data bytes in the local send queue. Only supported with Linux 2.4 and above.
In addition all ioctls documented in ip(7) and socket(7) are supported.
ERRORS
All errors documented for socket(7) or ip(7) may be returned by a send or receive on a UDP socket.
ECONNREFUSED No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.
VERSIONS
IP_RECVERR is a new feature in Linux 2.2.
CREDITS
This man page was written by Andi Kleen.
SEE ALSO
ip(7), socket(7), raw(7)
RFC768 for the User Datagram Protocol.
RFC1122 for the host requirements.
RFC1191 for a description of path MTU discovery.