📄 serversocket.html
字号:
public <B>ServerSocket</B>(int port, int backlog) throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Creates a server socket and binds it to the specified local port number, with the specified backlog. A port number of <code>0</code> creates a socket on any free port. <p> The maximum queue length for incoming connection indications (a request to connect) is set to the <code>backlog</code> parameter. If a connection indication arrives when the queue is full, the connection is refused. <p> If the application has specified a server socket factory, that factory's <code>createSocketImpl</code> method is called to create the actual socket implementation. Otherwise a "plain" socket is created. <p> If there is a security manager, its <code>checkListen</code> method is called with the <code>port</code> argument as its argument to ensure the operation is allowed. This could result in a SecurityException.<DD><DL><DT><B>Parameters:</B><DD><CODE>port</CODE> - the specified port, or <code>0</code> to use any free port.<DD><CODE>backlog</CODE> - the maximum length of the queue.<DT><B>Throws:</B><DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs when opening the socket.<DD><CODE><A HREF="../../java/lang/SecurityException.html">SecurityException</A></CODE> - if a security manager exists and its <code>checkListen</code> method doesn't allow the operation.<DT><B>See Also: </B><DD><A HREF="../../java/net/SocketImpl.html"><CODE>SocketImpl</CODE></A>, <A HREF="../../java/net/SocketImplFactory.html#createSocketImpl()"><CODE>SocketImplFactory.createSocketImpl()</CODE></A>, <A HREF="../../java/net/ServerSocket.html#setSocketFactory(java.net.SocketImplFactory)"><CODE>setSocketFactory(java.net.SocketImplFactory)</CODE></A>, <A HREF="../../java/lang/SecurityManager.html#checkListen(int)"><CODE>SecurityManager.checkListen(int)</CODE></A></DL></DD></DL><HR><A NAME="ServerSocket(int, int, java.net.InetAddress)"><!-- --></A><H3>ServerSocket</H3><PRE>public <B>ServerSocket</B>(int port, int backlog, <A HREF="../../java/net/InetAddress.html">InetAddress</A> bindAddr) throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Create a server with the specified port, listen backlog, and local IP address to bind to. The <i>bindAddr</i> argument can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses. If <i>bindAddr</i> is null, it will default accepting connections on any/all local addresses. The port must be between 0 and 65535, inclusive. <P>If there is a security manager, this method calls its <code>checkListen</code> method with the <code>port</code> argument as its argument to ensure the operation is allowed. This could result in a SecurityException. <P><DD><DL><DT><B>Parameters:</B><DD><CODE>port</CODE> - the local TCP port<DD><CODE>backlog</CODE> - the listen backlog<DD><CODE>bindAddr</CODE> - the local InetAddress the server will bind to<DT><B>Throws:</B><DD><CODE><A HREF="../../java/lang/SecurityException.html">SecurityException</A></CODE> - if a security manager exists and its <code>checkListen</code> method doesn't allow the operation.<DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs when opening the socket.<DT><B>Since: </B><DD>JDK1.1</DD><DT><B>See Also: </B><DD><A HREF="../../java/net/SocketOptions.html"><CODE>SocketOptions</CODE></A>, <A HREF="../../java/net/SocketImpl.html"><CODE>SocketImpl</CODE></A>, <A HREF="../../java/lang/SecurityManager.html#checkListen(int)"><CODE>SecurityManager.checkListen(int)</CODE></A></DL></DD></DL><!-- ============ METHOD DETAIL ========== --><A NAME="method_detail"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=1><FONT SIZE="+2"><B>Method Detail</B></FONT></TD></TR></TABLE><A NAME="getInetAddress()"><!-- --></A><H3>getInetAddress</H3><PRE>public <A HREF="../../java/net/InetAddress.html">InetAddress</A> <B>getInetAddress</B>()</PRE><DL><DD>Returns the local address of this server socket.<DD><DL><DT><B>Returns:</B><DD>the address to which this socket is connected, or <code>null</code> if the socket is not yet connected.</DL></DD></DL><HR><A NAME="getLocalPort()"><!-- --></A><H3>getLocalPort</H3><PRE>public int <B>getLocalPort</B>()</PRE><DL><DD>Returns the port on which this socket is listening.<DD><DL><DT><B>Returns:</B><DD>the port number to which this socket is listening.</DL></DD></DL><HR><A NAME="accept()"><!-- --></A><H3>accept</H3><PRE>public <A HREF="../../java/net/Socket.html">Socket</A> <B>accept</B>() throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. <p>A new Socket <code>s</code> is created and, if there is a security manager, the security manager's <code>checkAccept</code> method is called with <code>s.getInetAddress().getHostAddress()</code> and <code>s.getPort()</code> as its arguments to ensure the operation is allowed. This could result in a SecurityException.<DD><DL><DT><B>Returns:</B><DD>the new Socket<DT><B>Throws:</B><DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs when waiting for a connection.<DD><CODE><A HREF="../../java/lang/SecurityException.html">SecurityException</A></CODE> - if a security manager exists and its <code>checkListen</code> method doesn't allow the operation.<DT><B>See Also: </B><DD><A HREF="../../java/lang/SecurityManager.html#checkAccept(java.lang.String, int)"><CODE>SecurityManager.checkAccept(java.lang.String, int)</CODE></A></DL></DD></DL><HR><A NAME="implAccept(java.net.Socket)"><!-- --></A><H3>implAccept</H3><PRE>protected final void <B>implAccept</B>(<A HREF="../../java/net/Socket.html">Socket</A> s) throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket. So a FooServerSocket will typically hand this method an <i>empty</i> FooSocket. On return from implAccept the FooSocket will be connected to a client.<DD><DL><DT><B>Parameters:</B><DD><CODE>s</CODE> - the Socket<DT><B>Throws:</B><DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs when waiting for a connection.<DT><B>Since: </B><DD>JDK1.1</DD></DL></DD></DL><HR><A NAME="close()"><!-- --></A><H3>close</H3><PRE>public void <B>close</B>() throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Closes this socket.<DD><DL><DT><B>Throws:</B><DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs when closing the socket.</DL></DD></DL><HR><A NAME="setSoTimeout(int)"><!-- --></A><H3>setSoTimeout</H3><PRE>public void <B>setSoTimeout</B>(int timeout) throws <A HREF="../../java/net/SocketException.html">SocketException</A></PRE><DL><DD>Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time. If the timeout expires, a <B>java.io.InterruptedIOException</B> is raised, though the ServerSocket is still valid. The option <B>must</B> be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.<DD><DL><DT><B>Parameters:</B><DD><CODE>timeout</CODE> - the specified timeout, in milliseconds<DT><B>Throws:</B><DD><CODE><A HREF="../../java/net/SocketException.html">SocketException</A></CODE> - if there is an error in the underlying protocol, such as a TCP error.<DT><B>Since: </B><DD>JDK1.1</DD><DT><B>See Also: </B><DD><A HREF="../../java/net/ServerSocket.html#getSoTimeout()"><CODE>getSoTimeout()</CODE></A></DL></DD></DL><HR><A NAME="getSoTimeout()"><!-- --></A><H3>getSoTimeout</H3><PRE>public int <B>getSoTimeout</B>() throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Retrive setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).<DD><DL><DT><B>Returns:</B><DD>the SO_TIMEOUT value<DT><B>Throws:</B><DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs<DT><B>Since: </B><DD>JDK1.1</DD><DT><B>See Also: </B><DD><A HREF="../../java/net/ServerSocket.html#setSoTimeout(int)"><CODE>setSoTimeout(int)</CODE></A></DL></DD></DL><HR><A NAME="toString()"><!-- --></A><H3>toString</H3><PRE>public <A HREF="../../java/lang/String.html">String</A> <B>toString</B>()</PRE><DL><DD>Returns the implementation address and implementation port of this socket as a <code>String</code>.<DD><DL><DT><B>Overrides:</B><DD><CODE><A HREF="../../java/lang/Object.html#toString()">toString</A></CODE> in class <CODE><A HREF="../../java/lang/Object.html">Object</A></CODE></DL></DD><DD><DL><DT><B>Returns:</B><DD>a string representation of this socket.</DL></DD></DL><HR><A NAME="setSocketFactory(java.net.SocketImplFactory)"><!-- --></A><H3>setSocketFactory</H3><PRE>public static void <B>setSocketFactory</B>(<A HREF="../../java/net/SocketImplFactory.html">SocketImplFactory</A> fac) throws <A HREF="../../java/io/IOException.html">IOException</A></PRE><DL><DD>Sets the server socket implementation factory for the application. The factory can be specified only once. <p> When an application creates a new server socket, the socket implementation factory's <code>createSocketImpl</code> method is called to create the actual socket implementation. <p> If there is a security manager, this method first calls the security manager's <code>checkSetFactory</code> method to ensure the operation is allowed. This could result in a SecurityException.<DD><DL><DT><B>Parameters:</B><DD><CODE>fac</CODE> - the desired factory.<DT><B>Throws:</B><DD><CODE><A HREF="../../java/io/IOException.html">IOException</A></CODE> - if an I/O error occurs when setting the socket factory.<DD><CODE><A HREF="../../java/net/SocketException.html">SocketException</A></CODE> - if the factory has already been defined.<DD><CODE><A HREF="../../java/lang/SecurityException.html">SecurityException</A></CODE> - if a security manager exists and its <code>checkSetFactory</code> method doesn't allow the operation.<DT><B>See Also: </B><DD><A HREF="../../java/net/SocketImplFactory.html#createSocketImpl()"><CODE>SocketImplFactory.createSocketImpl()</CODE></A>, <A HREF="../../java/lang/SecurityManager.html#checkSetFactory()"><CODE>SecurityManager.checkSetFactory()</CODE></A></DL></DD></DL><!-- ========= END OF CLASS DATA ========= --><HR><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_bottom"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_bottom_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/ServerSocket.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/net/PasswordAuthentication.html"><B>PREV CLASS</B></A> <A HREF="../../java/net/Socket.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="ServerSocket.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | FIELD | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><font size="-1"><a href="http://java.sun.com/cgi-bin/bugreport.cgi">Submit a bug or feature</a><br>For further API reference and developer documentation, see <a href="http://java.sun.com/products/jdk/1.3/devdocs-vs-specs.html">Java 2 SDK SE Developer Documentation</a>. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. <p>Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.<br>Copyright 1993-2000 Sun Microsystems, Inc. 901 San Antonio Road<br>Palo Alto, California, 94303, U.S.A. All Rights Reserved.</font></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -