Imported upstream version 1.5
[manpages-zh.git] / raw / man7 / udp.7
blobc187635141a7d163420e36356920f26fb8c1d9b4
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" Permission is granted to distribute possibly modified copies
3 .\" of this page provided the header is included verbatim,
4 .\" and in case of nontrivial modification author and date
5 .\" of the modification is added to the header.
6 .\" $Id: udp.7,v 1.1 2003/12/20 03:31:54 bbbush Exp $
7 .TH UDP  7 1998-10-02 "Linux Man Page" "Linux Programmer's Manual" 
8 .SH NAME
9 udp \- User Datagram Protocol for IPv4
10 .SH SYNOPSIS
11 .B #include <sys/socket.h>
12 .br
13 .B #include <netinet/in.h>
14 .br
15 .B udp_socket = socket(PF_INET, SOCK_DGRAM, 0); 
16 .SH DESCRIPTION
17 This is an implemention of the User Datagram Protocol described in RFC768. It 
18 implements a connectionless, unreliable datagram packet service.
19 Packets may be reordered or duplicated before they arrive. UDP
20 generates and checks checksums to catch transmission errors.  
22 When a UDP socket is created, its local and remote addresses are unspecified.
23 Datagrams can be sent immediately using 
24 .BR sendto (2)
26 .BR sendmsg (2)
27 with a valid destination address as an argument.  When 
28 .BR connect (2) 
29 is called on the socket the default destination address is set and datagrams 
30 can now be sent using 
31 .BR send (2)
32 or 
33 .BR write (2)
34 without specifying an destination address.
35 It is still possible to send to other destinations by passing an address to
36 .BR sendto (2)
38 .BR sendmsg (2).
39 In order to receive packets the socket can be bound to an local
40 address first by using
41 .BR bind (2).
42 Otherwise the socket layer will automatically assign
43 a free local port out of the range defined by
44 .I net.ipv4.ip_local_port_range
45 and bind the socket to
46 .IR INADDR_ANY .
48 All receive operations return only one packet.  When the packet is smaller
49 than the passed buffer only that much data is returned, when it is bigger
50 the packet is truncated and the
51 .B MSG_TRUNC
52 flag is set.
53 .I MSG_WAITALL
54 is not supported.
56 IP options may be sent or received using the socket options described in 
57 .BR ip (7).
58 They are only processed by the kernel when the appropriate sysctl
59 is enabled (but still passed to the user even when it is turned off). See
60 .BR ip (7).
62 When the 
63 .B MSG_DONTROUTE
64 flag is set on sending the destination address must refer to an local 
65 interface address and the packet is only sent to that interface.  
67 UDP fragments a packet when its total length exceeds the interface MTU
68 (Maximum Transmission Unit).
69 A more network friendly alternative is to use path MTU discovery
70 as described in the
71 .B IP_PMTU_DISCOVER 
72 section of
73 .BR ip (7).
75 .SH "ADDRESS FORMAT"
76 UDP uses the IPv4 
77 .B sockaddr_in 
78 address format described in 
79 .BR ip (7). 
81 .SH "ERROR HANDLING"
82 All fatal errors will be passed to the user as an error return even 
83 when the socket is not connected. This includes asynchronous errors
84 received from the network. You may get an error for an earlier packet
85 that was sent on the same socket.
86 This behaviour differs from many other BSD socket implementations
87 which don't pass any errors unless the socket is connected.
88 Linux's behaviour is mandated by 
89 .BR RFC1122 .
91 For compatibility with legacy code it is possible to set the
92 .B SO_BSDCOMPAT  
93 SOL_SOCKET option to receive remote errors only when the socket has been 
94 connected (except for
95 .B EPROTO
96 and
97 .BR EMSGSIZE ).
98 It is better to fix the
99 code to handle errors properly than to enable this option.
100 Locally generated errors are always passed.
102 When the 
103 .B IP_RECVERR
104 option is enabled all errors are stored in the socket error queue
105 and can be received by
106 .BR recvmsg (2)
107 with the 
108 .B MSG_ERRQUEUE
109 flag set.
110 .SH IOCTLS
111 These ioctls can be accessed using
112 .BR ioctl (2).
113 The correct syntax is:
117 .BI int " value";
118 .IB error " = ioctl(" tcp_socket ", " ioctl_type ", &" value ");"
122 .B SIOCINQ
123 Gets a pointer to an integer as argument. Returns the size of the next
124 pending datagram in the integer in bytes, or 0 when no datagram is pending.
126 .B SIOCOUTQ
127 Returns the number of data bytes in the local send queue. Only supported
128 with Linux 2.4 and above.
130 In addition all ioctls documented in
131 .BR ip (7)
133 .BR socket (7)
134 are supported.
135 .SH ERRORS
136 All errors documented for 
137 .BR socket (7)
138 or 
139 .BR ip (7)
140 may be returned by a send or receive on a UDP socket. 
142 .B ECONNREFUSED
143 No receiver was associated with the destination address.  This might be
144 caused by a previous packet sent over the socket.
146 .SH VERSIONS
147 IP_RECVERR is a new feature in Linux 2.2.
149 .SH CREDITS
150 This man page was written by Andi Kleen.
152 .SH "SEE ALSO"
153 .BR ip (7),
154 .BR socket (7),
155 .BR raw (7)
157 RFC768 for the User Datagram Protocol.
159 RFC1122 for the host requirements.
161 RFC1191 for a description of path MTU discovery.