Java中ServerSocket源码
来源:互联网 发布:ubuntu部署openstack 编辑:程序博客网 时间:2024/06/18 15:06
Java中ServerSocket源码
publicclass ServerSocket implements java.io.Closeable { private boolean created = false; private boolean bound = false; private boolean closed = false; private Object closeLock = new Object(); private SocketImpl impl; private boolean oldImpl = false; ServerSocket(SocketImpl impl) { this.impl = impl; impl.setServerSocket(this); } public ServerSocket() throws IOException { setImpl(); } public ServerSocket(int port) throws IOException { this(port, 50, null); } public ServerSocket(int port, int backlog) throws IOException { this(port, backlog, null); } public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { setImpl(); if (port < 0 || port > 0xFFFF) throw new IllegalArgumentException( "Port value out of range: " + port); if (backlog < 1) backlog = 50; try { bind(new InetSocketAddress(bindAddr, port), backlog); } catch(SecurityException e) { close(); throw e; } catch(IOException e) { close(); throw e; } } SocketImpl getImpl() throws SocketException { if (!created) createImpl(); return impl; } private void checkOldImpl() { if (impl == null) return; // SocketImpl.connect() is a protected method, therefore we need to use // getDeclaredMethod, therefore we need permission to access the member try { AccessController.doPrivileged( new PrivilegedExceptionAction<Void>() { public Void run() throws NoSuchMethodException { Class[] cl = new Class[2]; cl[0] = SocketAddress.class; cl[1] = Integer.TYPE; impl.getClass().getDeclaredMethod("connect", cl); return null; } }); } catch (java.security.PrivilegedActionException e) { oldImpl = true; } } private void setImpl() { if (factory != null) { impl = factory.createSocketImpl(); checkOldImpl(); } else { // No need to do a checkOldImpl() here, we know it's an up to date // SocketImpl! impl = new SocksSocketImpl(); } if (impl != null) impl.setServerSocket(this); } void createImpl() throws SocketException { if (impl == null) setImpl(); try { impl.create(true); created = true; } catch (IOException e) { throw new SocketException(e.getMessage()); } } public void bind(SocketAddress endpoint) throws IOException { bind(endpoint, 50); } public void bind(SocketAddress endpoint, int backlog) throws IOException { if (isClosed()) throw new SocketException("Socket is closed"); if (!oldImpl && isBound()) throw new SocketException("Already bound"); if (endpoint == null) endpoint = new InetSocketAddress(0); if (!(endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException("Unsupported address type"); InetSocketAddress epoint = (InetSocketAddress) endpoint; if (epoint.isUnresolved()) throw new SocketException("Unresolved address"); if (backlog < 1) backlog = 50; try { SecurityManager security = System.getSecurityManager(); if (security != null) security.checkListen(epoint.getPort()); getImpl().bind(epoint.getAddress(), epoint.getPort()); getImpl().listen(backlog); bound = true; } catch(SecurityException e) { bound = false; throw e; } catch(IOException e) { bound = false; throw e; } } public InetAddress getInetAddress() { if (!isBound()) return null; try { InetAddress in = getImpl().getInetAddress(); if (!NetUtil.doRevealLocalAddress()) { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(in.getHostAddress(), -1); } return in; } catch (SecurityException e) { return InetAddress.getLoopbackAddress(); } catch (SocketException e) { // nothing // If we're bound, the impl has been created // so we shouldn't get here } return null; } public int getLocalPort() { if (!isBound()) return -1; try { return getImpl().getLocalPort(); } catch (SocketException e) { // nothing // If we're bound, the impl has been created // so we shouldn't get here } return -1; } public SocketAddress getLocalSocketAddress() { if (!isBound()) return null; return new InetSocketAddress(getInetAddress(), getLocalPort()); } public Socket accept() throws IOException { if (isClosed()) throw new SocketException("Socket is closed"); if (!isBound()) throw new SocketException("Socket is not bound yet"); Socket s = new Socket((SocketImpl) null); implAccept(s); return s; } protected final void implAccept(Socket s) throws IOException { SocketImpl si = null; try { if (s.impl == null) s.setImpl(); else { s.impl.reset(); } si = s.impl; s.impl = null; si.address = new InetAddress(); si.fd = new FileDescriptor(); getImpl().accept(si); SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkAccept(si.getInetAddress().getHostAddress(), si.getPort()); } } catch (IOException e) { if (si != null) si.reset(); s.impl = si; throw e; } catch (SecurityException e) { if (si != null) si.reset(); s.impl = si; throw e; } s.impl = si; s.postAccept(); } public void close() throws IOException { synchronized(closeLock) { if (isClosed()) return; if (created) impl.close(); closed = true; } } public ServerSocketChannel getChannel() { return null; } public boolean isBound() { // Before 1.3 ServerSockets were always bound during creation return bound || oldImpl; } public boolean isClosed() { synchronized(closeLock) { return closed; } } public synchronized void setSoTimeout(int timeout) throws SocketException { if (isClosed()) throw new SocketException("Socket is closed"); getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout)); } public synchronized int getSoTimeout() throws IOException { if (isClosed()) throw new SocketException("Socket is closed"); Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); /* extra type safety */ if (o instanceof Integer) { return ((Integer) o).intValue(); } else { return 0; } } public void setReuseAddress(boolean on) throws SocketException { if (isClosed()) throw new SocketException("Socket is closed"); getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); } public boolean getReuseAddress() throws SocketException { if (isClosed()) throw new SocketException("Socket is closed"); return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); } public String toString() { if (!isBound()) return "ServerSocket[unbound]"; InetAddress in; if (!NetUtil.doRevealLocalAddress() && System.getSecurityManager() != null) { in = InetAddress.getLoopbackAddress(); } else { in = impl.getInetAddress(); } return "ServerSocket[addr=" + in + ",localport=" + impl.getLocalPort() + "]"; } void setBound() { bound = true; } void setCreated() { created = true; } private static SocketImplFactory factory = null; public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException { if (factory != null) { throw new SocketException("factory already defined"); } SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkSetFactory(); } factory = fac; } public synchronized void setReceiveBufferSize (int size) throws SocketException { if (!(size > 0)) { throw new IllegalArgumentException("negative receive size"); } if (isClosed()) throw new SocketException("Socket is closed"); getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size)); } public synchronized int getReceiveBufferSize() throws SocketException{ if (isClosed()) throw new SocketException("Socket is closed"); int result = 0; Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); if (o instanceof Integer) { result = ((Integer)o).intValue(); } return result; } public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { /* Not implemented yet */ }}
0 0
- Java中ServerSocket源码
- java socket/Serversocket编程详解(中/英文)
- java socket/Serversocket编程详解(中/英)
- java socket/Serversocket编程详解(中/英文)
- Java中ServerSocket与Socket的区别
- Java 中ServerSocket与Socket 通信的简单示例
- java中ServerSocket和Socket的简单用法
- 拿来主义:java中socket和serversocket 的用法
- java中ServerSocket 与 Socket的用法和区别
- java中给出一个多线程TCP的ServerSocket例子?
- java中请给出TCP的ServerSocket通信的例子?
- java.net.ServerSocket翻译
- 读ServerSocket.java心得
- java ServerSocket API 详解
- java.net 类 ServerSocket
- java.net.ServerSocket详解
- java.net.ServerSocket
- Java Socket和ServerSocket
- BZOJ1176: [Balkan2007]Mokia CDQ分治
- 利用MySQL建立本地数据库
- 编码方法
- Java分层
- javascript使用image.height和image.width获取图片宽高值为0,获取失败的原因
- Java中ServerSocket源码
- PHP开发小技巧⑥—判断是否是移动端访问
- 伯克利大学“机器学习(Practical Machine Learning)"课件及相关资料
- MyEclipse Servers视窗出现“Could not create the view: An unexpected exception was thrown”错误解决办法
- php常见的sql语句
- 润乾集算报表通过API修改单元格属性
- 一个读取INI配置文件的类
- Hbase概述
- 1031: Mincost