1999-04-07 22:42:40 +08:00
|
|
|
// Socket.java
|
|
|
|
|
2000-03-08 03:55:28 +08:00
|
|
|
/* Copyright (C) 1999 Free Software Foundation
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
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.
|
1999-05-27 01:00:06 +08:00
|
|
|
* Status: I believe all methods are implemented.
|
1999-04-07 22:42:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
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);
|
1999-05-29 03:29:53 +08:00
|
|
|
// 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.
|
1999-04-07 22:42:40 +08:00
|
|
|
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);
|
1999-05-29 03:29:53 +08:00
|
|
|
// 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.
|
1999-04-07 22:42:40 +08:00
|
|
|
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);
|
1999-05-29 03:29:53 +08:00
|
|
|
// FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
|
1999-04-07 22:42:40 +08:00
|
|
|
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);
|
1999-05-29 03:29:53 +08:00
|
|
|
// FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
|
1999-04-07 22:42:40 +08:00
|
|
|
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);
|
1999-05-29 03:29:53 +08:00
|
|
|
// 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.
|
1999-04-07 22:42:40 +08:00
|
|
|
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);
|
1999-05-29 03:29:53 +08:00
|
|
|
// 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.
|
1999-04-07 22:42:40 +08:00
|
|
|
impl.connect(host, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
public InetAddress getInetAddress ()
|
|
|
|
{
|
|
|
|
return impl.getInetAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
public InetAddress getLocalAddress ()
|
|
|
|
{
|
1999-06-17 08:21:26 +08:00
|
|
|
// FIXME: see note in DatagramSocket.java about checkConnect() and security
|
1999-05-27 01:00:06 +08:00
|
|
|
try
|
|
|
|
{
|
1999-06-17 08:21:26 +08:00
|
|
|
return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
|
1999-05-27 01:00:06 +08:00
|
|
|
}
|
|
|
|
catch (SocketException x)
|
|
|
|
{
|
|
|
|
// (hopefully) shouldn't happen
|
|
|
|
System.err.println(x);
|
|
|
|
throw new java.lang.InternalError("Error in PlainSocketImpl.getOption");
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
impl.setOption( SocketOptions.TCP_NODELAY, new Boolean(on) );
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getTcpNoDelay() throws SocketException
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
Boolean bool = (Boolean)impl.getOption( SocketOptions.TCP_NODELAY );
|
|
|
|
return bool.booleanValue();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setSoLinger(boolean on, int linger) throws SocketException
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
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) );
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-04-24 00:36:22 +08:00
|
|
|
public int getSoLinger() throws SocketException
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
Object linger = impl.getOption(SocketOptions.SO_LINGER);
|
|
|
|
if (linger instanceof Integer)
|
|
|
|
return ((Integer)linger).intValue();
|
|
|
|
else
|
|
|
|
return -1;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
public synchronized void setSoTimeout (int timeout) throws SocketException
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
if (timeout < 0)
|
|
|
|
throw new IllegalArgumentException("Invalid timeout: " + timeout);
|
|
|
|
|
|
|
|
impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
public synchronized int getSoTimeout () throws SocketException
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
|
|
|
|
if (timeout instanceof Integer)
|
|
|
|
return ((Integer)timeout).intValue();
|
|
|
|
else
|
|
|
|
return 0;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
// JDK1.2
|
1999-04-07 22:42:40 +08:00
|
|
|
public void setSendBufferSize (int size) throws SocketException
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
if (size <= 0)
|
|
|
|
throw new IllegalArgumentException("Invalid buffer size: " + size);
|
|
|
|
|
|
|
|
impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
// JDK1.2
|
1999-04-07 22:42:40 +08:00
|
|
|
public int getSendBufferSize () throws SocketException
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
Integer buf = (Integer)impl.getOption(SocketOptions.SO_SNDBUF);
|
|
|
|
return buf.intValue();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
// JDK1.2
|
1999-04-07 22:42:40 +08:00
|
|
|
public void setReceiveBufferSize (int size) throws SocketException
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
if (size <= 0)
|
|
|
|
throw new IllegalArgumentException("Invalid buffer size: " + size);
|
|
|
|
|
|
|
|
impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
// JDK1.2
|
1999-04-07 22:42:40 +08:00
|
|
|
public int getReceiveBufferSize () throws SocketException
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
Integer buf = (Integer)impl.getOption(SocketOptions.SO_RCVBUF);
|
|
|
|
return buf.intValue();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
public synchronized void close () throws IOException
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
|
|
|
impl.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString ()
|
|
|
|
{
|
1999-05-27 01:00:06 +08:00
|
|
|
return "Socket" + impl.toString();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-05-27 01:00:06 +08:00
|
|
|
public static synchronized void setSocketImplFactory (SocketImplFactory fac)
|
1999-04-07 22:42:40 +08:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
factory = fac;
|
|
|
|
}
|
|
|
|
}
|