📄 tyt14fi.htm
字号:
<FONT COLOR=#000080>writev(<I>socket</I>, <I>iovector</I>, <I>length</I>)</FONT></PRE><P>where <I>length</I> is the number of entries in iovector.<BR><P>The type of function chosen to send data through a socket depends on the type of connection used and the level of complexity of the application. To a considerable degree, it is also a personal choice of the programmer.<BR><BR><A ID=E69E184 NAME=E69E184></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR=#FF0000><B>Receiving Data</B></FONT></CENTER></H4><BR><P>Not surprisingly, because there are five functions to send data through a socket, there are five corresponding functions to receive data: read, readv, recv, recvfrom, and recvmsg. They all accept incoming data from a socket into a reception buffer. The receive buffer can then be transferred from TCP to the application.<BR><P>The read function is the simplest and can be used only when a socket is connected. Its format is<BR><BR><PRE><FONT COLOR=#000080>read(<I>socket</I>, <I>buffer</I>, <I>length</I>)</FONT></PRE><P>The first parameter is the number of the socket or a file descriptor from which to read the data, followed by the memory address in which to store the incoming data, and the maximum number of bytes to be read.<BR><P>As with writev, the readv command enables incoming messages to be placed in noncontiguous memory locations through the use of an iovector. The format of readv is<BR><BR><PRE><FONT COLOR=#000080>readv(<I>socket</I>, <I>iovector</I>, <I>length</I>)</FONT></PRE><P><I>length</I> is the number of entries in the iovector. The format of the iovector is the same as mentioned previously and shown in Figure 14.4.<BR><P>The recv function also can be used with connected sockets. It has the format<BR><BR><PRE><FONT COLOR=#000080>recv(<I>socket</I>, <I>buffer_address</I>, <I>length</I>, <I>flags</I>)</FONT></PRE><P>which corresponds to the send function's arguments.<BR><P>The recvfrom and recvmsg functions enable data to be read from an unconnected socket. Their formats include the sender's address:<BR><PRE><FONT COLOR=#000080>recvfrom(<I>socket</I>, <I>buffer</I>_<I>address</I>, <I>length</I>, <I>flags</I>, <I>source</I>_<I>address</I>, <I>address</I>_<I>length</I>)recvmsg(<I>socket</I>, <I>message</I>_<I>structure</I>, <I>flags</I>)</FONT></PRE><P>The message structure in the recvmsg function corresponds to the structure in sendmsg. (See Figure 14.3.)<BR><BR><A ID=E69E185 NAME=E69E185></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR=#FF0000><B>Server Listening</B></FONT></CENTER></H4><BR><P>A server application that expects clients to call in to it has to create a socket (using socket), bind it to a port (with bind), then wait for incoming requests for data. The listen function handles problems that could occur with this type of behavior by establishing a queue for incoming connection requests. The queue prevents bottlenecks and collisions, such as when a new request arrives before a previous one has been completely handled, or two requests arrive simultaneously.<BR><P>The listen function establishes a buffer to queue incoming requests, thereby avoiding losses. The function lets the socket accept incoming connection requests, which are all sent to the queue for future processing. The function's format is<BR><BR><PRE><FONT COLOR=#000080>listen(<I>socket</I>, <I>queue_length</I>)</FONT></PRE><P>where <I>queue_length</I> is the size of the incoming buffer. If the buffer has room, incoming requests for connections are added to the buffer and the application can deal with them in the order of reception. If the buffer is full, the connection request is rejected.<BR><P>After the server has used listen to set up the incoming connection request queue, the accept function is used to actually wait for a connection. The format of the function is<BR><BR><PRE><FONT COLOR=#000080>accept(<I>socket</I>, <I>address</I>, <I>length</I>)</FONT></PRE><P><I>socket</I> is the socket on which to accept requests; <I>address</I> is a pointer to a structure similar to Figure 14.1; and <I>length</I> is a pointer to an integer showing the length of the address.<BR><P>When a connection request is received, the protocol places the address of the client in the memory location indicated by the address parameter, and the length of that address in the length location. It then creates a new socket that has the client and server connected together, sending back the socket description to the client. The socket on which the request was received remains open for other connection requests. This enables multiple requests for a connection to be processed, whereas if that socket was closed down with each connection request, only one client/server process could be handled at a time.<BR><P>One possible special occurrence must be handled on UNIX systems. It is possible for a single process to wait for a connection request on multiple sockets. This reduces the number of processes that monitor sockets, thereby lowering the amount of overhead the machine uses. To provide for this type of process, the select function is used. The format of the function is<BR><BR><PRE><FONT COLOR=#000080>select(<I>num_desc</I>, <I>in_desc</I>, <I>out_desc</I>, <I>excep_desc</I>, <I>timeout</I>)</FONT></PRE><P><I>num_desc</I> is the number of sockets or descriptors that are monitored; <I>in_desc</I> and <I>out_desc</I> are pointers to a bit mask that indicates the sockets or file descriptors to monitor for input and output, respectively; <I>excep_desc</I> is a pointer to a bit mask that specifies the sockets or file descriptors to check for exception conditions; and <I>timeout</I> is a pointer to an integer that indicates how long to wait (a value of 0 indicates forever). To use the select function, a server creates all the necessary sockets first, then calls select to determine which ones are for input, output, and exceptions.<BR><BR><A ID=E69E186 NAME=E69E186></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR=#FF0000><B>Getting Status Information</B></FONT></CENTER></H4><BR><P>Several status functions are used to obtain information about a connection. They can be used at any time, although they are typically used to establish the integrity of a connection in case of problems or to control the behavior of the socket.<BR><P>The status functions require the name of the local connection, and they return a set of information, which might include the local and remote socket names, local connection name, receive and send window states, number of buffers waiting for an acknowledgment, number of buffers waiting for data, and current values for the urgent state, precedence, security, and timeout variables. Most of this information is read from the Transmission Control Block (TCB). The format of the information and the exact contents vary slightly, depending on the implementation.<BR><P>The function getsockopt enables an application to query the socket for information. The function format is<BR><BR><PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -