⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tyt14fi.htm

📁 一个学习tcp/ip协议的教程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<FONT COLOR=#000080>getsockopt(<I>socket</I>, <I>level</I>, <I>option_id</I>, <I>option_result</I>, <I>length</I>)</FONT></PRE><P><I>socket</I> is the number of the socket; <I>level</I> indicates whether the function refers to the socket itself or the protocol that uses it; <I>option_id</I> is a single integer that identifies the type of information requested; <I>option_result</I> is a pointer to a memory location where the function should place the result of the query; and <I>length</I> is the length of the result.<BR><P>The corresponding setsockopt function lets the application set a value for the socket. The function's format is the same as getsockopt except that <I>option_result</I> points to the value that is to be set, and <I>length</I> is the length of the value.<BR><P>Two functions provide information about the local address of a socket. The getpeername function returns the address of the remote end. The getsockname function returns the local address of a socket. They have the following formats:<BR><PRE><FONT COLOR=#000080>getpeername(<I>socket</I>, <I>destination</I>_<I>address</I>, <I>address</I>_<I>length</I>)getsockname(<I>socket</I>, <I>local_address</I>, <I>address_length</I>)</FONT></PRE><P>The addresses in both functions are pointers to a structure of the format shown in Figure 14.1.<BR><P>Two host name functions for BSD UNIX are gethostname and sethostname, which enable an application to obtain the name of the host and set the host name (if permissions allow). Their formats are as follows:<BR><PRE><FONT COLOR=#000080>sethostname(<I>name</I>, <I>length</I>)gethostname(<I>name</I>, <I>length</I>)</FONT></PRE><P>The <I>name</I> is the address of an array that holds the name, and the <I>length</I> is an integer that gives the name's length.<BR><P>A similar set of functions provides for domain names. The functions setdomainname and getdomainname enable an application to obtain or set the domain names. Their formats are<BR><PRE><FONT COLOR=#000080>setdomainname(<I>name</I>, <I>length</I>)getdomainname(<I>name</I>, <I>length</I>)</FONT></PRE><P>The parameters are the same as with the sethostname and gethostname functions, except for the format of the name (which reflects domain name format).<BR><BR><A ID=E69E187 NAME=E69E187></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR=#FF0000><B>Closing a Connection</B></FONT></CENTER></H4><BR><P>The close function closes a connection. It requires only the local connection name to complete the process. It also takes care of the TCB and releases any variable created by the connection. No output is generated.<BR><P>The close function is initiated with the call<BR><BR><PRE><FONT COLOR=#000080>close(<I>socket</I>)</FONT></PRE><P>where the socket name is required. If an application terminates abnormally, the operating system closes all sockets that were open prior to the termination.<BR><BR><A ID=E69E188 NAME=E69E188></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR=#FF0000><B>Aborting a Connection</B></FONT></CENTER></H4><BR><P>The abort function instructs TCP to discard all data that currently resides in send and receive buffers and close the connection. It takes the local connection name as input. No output is generated. This function can be used in case of emergency shutdown routines, or in case of a fatal failure of the connection or associated software.<BR><P>The abort function is usually implemented by the close() call, although some special instructions might be available with different implementations.<BR><BR><A ID=E69E189 NAME=E69E189></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR=#FF0000><B>UNIX Forks</B></FONT></CENTER></H4><BR><P>UNIX has two system calls that can affect sockets: fork and exec. Both are frequently used by UNIX developers because of their power. (In fact, forks are one of the most powerful tools UNIX offers, and one that most other operating systems lack.) For simplicity, I deal with the two functions as though they perform the same task.<BR><P>A fork call creates a copy of the existing application as a new process and starts executing it. The new process has all the original's file descriptors and socket information. This can cause a problem if the application programmer didn't take into account the fact that two (or more) processes try to use the same socket (or file) simultaneously. Therefore, applications that can fork have to take into account potential conflicts and code around them by checking the status of shared sockets.<BR><P>The operating system itself keeps a table of each socket and how many processes have access to it. An internal counter is incremented or decremented with each process's open or close function call for the socket. When the last process using a socket is terminated, the socket is permanently closed. This prevents one forked process from closing a socket when its original is still using it.<BR><BR><A ID=E68E128 NAME=E68E128></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR=#FF0000><B>Summary</B></FONT></CENTER></H3><BR><P>Today you have seen the basic functions performed by the socket API during establishment of a TCP or UDP call. You have also seen the functions that are available to application programmers. Although the treatment has been at a high level, you should be able to see that working with sockets is not a complex, confusing task. Indeed, socket programming is surprisingly easy once you have tried it.<BR><P>Not everyone wants to write TCP or UDP applications, of course. However, understanding the basics of the socket API helps in understanding the protocol and troubleshooting. If you are interested in programming sockets, one of the best books on the subject is <I>UNIX Network </I><I>Programming, </I>by W. Richard Stevens (Macmillan).<BR><BR><A ID=E68E129 NAME=E68E129></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR=#FF0000><B>Q&amp;A</B></FONT></CENTER></H3><BR><P><B>What is the socket interface used for?</B><BR><P>The socket interface enables you to write applications that make optimal use of the TCP/IP family of protocols. Without it, you would need another layer of application to translate your program's calls to TCP/IP calls.<BR><P><B>What is the difference between blocking and nonblocking functions?</B><BR><P>A blocking function waits for the function to terminate before enabling the application to continue. A nonblocking function enables the application to continue executing while the function is performed. Both have important uses in applications.<BR><P><B>What does binding do?</B><BR><P>Binding makes a logical connection between a socket and the application. Without it, the application couldn't access the socket.<BR><P><B>What happens when an active </B><B>open</B><B> command is executed?</B><BR><P>An active open command creates a socket and binds it, then issues a connect call to identify the IP address and port. The active open command then tries to establish communications.<BR><P><B>What is the difference between an </B><B>abort</B><B> and a </B><B>close</B><B> operation?</B><BR><P>A close operation closes a connection. An abort abandons whatever communications are currently underway and closes the connection. With an abort, any information in receive buffers is discarded.<BR><BR><A ID=E68E130 NAME=E68E130></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR=#FF0000><B>Quiz</B></FONT></CENTER></H3><BR><OL><LI>What are the six basic socket commands?<BR></LI><BR><LI>A Transmission Control Block performs what function?<BR></LI><BR><LI>What is the difference between an unspecified passive open and a fully specified passive open?<BR></LI><BR><LI>What command displays status information about a socket?<BR></LI><BR><LI>What is a fork?<BR></LI><BR></OL><P><P ALIGN=CENTER><A HREF=tyt13fi.htm TARGET=_self><IMG SRC=blanprev.gif WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A><A HREF=#I0 TARGET=_self><IMG SRC=blantop.gif WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Page Top"></A><A HREF=index-1.htm TARGET=_self><IMG SRC=blantoc.gif WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT=TOC></A><A HREF=tytxafi.htm TARGET=_self><IMG SRC=blannext.gif WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -