Socket.java 7.21 KB
Newer Older
Tom Tromey committed
1 2
// Socket.java

3
/* Copyright (C) 1999  Free Software Foundation
Tom Tromey committed
4 5 6 7 8 9 10 11 12 13 14 15 16

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

/**
  * @author Per Bothner <bothner@cygnus.com>
  * @date January 6, 1999.
  */

/** Written using on-line Java Platform 1.2 API Specification.
Warren Levy committed
17
  * Status:  I believe all methods are implemented.
Tom Tromey committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  */

package java.net;
import java.io.*;

public class Socket
{
  static SocketImplFactory factory;
  SocketImpl impl;

  protected Socket ()
  {
  }

  protected Socket (SocketImpl impl) throws SocketException
  {
    this.impl = impl;
  }

  public Socket (String host, int port)
    throws UnknownHostException, IOException
  {
    this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkConnect(host, port);
    impl.create(true);
45 46 47
    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
Tom Tromey committed
48 49 50 51 52 53 54 55 56 57 58
    impl.connect(host, port);
  }

  public Socket (InetAddress address, int port)
    throws IOException 
  {
    this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkConnect(address.getHostName(), port);
    impl.create(true);
59 60 61
    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
Tom Tromey committed
62 63 64 65 66 67 68 69 70 71 72
    impl.connect(address, port);
  }

  public Socket (String host, int port,
		 InetAddress localAddr, int localPort) throws IOException
  {
    this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkConnect(host, port);
    impl.create(true);
73
    // FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
Tom Tromey committed
74 75 76 77 78 79 80 81 82 83 84 85
    impl.bind(localAddr, localPort);
    impl.connect(host, port);
  }

  public Socket (InetAddress address, int port,
		 InetAddress localAddr, int localPort) throws IOException
  {
    this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkConnect(address.getHostName(), port);
    impl.create(true);
86
    // FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
Tom Tromey committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    impl.bind(localAddr, localPort);
    impl.connect(address, port);
  }

  /**
   * @deprecated Use DatagramSocket instead for UDP transport.
   */
  public Socket (String host, int port, boolean stream) throws IOException
  {
    impl = factory == null ? new PlainSocketImpl()
      : factory.createSocketImpl();
    impl.create(stream);
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkConnect(host, port);
102 103 104
    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
Tom Tromey committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    impl.connect(host, port);
  }

  /**
   * @deprecated Use DatagramSocket instead for UDP transport.
   */
  public Socket (InetAddress host, int port, boolean stream) throws IOException
  {
    impl = factory == null ? new PlainSocketImpl()
      : factory.createSocketImpl();
    impl.create(stream);
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkConnect(host.getHostName(), port);
119 120 121
    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
Tom Tromey committed
122 123 124 125 126 127 128 129 130 131
    impl.connect(host, port);
  }

  public InetAddress getInetAddress ()
  {
    return impl.getInetAddress();
  }

  public InetAddress getLocalAddress ()
  {
132
    // FIXME: see note in DatagramSocket.java about checkConnect() and security
Warren Levy committed
133 134
    try
      {
135
	return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
Warren Levy committed
136 137 138 139 140 141 142
      }
    catch (SocketException x)
      {
	// (hopefully) shouldn't happen
	System.err.println(x);
        throw new java.lang.InternalError("Error in PlainSocketImpl.getOption");
      }
Tom Tromey committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  }

  public int getPort ()
  {
    return impl.getPort();
  }

  public int getLocalPort ()
  {
    return impl.getLocalPort();
  }

  public InputStream getInputStream () throws IOException
  {
    return impl.getInputStream();
  }

  public OutputStream getOutputStream () throws IOException
  {
    return impl.getOutputStream();
  }

  public void setTcpNoDelay (boolean on)  throws SocketException
  {
Warren Levy committed
167
    impl.setOption( SocketOptions.TCP_NODELAY, new Boolean(on) );
Tom Tromey committed
168 169 170 171
  }

  public boolean getTcpNoDelay() throws SocketException
  {
Warren Levy committed
172 173
    Boolean bool = (Boolean)impl.getOption( SocketOptions.TCP_NODELAY );
    return bool.booleanValue();
Tom Tromey committed
174 175 176 177
  }

  public void setSoLinger(boolean on, int linger) throws SocketException
  {
Warren Levy committed
178 179 180 181 182 183 184 185 186 187
    if ( on && (linger >= 0) ) 
      {
	if (linger > 65535)
	  linger = 65535;
	impl.setOption( SocketOptions.SO_LINGER, new Integer(linger) );
      } 
    else if ( on && (linger < 0) ) 
      throw new IllegalArgumentException("SO_LINGER must be >= 0");
    else
      impl.setOption( SocketOptions.SO_LINGER, new Boolean(false) );
Tom Tromey committed
188 189
  }

190
  public int getSoLinger() throws SocketException
Tom Tromey committed
191
  {
Warren Levy committed
192 193 194 195 196
    Object linger = impl.getOption(SocketOptions.SO_LINGER);    
    if (linger instanceof Integer) 
      return ((Integer)linger).intValue();
    else
      return -1;
Tom Tromey committed
197 198
  }

Warren Levy committed
199
  public synchronized void setSoTimeout (int timeout) throws SocketException
Tom Tromey committed
200
  {
Warren Levy committed
201 202 203 204
    if (timeout < 0)
      throw new IllegalArgumentException("Invalid timeout: " + timeout);

    impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
Tom Tromey committed
205 206
  }

Warren Levy committed
207
  public synchronized int getSoTimeout () throws SocketException
Tom Tromey committed
208
  {
Warren Levy committed
209 210 211 212 213
    Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
    if (timeout instanceof Integer) 
      return ((Integer)timeout).intValue();
    else
      return 0;
Tom Tromey committed
214 215
  }

Warren Levy committed
216
  // JDK1.2
Tom Tromey committed
217 218
  public void setSendBufferSize (int size) throws SocketException
  {
Warren Levy committed
219 220 221 222
    if (size <= 0)
      throw new IllegalArgumentException("Invalid buffer size: " + size);

    impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
Tom Tromey committed
223 224
  }

Warren Levy committed
225
  // JDK1.2
Tom Tromey committed
226 227
  public int getSendBufferSize () throws SocketException
  {
Warren Levy committed
228 229
    Integer buf = (Integer)impl.getOption(SocketOptions.SO_SNDBUF);
    return buf.intValue();
Tom Tromey committed
230 231
  }

Warren Levy committed
232
  // JDK1.2
Tom Tromey committed
233 234
  public void setReceiveBufferSize (int size) throws SocketException
  {
Warren Levy committed
235 236 237 238
    if (size <= 0)
      throw new IllegalArgumentException("Invalid buffer size: " + size);

    impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
Tom Tromey committed
239 240
  }

Warren Levy committed
241
  // JDK1.2
Tom Tromey committed
242 243
  public int getReceiveBufferSize () throws SocketException
  {
Warren Levy committed
244 245
    Integer buf = (Integer)impl.getOption(SocketOptions.SO_RCVBUF);
    return buf.intValue();
Tom Tromey committed
246 247
  }

Warren Levy committed
248
  public synchronized void close ()  throws IOException
Tom Tromey committed
249 250 251 252 253 254
  {
    impl.close();
  }

  public String toString ()
  {
Warren Levy committed
255
    return "Socket" + impl.toString();
Tom Tromey committed
256 257
  }

Warren Levy committed
258
  public static synchronized void setSocketImplFactory (SocketImplFactory fac)
Tom Tromey committed
259 260 261 262 263
    throws IOException
  {
    factory = fac;
  }
}