home *** CD-ROM | disk | FTP | other *** search
- package java.net;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- public final class Socket {
- SocketImpl impl;
- private static SocketImplFactory factory;
-
- Socket() {
- this.impl = (SocketImpl)(factory != null ? factory.createSocketImpl() : new PlainSocketImpl());
- }
-
- public Socket(String host, int port) throws UnknownHostException, IOException {
- this(host, port, true);
- }
-
- public Socket(String host, int port, boolean stream) throws IOException {
- this(InetAddress.getByName(host), port, stream);
- }
-
- public Socket(InetAddress address, int port) throws IOException {
- this(address, port, true);
- }
-
- public Socket(InetAddress address, int port, boolean stream) throws IOException {
- this();
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkConnect(address.getHostAddress(), port);
- }
-
- try {
- this.impl.create(stream);
- this.impl.connect(address, port);
- } catch (SocketException e) {
- this.impl.close();
- throw e;
- }
- }
-
- public InetAddress getInetAddress() {
- return this.impl.getInetAddress();
- }
-
- public int getPort() {
- return this.impl.getPort();
- }
-
- public int getLocalPort() {
- return this.impl.getLocalPort();
- }
-
- public InputStream getInputStream() throws IOException {
- return this.impl.getInputStream();
- }
-
- public OutputStream getOutputStream() throws IOException {
- return this.impl.getOutputStream();
- }
-
- public synchronized void close() throws IOException {
- this.impl.close();
- }
-
- public String toString() {
- return "Socket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
- }
-
- public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException {
- if (factory != null) {
- throw new SocketException("factory already defined");
- } else {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkSetFactory();
- }
-
- factory = fac;
- }
- }
- }
-