📄 socklib.html
字号:
<p></blockquote><h4>OPTION FOR DATAGRAM SOCKETS</h4><blockquote><p>The following section discusses an option for datagram (UDP) sockets.<p></blockQuote><h4>SO_BROADCAST -- Sending to Multiple Destinations</h4><blockQuote>Specify the <b>SO_BROADCAST</b> option when an application needs to senddata to more than one destination:<pre> setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval));</pre>The value at <i>optval</i> is an integer (type <i>int</i>), either 1 (on) or 0(off).<p></blockquote><h4>OPTIONS FOR DATAGRAM AND RAW SOCKETS</h4><blockquote><p>The following section discusses options for multicasting on UDP and RAWsockets.<p></blockQuote><h4>IP_ADD_MEMBERSHIP -- Join a Multicast Group</h4><blockQuote>Specify the <b>IP_ADD_MEMBERSHIP</b> option when a process needs to joinmulticast group:<pre> setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq, sizeof (ipMreq));</pre>The value of <i>ipMreq</i> is an <b>ip_mreq</b> structure. <b>ipMreq.imr_multiaddr.s</b>_addr</b> is the internet multicast address<b>ipMreq.imr_interface.s</b>_addr</b> is the internet unicast address of the interface through which the multicast packet needs to pass.<p></blockQuote><h4>IP_DROP_MEMBERSHIP -- Leave a Multicast Group</h4><blockQuote>Specify the <b>IP_DROP_MEMBERSHIP</b> option when a process needs to leavea previously joined multicast group:<pre> setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&ipMreq, sizeof (ipMreq));</pre>The value of <i>ipMreq</i> is an <b>ip_mreq</b> structure. <b>ipMreq.imr_multiaddr.s</b>_addr</b> is the internet multicast address.<b>ipMreq.imr_interface.s</b>_addr</b> is the internet unicast address of the interface to which the multicast address was bound.<p></blockQuote><h4>IP_MULTICAST_IF -- Select a Default Interface for Outgoing Multicasts</h4><blockQuote>Specify the <b>IP_MULTICAST_IF</b> option when an application needs to specifyan outgoing network interface through which all multicast packetsare sent:<pre> setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&ifAddr, sizeof (mCastAddr));</pre>The value of <i>ifAddr</i> is an <b>in_addr</b> structure. <b>ifAddr.s</b>_addr</b> is the internet network interface address.<p></blockQuote><h4>IP_MULTICAST_TTL -- Select a Default TTL</h4><blockQuote>Specify the <b>IP_MULTICAST_TTL</b> option when an application needs to selecta default TTL (time to live) for outgoing multicastpackets:<p><pre> setsockopt (sock, IPPROTO_IP, IP_MULTICAST_TTL, &optval, sizeof(optval));</pre>The value at <i>optval</i> is an integer (type <i>int</i>), time to live value.<p><table><tr valign=top><th align=left>optval(TTL) </th><th align=left> Application </th><th align=left> Scope</tr><tr><td colspan="3"><hr></tr><tr valign=top><td align=left>0 </td><td align=left> </td><td align=left> same interface</tr><tr valign=top><td align=left>1 </td><td align=left> </td><td align=left> same subnet</tr><tr valign=top><td align=left>31 </td><td align=left> local event video </td><td align=left> </tr><tr valign=top><td align=left>32 </td><td align=left> </td><td align=left> same site</tr><tr valign=top><td align=left>63 </td><td align=left> local event audio </td><td align=left> </tr><tr valign=top><td align=left>64 </td><td align=left> </td><td align=left> same region</tr><tr valign=top><td align=left>95 </td><td align=left> IETF channel 2 video </td><td align=left> </tr><tr valign=top><td align=left>127 </td><td align=left> IETF channel 1 video </td><td align=left> </tr><tr valign=top><td align=left>128 </td><td align=left> </td><td align=left> same continent</tr><tr valign=top><td align=left>159 </td><td align=left> IETF channel 2 audio </td><td align=left> </tr><tr valign=top><td align=left>191 </td><td align=left> IETF channel 1 audio </td><td align=left> </tr><tr valign=top><td align=left>223 </td><td align=left> IETF channel 2 low-rate audio </td><td align=left> </tr><tr valign=top><td align=left>255 </td><td align=left> IETF channel 1 low-rate audio </td><td align=left></tr><tr valign=top><td align=left> </td><td align=left> unrestricted in scope </td><td align=left></tr><tr valign=top><td align=left></tr></tr></table></blockQuote><h4>IP_MULTICAST_LOOP -- Enable or Disable Loopback</h4><blockQuote>Enable or disable loopback of outgoing multicasts.<pre> setsockopt (sock, IPPROTO_IP, IP_MULTICAST_LOOP, &optval, sizeof(optval));</pre>The value at <i>optval</i> is an integer (type <i>int</i>), either 1(on) or 0(off).<p></blockquote><h4>OPTIONS FOR BOTH STREAM AND DATAGRAM SOCKETS</h4><blockquote><p>The following sections describe options that can be used with eitherstream or datagram sockets.<p></blockQuote><h4>SO_REUSEADDR -- Reusing a Socket Address</h4><blockQuote>Specify the <b>SO_REUSEADDR</b> option to bind a stream socket to a localport that may be still bound to another stream socket:<pre> setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));</pre>The value at <i>optval</i> is an integer (type <i>int</i>), either 1 (on) or 0(off).<p>When the <b>SO_REUSEADDR</b> option is turned on, applications may bind astream socket to a local port even if it is still bound to anotherstream socket, if that other socket is associated with a "zombie" protocolcontrol block context not yet freed from previous sessions. Theuniqueness of port number combinations for each connection is stillpreserved through sanity checks performed at actual connectionsetup time. If this option is not turned on and an applicationattempts to bind to a port which is being used by a zombie protocolcontrol block, the <b><i><a href="./sockLib.html#bind">bind</a></i>( )</b> call fails.<p></blockQuote><h4>SO_SNDBUF -- Specifying the Size of the Send Buffer</h4><blockQuote>Specify the <b>SO_SNDBUF</b> option to adjust the maximum size of thesocket-level send buffer:<pre> setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof (optval));</pre>The value at <i>optval</i> is an integer (type <b>int</b>) that specifies thesize of the socket-level send buffer to be allocated.<p>When stream or datagram sockets are created, each transport protocolreserves a set amount of space at the socket level for use when thesockets are attached to a protocol. For TCP, the default size ofthe send buffer is 8192 bytes. For UDP, the default size of thesend buffer is 9216 bytes. Socket-level buffers are allocateddynamically from the mbuf pool.<p>The effect of setting the maximum size of buffers (for both<b>SO_SNDBUF</b> and <b>SO_RCVBUF</b>, described below) is not actually toallocate the mbufs from the mbuf pool, but to set the high-watermark in the protocol data structure which is used later to limit theamount of mbuf allocation. Thus, the maximum size specified for thesocket level send and receive buffers can affect the performance ofbulk data transfers. For example, the size of the TCP receivewindows is limited by the remaining socket-level buffer space.These parameters must be adjusted to produce the optimal result fora given application.<p></blockQuote><h4>SO_RCVBUF -- Specifying the Size of the Receive Buffer</h4><blockQuote>Specify the <b>SO_RCVBUF</b> option to adjust the maximum size of thesocket-level receive buffer:<pre> setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof (optval));</pre>The value at <i>optval</i> is an integer (type <b>int</b>) that specifies thesize of the socket-level receive buffer to be allocated.<p>When stream or datagram sockets are created, each transport protocolreserves a set amount of space at the socket level for use when thesockets are attached to a protocol. For TCP, the default size is8192 bytes. UDP reserves 41600 bytes, enough space for up to fortyincoming datagrams (1 Kbyte each).<p>See the <b>SO_SNDBUF</b> discussion above for a discussion of the impact ofbuffer size on application performance.<p></blockQuote><h4>SO_OOBINLINE -- Placing Urgent Data in the Normal Data Stream</h4><blockQuote>Specify the <b>SO_OOBINLINE</b> option to place urgent data within thenormal receive data stream:<pre> setsockopt (sock, SOL_SOCKET, SO_OOBINLINE, &optval, sizeof (optval));</pre>TCP provides an expedited data service which does not conform to thenormal constraints of sequencing and flow control of datastreams. The expedited service delivers "out-of-band" (urgent) dataahead of other "normal" data to provide interrupt-like services (forexample, when you hit a CTRL-C during telnet or rlogin session whiledata is being displayed on the screen.)<p>TCP does not actually maintain a separate stream to support theurgent data. Instead, urgent data delivery is implemented as apointer (in the TCP header) which points to the sequence number ofthe octet following the urgent data. If more than one transmissionof urgent data is received from the peer, they are all put into thenormal stream. This is intended for applications that cannot affordto miss out on any urgent data but are usually too slow to respondto them promptly.<p></blockquote><h4>RETURNS</h4><blockquote><p><p>OK, or ERROR if there is an invalid socket, an unknown option, an optionlength greater than MLEN, insufficient mbufs, or the call is unable to setthe specified option.</blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./sockLib.html#top">sockLib</a></b><hr><a name="getsockopt"></a><p align=right><a href="rtnIndex.html"><i>Libraries : Routines</i></a></p></blockquote><h1><i>getsockopt</i>( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong><i>getsockopt</i>( )</strong> - get socket options</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>STATUS getsockopt ( int s, /* socket */ int level, /* protocol level for options */ int optname, /* name of option */ char * optval, /* where to put option */ int * optlen /* where to put option length */ )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine returns relevant option values associated with a socket.To manipulate options at the "socket" level, <i>level</i> should be <b>SOL_SOCKET</b>.Any other levels should use the appropriate protocol number.The parameter <i>optlen</i> should be initialized to indicate the amount ofspace referenced by <i>optval</i>. On return, the value of the option is copied to<i>optval</i> and the actual size of the option is copied to <i>optlen</i>.<p>Although <i>optval</i> is passed as a char *, the actual variable whose address gets passed in should be an integer or a structure, depending on which <i>optname</i> is being passed. Refer to <b><i><a href="./sockLib.html#setsockopt">setsockopt</a></i>( )</b> to determine the correct type of the actual variable (whose address should then be cast to a char *).<p></blockquote><h4>RETURNS</h4><blockquote><p><p>OK, or ERROR if there is an invalid socket, an unknown option, or the callis unable to get the specified option.<p></blockquote><h4>EXAMPLE</h4><blockquote><p>Because <b>SO_REUSEADDR</b> has an integer parameter, the variable to bepassed to <b><i><a href="./sockLib.html#getsockopt">getsockopt</a></i>( )</b> should be declared as<pre> int reuseVal;</pre>and passed in as <pre> (char *)&reuseVal.</pre>Otherwise the user might mistakenly declare <b>reuseVal</b> as a character,in which case <b><i><a href="./sockLib.html#getsockopt">getsockopt</a></i>( )</b> will only return the first byte of theinteger representing the state of this option. Then whether the returnvalue is correct or always 0 depends on the endian-ness of the machine.<p></blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./sockLib.html#top">sockLib</a></b>, <b><i><a href="./sockLib.html#setsockopt">setsockopt</a></i>( )</b><hr><a name="getsockname"></a><p align=right><a href="rtnIndex.html"><i>Libraries : Routines</i></a></p></blockquote><h1><i>getsockname</i>( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong><i>getsockname</i>( )</strong> - get a socket name</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>STATUS getsockname ( int s, /* socket descriptor */ struct sockaddr * name, /* where to return name */ int * namelen /* space available in name, later filled in */ /* actual name size */ )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine gets the current name for the specified socket <i>s</i>.The parameter <i>namelen</i> should be initialized to indicate the amount ofspace referenced by <i>name</i>. On return, the name of the socket is copied to<i>name</i> and the actual size of the socket name is copied to <i>namelen</i>.<p></blockquote><h4>RETURNS</h4><blockquote><p>OK, or ERROR if the socket is invalidor not connected.</blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./sockLib.html#top">sockLib</a></b><hr><a name="getpeername"></a><p align=right><a href="rtnIndex.html"><i>Libraries : Routines</i></a></p></blockquote><h1><i>getpeername</i>( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong><i>getpeername</i>( )</strong> - get the name of a connected peer</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>STATUS getpeername ( int s, /* socket descriptor */ struct sockaddr * name, /* where to put name */ int * namelen /* space available in name, later filled in */ /* actual name size */ )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine gets the name of the peer connected to socket <i>s</i>.The parameter <i>namelen</i> should be initialized to indicate the amount ofspace referenced by <i>name</i>. On return, the name of the socket is copied to<i>name</i> and the actual size of the socket name is copied to <i>namelen</i>.<p></blockquote><h4>RETURNS</h4><blockquote><p>OK, or ERROR if the socket is invalidor not connected.</blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./sockLib.html#top">sockLib</a></b><hr><a name="shutdown"></a><p align=right><a href="rtnIndex.html"><i>Libraries : Routines</i></a></p></blockquote><h1><i>shutdown</i>( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong><i>shutdown</i>( )</strong> - shut down a network connection</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>STATUS shutdown ( int s, /* socket to shut down */ int how /* 0 = receives disallowed 1 = sends disallowed 2 = sends and */ /* disallowed */ )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine shuts down all, or part, of a connection-based socket <i>s</i>.If the value of <i>how</i> is 0, receives are disallowed. If <i>how</i> is 1,sends are disallowed. If <i>how</i> is 2, both sends and receives aredisallowed.<p></blockquote><h4>RETURNS</h4><blockquote><p>OK, or ERROR if the socket is invalid or not connected.</blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./sockLib.html#top">sockLib</a></b></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -