udp 中文man頁面
NAME (名字)
udp - IPv4 上面的 UDP 協(xié)議.
SYNOPSIS (總覽)
#include <sys/socket.h>
#include <netinet/in.h>
udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
DESCRIPTION(描述)
這是一個 RFC768 中的用戶數(shù)據(jù)報協(xié)議的實現(xiàn). 它實現(xiàn)無連接的, 不可靠的數(shù)據(jù)報信息包服務(wù). 信息包可能在它們傳輸?shù)竭_之前重新排序或者重復(fù). UDP 通過生成和檢查校驗和來俘獲傳輸錯誤.
當(dāng)創(chuàng)建一個 UDP 套接字時, 它的本地和遠程地址是不確定的. 可以使用帶一個有效目的地址作為參數(shù)的 sendto(2) 或者 sendmsg(2) 立即發(fā)送數(shù)據(jù)報.如果套接字上調(diào)用了 connect(2) 時, 則設(shè)置默認的目的地址, 數(shù)據(jù)報可以使用 send(2) 或者 write(2) 發(fā)送而不需要指定目的地址. 也可以通過傳遞一個地址給 sendto(2) 或者 sendmsg(2) 來發(fā)送到其它目的地址. 為了接收信息包,套接字必須首先用 bind(2) 綁定一個本地地址, 如果沒有這么做, 套接字層在第一個用戶接收請求時將自動分配一個本地端口.
所有接收操作只返回一個信息包. 當(dāng)信息包小于所傳遞的緩沖區(qū)時, 則只返回那些數(shù)據(jù), 當(dāng)信息包大于所傳遞的緩沖區(qū)時,則截斷信息包并設(shè)置 MSG_TRUNC 標(biāo)志.
IP 選項可以使用描述于 ip(7) 中的套接字選項發(fā)送或接收. 只有打開了合適的 sysctl 時, 內(nèi)核才處理它們(不過即使關(guān)閉了它們, 仍然會傳遞給用戶). 參見 ip(7).
如果設(shè)置了 MSG_DONTROUTE 標(biāo)志,則發(fā)送時目的地址必須指向一個本地接口地址, 而且信息包只發(fā)到該接口.
當(dāng) UDP 的總長超過接口 MTU(Maximum Transmission Unit 最大傳輸單元)時, UDP 會對信息包進行分段. 一個更為網(wǎng)絡(luò)友好的可選方法是使用 path MTU discovery(路徑MTU發(fā)現(xiàn)), 它描述于 ip(7) 中的 IP_PMTU_DISCOVER 部分.
ADDRESS FORMAT (地址格式)
UDP 使用 IPv4 的 sockaddr_in 地址格式,其描述于 ip(7) 中.
ERROR HANDLING (錯誤處理)
所有重大錯誤都會以錯誤返回值的方式傳遞給用戶, 即使套接字沒有連接亦如此.這種處理方式有別于許多其它的 BSD 套接字實現(xiàn)方法, 除非套接字連接上, 否則那些方法不會傳遞任何錯誤, Linux 的處理方式遵循 RFC1122 的要求.
為了與以前的代碼兼容,可以設(shè)置 SO_BSDCOMPAT SOL_SOCKET 選項令只在套接字已連接的情況下接收遠程錯誤( EPROTO 和 EMSGSIZE) 除外. 最好是修復(fù)代碼并適當(dāng)?shù)靥幚礤e誤, 而不要打開該選項. 本地產(chǎn)生的錯誤總是傳遞.
當(dāng)打開了 IP_RECVERR 選項時, 所有錯誤可以存儲在套接字錯誤隊列中, 并可以通過帶 MSG_ERRQUEUE 標(biāo)識設(shè)置的 recvmsg(2) 來接收.
ERRORS (錯誤)
所有列在 socket(7) 或 ip(7) 中的錯誤都可以在一個 UDP 套接字上收發(fā)時收到.
ECONNREFUSED 沒有與目的地址相關(guān)聯(lián)的接收者. 這可能由于在前面一個通過該套接字發(fā)送的信息包而引發(fā).
VERSIONS(版本)
IP_RECVERR 是 Linux 2.2 中的新功能.
CREDITS(尾注)
本手冊頁的作者為 Andi Kleen.
SEE ALSO(另見)
ip(7), socket(7), raw(7).
RFC768: 用戶數(shù)據(jù)報協(xié)議.
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.