home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIPHEFT062001.ISO / browser / nc32lyc / comm.z / java40.jar / java / net / Socket.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-08-15  |  4.4 KB  |  172 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. public class Socket {
  8.    protected SocketImpl impl;
  9.    private static SocketImplFactory factory;
  10.  
  11.    protected Socket() {
  12.       this.initSocketImpl();
  13.    }
  14.  
  15.    protected void setSocketImpl(SocketImpl var1) {
  16.       if (this.impl == null) {
  17.          this.impl = var1;
  18.       }
  19.  
  20.    }
  21.  
  22.    protected void initSocketImpl() {
  23.       this.setSocketImpl(this.createSocketImpl());
  24.    }
  25.  
  26.    protected SocketImpl createSocketImpl() {
  27.       return (SocketImpl)(factory != null ? factory.createSocketImpl() : new PlainSocketImpl());
  28.    }
  29.  
  30.    protected Socket(SocketImpl var1) throws SocketException {
  31.       this.setSocketImpl(var1);
  32.    }
  33.  
  34.    public Socket(String var1, int var2) throws UnknownHostException, IOException {
  35.       this(InetAddress.getByName(var1), var2, (InetAddress)null, 0, true);
  36.    }
  37.  
  38.    public Socket(InetAddress var1, int var2) throws IOException {
  39.       this(var1, var2, (InetAddress)null, 0, true);
  40.    }
  41.  
  42.    public Socket(String var1, int var2, InetAddress var3, int var4) throws IOException {
  43.       this(InetAddress.getByName(var1), var2, var3, var4, true);
  44.    }
  45.  
  46.    public Socket(InetAddress var1, int var2, InetAddress var3, int var4) throws IOException {
  47.       this(var1, var2, var3, var4, true);
  48.    }
  49.  
  50.    public Socket(String var1, int var2, boolean var3) throws IOException {
  51.       this(InetAddress.getByName(var1), var2, (InetAddress)null, 0, var3);
  52.    }
  53.  
  54.    public Socket(InetAddress var1, int var2, boolean var3) throws IOException {
  55.       this(var1, var2, (InetAddress)null, 0, var3);
  56.    }
  57.  
  58.    private Socket(InetAddress var1, int var2, InetAddress var3, int var4, boolean var5) throws IOException {
  59.       this.initSocketImpl();
  60.       if (var2 >= 0 && var2 <= 65535) {
  61.          if (var4 >= 0 && var4 <= 65535) {
  62.             System.getSecurityManager().checkConnect(var1.getHostAddress(), var2);
  63.  
  64.             try {
  65.                this.impl.create(var5);
  66.                if (var3 != null || var4 > 0) {
  67.                   if (var3 == null) {
  68.                      var3 = InetAddress.anyLocalAddress;
  69.                   }
  70.  
  71.                   this.impl.bind(var3, var4);
  72.                }
  73.  
  74.                this.impl.connect(var1, var2);
  75.             } catch (SocketException var7) {
  76.                this.impl.close();
  77.                throw var7;
  78.             }
  79.          } else {
  80.             throw new IllegalArgumentException("port out range:" + var4);
  81.          }
  82.       } else {
  83.          throw new IllegalArgumentException("port out range:" + var2);
  84.       }
  85.    }
  86.  
  87.    public InetAddress getInetAddress() {
  88.       return this.impl.getInetAddress();
  89.    }
  90.  
  91.    public InetAddress getLocalAddress() {
  92.       Object var1 = null;
  93.  
  94.       try {
  95.          var3 = (InetAddress)this.impl.getOption(15);
  96.       } catch (Exception var2) {
  97.          var3 = InetAddress.anyLocalAddress;
  98.       }
  99.  
  100.       return var3;
  101.    }
  102.  
  103.    public int getPort() {
  104.       return this.impl.getPort();
  105.    }
  106.  
  107.    public int getLocalPort() {
  108.       return this.impl.getLocalPort();
  109.    }
  110.  
  111.    public InputStream getInputStream() throws IOException {
  112.       SecurityManager.enablePrivilege("UniversalConnect");
  113.       return this.impl.getInputStream();
  114.    }
  115.  
  116.    public OutputStream getOutputStream() throws IOException {
  117.       SecurityManager.enablePrivilege("UniversalConnect");
  118.       return this.impl.getOutputStream();
  119.    }
  120.  
  121.    public void setTcpNoDelay(boolean var1) throws SocketException {
  122.       this.impl.setOption(1, new Boolean(var1));
  123.    }
  124.  
  125.    public boolean getTcpNoDelay() throws SocketException {
  126.       return (Boolean)this.impl.getOption(1);
  127.    }
  128.  
  129.    public void setSoLinger(boolean var1, int var2) throws SocketException {
  130.       if (!var1) {
  131.          this.impl.setOption(128, new Boolean(var1));
  132.       } else {
  133.          this.impl.setOption(128, new Integer(var2));
  134.       }
  135.    }
  136.  
  137.    public int getSoLinger() throws SocketException {
  138.       Object var1 = this.impl.getOption(128);
  139.       return var1 instanceof Integer ? (Integer)var1 : -1;
  140.    }
  141.  
  142.    public synchronized void setSoTimeout(int var1) throws SocketException {
  143.       this.impl.setOption(4102, new Integer(var1));
  144.    }
  145.  
  146.    public synchronized int getSoTimeout() throws SocketException {
  147.       Object var1 = this.impl.getOption(4102);
  148.       return var1 instanceof Integer ? (Integer)var1 : 0;
  149.    }
  150.  
  151.    public synchronized void close() throws IOException {
  152.       this.impl.close();
  153.    }
  154.  
  155.    public String toString() {
  156.       return "Socket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
  157.    }
  158.  
  159.    public static synchronized void setSocketImplFactory(SocketImplFactory var0) throws IOException {
  160.       if (factory != null) {
  161.          throw new SocketException("factory already defined");
  162.       } else {
  163.          SecurityManager var1 = System.getSecurityManager();
  164.          if (var1 != null) {
  165.             var1.checkSetFactory();
  166.          }
  167.  
  168.          factory = var0;
  169.       }
  170.    }
  171. }
  172.