?? tyt14fi.htm
字號(hào):
<IMG SRC="note.gif" tppabs="http://www.mcp.com/817948800/0-672/0-672-30885-1/note.gif" WIDTH = 75 HEIGHT = 46>Connectionless protocols such as UDP do not require a connected state to function. They can, however, be connected to enable transfer between the two sockets without having to specify the destination address each time. Connection-based protocols such as TCP require both ends of the connection to be specified.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>To establish a connection to a remote socket, the connect function is used. The connect function's format is
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">connect(<I>socket</I>, <I>destination_address</I>, <I>address_length</I>)</FONT></PRE>
<P>The <I>socket</I> is the integer number of the socket to which to connect; the <I>destination_address</I> is the socket address data structure for the destination address (using the same format as shown in Figure 14.1); and the <I>address_length</I> is the length of the destination address in bytes.
<BR>
<P>The manner in which connect functions is protocol-dependent. For TCP, connect establishes the connection between the two endpoints and returns the information about the remote socket to the application. If a connection can't be established, an error message is generated. For a connectionless protocol such as UDP, the connect function is still necessary but stores only the destination address for the application.
<BR>
<BR>
<A ID="E69E182" NAME="E69E182"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B><I>open</I></B><B> Command</B></FONT></CENTER></H4>
<BR>
<P>The open command prepares a communications port for communications. This is an alternative to the combination of the functions shown previously, used by applications for specific purposes. There are really three kinds of open commands, two of which set a server to receive incoming requests and the third used by a client to initiate a request. With every open command, a TCB is created for that connection.
<BR>
<P>The three open commands are an unspecified passive open (which enables a server to wait for a connection request from any client), a fully specified passive open (which enables a server to wait for a connection request from a specific client), and an active open (which initiates a connection with a server). The input and output expected from each command are shown in Table 14.1.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 14.1. Open command parameters.</B></FONT></CENTER>
<BR>
<CENTER><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >
<TR>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P><B><I>Type</I></B>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P><B><I>Input</I></B>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P><B><I>Output</I></B>
</FONT>
<TR>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>Unspecified
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local port
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local connection name
<BR>
</FONT>
<TR>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>passive open
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>Optional: timeout, precedence, security, maximum segment size
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local connection name
<BR>
</FONT>
<TR>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>Fully specified passive open
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local port, remote IP address, remote port Optional: timeout, precedence, security, maximum segment size
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local connection name
<BR>
</FONT>
<TR>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>Active open
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local port, destination IP address, destination port Optional: timeout, precedence, security, maximum segment size
<BR>
</FONT>
<TD BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<P>local connection name</FONT>
</TABLE></CENTER><BR>
<P>When an open command is issued by an application, a set of functions within the socket interface is executed to set up the TCB, initiate the socket number, and establish preliminary values for the variables used in the TCB and the application.
<BR>
<P>The passive open command is issued by a server to wait for incoming requests. With the TCP (connection-based) protocol, the passive open issues the following function calls:
<BR>
<UL>
<LI>socket: Creates the sockets and identifies the type of communications.
<BR></LI>
<BR>
<LI>bind: Establishes the server socket for the connection.
<BR></LI>
<BR>
<LI>listen: Establishes a client queue.
<BR></LI>
<BR>
<LI>accept: Waits for incoming connection requests on the socket.
<BR></LI>
<BR>
</UL>
<P>The active open command is issued by a client. For TCP, it issues two functions:
<BR>
<UL>
<LI>socket: Creates the socket and identifies the communications type.
<BR></LI>
<BR>
<LI>connect: Identifies the server's IP address and port; attempts to establish communications.
<BR></LI>
<BR>
</UL>
<P>If the exact port to use is specified as part of the open command, a bind function call replaces the connect function.
<BR>
<BR>
<A ID="E69E183" NAME="E69E183"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Sending Data</B></FONT></CENTER></H4>
<BR>
<P>There are five functions within the Socket API for sending data through a socket. These are send, sendto, sendmsg, write, and writev. Not surprisingly, all these functions send data from the application to TCP. They do this through a buffer created by the application (for example, it might be a memory address or a character string), passing the entire buffer to TCP. The send, write, and writev functions work only with a connected socket because they have no provision to specify a destination address within their function call.
<BR>
<P>The format of the send function is simple. It takes the local socket connection number, the buffer address for the message to be sent, the length of the message in bytes, a Push flag, and an Urgent flag as parameters. An optional timeout might be specified. Nothing is returned as output from the send function. The format is
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">send(<I>socket</I>, <I>buffer_address</I>, <I>length, flags</I>)</FONT></PRE>
<P>The sendto and sendmsg functions are similar except they enable an application to send a message through an unconnected socket. They both require the destination address as part of their function call. The sendmsg function is simpler in format than the sendto function, primarily because another data structure is used to hold information. The sendmsg function is often used when the format of the sendto function would be awkward and inefficient in the application's code. Their formats are
<BR>
<PRE>
<FONT COLOR="#000080">sendto(<I>socket</I>, <I>buffer</I>_<I>address</I>, <I>length</I>, <I>flags</I>, <I>destination</I>, <I>address</I>_<I>length</I>)
sendmsg(<I>socket</I>, <I>message</I>_<I>structure</I>, <I>flags</I>)</FONT></PRE>
<P>The last two parameters in the sendto function are the destination address and the length of the destination address. The address is specified using the format shown in Figure 14.1. The <I>message_structure</I> of the sendmsg function contains the information left out of the sendto function call. The format of the message structure is shown in Figure 14.3.
<BR>
<P><B><A HREF="14tyt03.gif" tppabs="http://www.mcp.com/817948800/0-672/0-672-30885-1/14tyt03.gif">Figure 14.3. The message structure used by </B><B>sendmsg</B><B>.</A></B>
<BR>
<P>The fields in the sendmsg message structure give the socket address, size of the socket address, a pointer to the iovector, which contains information about the message to be sent, the length of the iovector, the destination address, and the length of the destination address.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>
<IMG SRC="note.gif" tppabs="http://www.mcp.com/817948800/0-672/0-672-30885-1/note.gif" WIDTH = 75 HEIGHT = 46>The sendmsg function uses the message structure to simplify the function call. It also has another advantage: the recvmsg function uses the same structure, simplifying an application's code.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The iovector is an address for an array that points to the message to be sent. The array is a set of pointers to the bytes that comprise the message. The format of the iovector is simple. For each 32-bit address to a memory location with a chunk of the message, a corresponding 32-bit field holds the length of the message in that memory location. This format is repeated until the entire message is specified. This is shown in Figure 14.4. The iovector format enables a noncontiguous message to be sent. In other words, the first part of the message can be in one location in memory, and the rest is separated by other information. This can be useful because it saves the application from copying long messages into a contiguous location.
<BR>
<P><B><A HREF="14tyt04.gif" tppabs="http://www.mcp.com/817948800/0-672/0-672-30885-1/14tyt04.gif">Figure 14.4. The </B><B>iovector</B><B> format.</A></B>
<BR>
<P>The write function takes three arguments: the socket number, the buffer address of the message to be sent, and the length of the message to send. The format of the function call is
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">write(<I>socket</I>, <I>buffer_address</I>, <I>length</I>)</FONT></PRE>
<P>The writev function is similar to write except it uses the iovector to hold the message. This lets it send a message without copying it into another memory address. The format of writev is
<BR>
<BR>
<PRE>
<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>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -