home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / net / Socket.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  3.1 KB  |  84 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. public final class Socket {
  8.    SocketImpl impl;
  9.    private static SocketImplFactory factory;
  10.  
  11.    Socket() {
  12.       this.impl = (SocketImpl)(factory != null ? factory.createSocketImpl() : new PlainSocketImpl());
  13.    }
  14.  
  15.    public Socket(String host, int port) throws UnknownHostException, IOException {
  16.       this(host, port, true);
  17.    }
  18.  
  19.    public Socket(String host, int port, boolean stream) throws IOException {
  20.       this(InetAddress.getByName(host), port, stream);
  21.    }
  22.  
  23.    public Socket(InetAddress address, int port) throws IOException {
  24.       this(address, port, true);
  25.    }
  26.  
  27.    public Socket(InetAddress address, int port, boolean stream) throws IOException {
  28.       this();
  29.       SecurityManager security = System.getSecurityManager();
  30.       if (security != null) {
  31.          security.checkConnect(address.getHostAddress(), port);
  32.       }
  33.  
  34.       try {
  35.          this.impl.create(stream);
  36.          this.impl.connect(address, port);
  37.       } catch (SocketException e) {
  38.          this.impl.close();
  39.          throw e;
  40.       }
  41.    }
  42.  
  43.    public InetAddress getInetAddress() {
  44.       return this.impl.getInetAddress();
  45.    }
  46.  
  47.    public int getPort() {
  48.       return this.impl.getPort();
  49.    }
  50.  
  51.    public int getLocalPort() {
  52.       return this.impl.getLocalPort();
  53.    }
  54.  
  55.    public InputStream getInputStream() throws IOException {
  56.       return this.impl.getInputStream();
  57.    }
  58.  
  59.    public OutputStream getOutputStream() throws IOException {
  60.       return this.impl.getOutputStream();
  61.    }
  62.  
  63.    public synchronized void close() throws IOException {
  64.       this.impl.close();
  65.    }
  66.  
  67.    public String toString() {
  68.       return "Socket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
  69.    }
  70.  
  71.    public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException {
  72.       if (factory != null) {
  73.          throw new SocketException("factory already defined");
  74.       } else {
  75.          SecurityManager security = System.getSecurityManager();
  76.          if (security != null) {
  77.             security.checkSetFactory();
  78.          }
  79.  
  80.          factory = fac;
  81.       }
  82.    }
  83. }
  84.