📄 serversocket.java
字号:
/* * Java core library component. * * Copyright (c) 1997, 1998 * Transvirtual Technologies, Inc. All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. */package java.net;import java.io.IOException;import kaffe.net.DefaultSocketImplFactory;public class ServerSocket {private static SocketImplFactory factory = new DefaultSocketImplFactory();private SocketImpl impl;public ServerSocket(int port) throws IOException { this(port, 50);}public ServerSocket(int port, int backlog) throws IOException { this(port, backlog, null);}/** * Create a server with the specified port, listen backlog, and local * IP address to bind to. The bindAddr argument can be used on a * multi-homed host for a ServerSocket that will only accept connect * requests to one of its addresses. If bindAddr is null, it will default * accepting connections on any/all local addresses. The port must be * between 0 and 65535, inclusive. * * @param port the local TCP port * @param backlog the listen backlog * @param bindAddr the local InetAddress the server will bind to */public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { if (port < 0 || port > 65535) throw new IllegalArgumentException(); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkListen(port); if (bindAddr == null) { bindAddr = InetAddress.getAnyAddress(); } impl = factory.createSocketImpl(); try { impl.create(true); impl.bind(bindAddr, port); impl.listen(backlog); } catch (IOException ioe) { try { impl.close(); } catch (IOException _) { } throw ioe; }}public Socket accept() throws IOException { Socket s = new Socket(); s.impl.localport = impl.localport; implAccept(s); return (s);}protected final void implAccept(Socket s) throws IOException { impl.accept(s.impl); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkAccept(s.getInetAddress().getHostName(), s.getPort());}public void close() throws IOException { impl.close();}public InetAddress getInetAddress() { try { return ((InetAddress)impl.getOption(SocketOptions.SO_BINDADDR)); } catch (SocketException e1) { try { return (InetAddress.getLocalHost()); } catch (UnknownHostException e2) { return (InetAddress.getLoopback()); } }}public int getLocalPort() { return (impl.getLocalPort());}public synchronized int getSoTimeout() throws IOException { return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();}public synchronized void setSoTimeout(int timeout) throws SocketException { impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));}public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException { SecurityManager sm = System.getSecurityManager(); if( sm != null ) sm.checkSetFactory(); factory = fac;}public SocketImpl newSocketImpl() { return (impl);}public String toString() { return (impl.toString());}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -