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

📄 serversocket.java

📁 linux下编程用 编译软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ServerSocket.java -- Class for implementing server side sockets   Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2006   Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.net;import gnu.java.net.PlainSocketImpl;import java.io.IOException;import java.nio.channels.IllegalBlockingModeException;import java.nio.channels.ServerSocketChannel;/* Written using on-line Java Platform 1.2 API Specification. * Status:  I believe all methods are implemented. *//** * This class models server side sockets.  The basic model is that the * server socket is created and bound to some well known port.  It then * listens for and accepts connections.  At that point the client and * server sockets are ready to communicate with one another utilizing * whatever application layer protocol they desire. * * As with the <code>Socket</code> class, most instance methods of this class * simply redirect their calls to an implementation class. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Per Bothner (bothner@cygnus.com) */public class ServerSocket{  /**   * This is the user defined SocketImplFactory, if one is supplied   */  private static SocketImplFactory factory;  /**   * This is the SocketImp object to which most instance methods in this   * class are redirected   */  private SocketImpl impl;  /**   * We need to retain the local address even after the socket is closed.   */  private InetSocketAddress local;  /*   * This constructor is only used by java.nio.   */  // FIXME: Workaround a bug in gcj.  //ServerSocket (PlainSocketImpl impl) throws IOException  ServerSocket(SocketImpl impl) throws IOException  {    if (impl == null)      throw new NullPointerException("impl may not be null");    this.impl = impl;    this.impl.create(true);  }  /*   * This method is only used by java.nio.   */  // FIXME: Workaround a bug in gcj.  //PlainSocketImpl getImpl()  SocketImpl getImpl()  {    return impl;  }  /**   * Constructor that simply sets the implementation.   *   * @exception IOException If an error occurs   *   * @specnote This constructor is public since JDK 1.4   */  public ServerSocket() throws IOException  {    if (factory != null)      impl = factory.createSocketImpl();    else      impl = new PlainSocketImpl();    impl.create(true);  }  /**   * Creates a server socket and binds it to the specified port.  If the   * port number is 0, a random free port will be chosen.  The pending   * connection queue on this socket will be set to 50.   *   * @param port The port number to bind to   *   * @exception IOException If an error occurs   * @exception SecurityException If a security manager exists and its   * checkListen method doesn't allow the operation   */  public ServerSocket(int port) throws IOException  {    this(port, 50);  }  /**   * Creates a server socket and binds it to the specified port.  If the   * port number is 0, a random free port will be chosen.  The pending   * connection queue on this socket will be set to the value passed as   * arg2.   *   * @param port The port number to bind to   * @param backlog The length of the pending connection queue   *   * @exception IOException If an error occurs   * @exception SecurityException If a security manager exists and its   * checkListen method doesn't allow the operation   */  public ServerSocket(int port, int backlog) throws IOException  {    this(port, backlog, null);  }  /**   * Creates a server socket and binds it to the specified port.  If the   * port number is 0, a random free port will be chosen.  The pending   * connection queue on this socket will be set to the value passed as   * backlog.  The third argument specifies a particular local address to   * bind t or null to bind to all local address.   *   * @param port The port number to bind to   * @param backlog The length of the pending connection queue   * @param bindAddr The address to bind to, or null to bind to all addresses   *   * @exception IOException If an error occurs   * @exception SecurityException If a security manager exists and its   * checkListen method doesn't allow the operation   *   * @since 1.1   */  public ServerSocket(int port, int backlog, InetAddress bindAddr)    throws IOException  {    this();    // bind/listen socket    bind(new InetSocketAddress(bindAddr, port), backlog);  }  /**   * Binds the server socket to a specified socket address   *   * @param endpoint The socket address to bind to   *   * @exception IOException If an error occurs   * @exception IllegalArgumentException If address type is not supported   * @exception SecurityException If a security manager exists and its   * checkListen method doesn't allow the operation   *   * @since 1.4   */  public void bind(SocketAddress endpoint) throws IOException  {    bind(endpoint, 50);  }  /**   * Binds the server socket to a specified socket address   *   * @param endpoint The socket address to bind to   * @param backlog The length of the pending connection queue   *   * @exception IOException If an error occurs   * @exception IllegalArgumentException If address type is not supported   * @exception SecurityException If a security manager exists and its   * checkListen method doesn't allow the operation   *   * @since 1.4   */  public void bind(SocketAddress endpoint, int backlog)    throws IOException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");    if (! (endpoint instanceof InetSocketAddress))      throw new IllegalArgumentException("Address type not supported");    InetSocketAddress tmp = (InetSocketAddress) endpoint;    SecurityManager s = System.getSecurityManager();    if (s != null)      s.checkListen(tmp.getPort());    InetAddress addr = tmp.getAddress();    // Initialize addr with 0.0.0.0.    if (addr == null)      addr = InetAddress.ANY_IF;    try      {	impl.bind(addr, tmp.getPort());	impl.listen(backlog);	local = new InetSocketAddress(            (InetAddress) impl.getOption(SocketOptions.SO_BINDADDR),            impl.getLocalPort());      }    catch (IOException exception)      {	close();	throw exception;      }    catch (RuntimeException exception)      {	close();	throw exception;      }    catch (Error error)      {	close();	throw error;      }  }  /**   * This method returns the local address to which this socket is bound   *   * @return The socket's local address   */  public InetAddress getInetAddress()  {    if (local == null)      return null;    return local.getAddress();  }  /**   * This method returns the local port number to which this socket is bound   *   * @return The socket's port number   */  public int getLocalPort()  {    if (local == null)      return -1;    return local.getPort();  }  /**   * Returns the local socket address   *   * @return the local socket address, null if not bound   *    * @since 1.4   */  public SocketAddress getLocalSocketAddress()  {    return local;  }  /**

⌨️ 快捷键说明

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