2003-03-20 09:19:58 +01:00
|
|
|
/* PlainSocketImpl.java -- Default socket implementation
|
2005-02-17 19:59:55 +01:00
|
|
|
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
|
2003-10-21 14:29:43 +02:00
|
|
|
Free Software Foundation, Inc.
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2003-03-20 09:19:58 +01:00
|
|
|
This file is part of GNU Classpath.
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2003-03-20 09:19:58 +01:00
|
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
2005-06-30 05:22:09 +02:00
|
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA.
|
2003-03-20 09:19:58 +01:00
|
|
|
|
|
|
|
Linking this library statically or dynamically with other modules is
|
|
|
|
making a combined work based on this library. Thus, the terms and
|
|
|
|
conditions of the GNU General Public License cover the whole
|
|
|
|
combination.
|
|
|
|
|
|
|
|
As a special exception, the copyright holders of this library give you
|
|
|
|
permission to link this library with independent modules to produce an
|
|
|
|
executable, regardless of the license terms of these independent
|
|
|
|
modules, and to copy and distribute the resulting executable under
|
|
|
|
terms of your choice, provided that you also meet, for each linked
|
|
|
|
independent module, the terms and conditions of the license of that
|
|
|
|
module. An independent module is a module which is not derived from
|
|
|
|
or based on this library. If you modify this library, you may extend
|
|
|
|
this exception to your version of the library, but you are not
|
|
|
|
obligated to do so. If you do not wish to do so, delete this
|
|
|
|
exception statement from your version. */
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
|
DatagramSocket.java, [...]: Use gnu.java.net.Plain*SocketImpl instead of java.net.PlainSocketImpl.
2003-09-10 Michael Koch <konqueror@gmx.de>
* java/net/DatagramSocket.java,
java/net/MulticastSocket.java,
java/net/ServerSocket.java,
java/net/Socket.java:
Use gnu.java.net.Plain*SocketImpl instead of
java.net.PlainSocketImpl.
* java/net/PlainDatagramSocketImpl.java,
java/net/PlainSocketImpl.java,
java/net/SocketInputStream.java,
java/net/SocketOutputStream.java,
java/net/natPlainDatagramSocketImplNoNet.cc,
java/net/natPlainDatagramSocketImplPosix.cc,
java/net/natPlainDatagramSocketImplWin32.cc,
java/net/natPlainSocketImplNoNet.cc,
java/net/natPlainSocketImplPosix.cc,
java/net/natPlainSocketImplWin32.cc:
Removed.
* gnu/java/net/PlainDatagramSocketImpl.java,
gnu/java/net/PlainSocketImpl.java,
gnu/java/net/SocketInputStream.java,
gnu/java/net/SocketOutputStream.java,
gnu/java/net/natPlainDatagramSocketImplNoNet.cc,
gnu/java/net/natPlainDatagramSocketImplPosix.cc,
gnu/java/net/natPlainDatagramSocketImplWin32.cc,
gnu/java/net/natPlainSocketImplNoNet.cc,
gnu/java/net/natPlainSocketImplPosix.cc,
gnu/java/net/natPlainSocketImplWin32.cc:
New files (moved from java/net).
* configure.in: Create links for gnu/java/net/natPlain*SocketImpl.cc
instead of java/net/natPlain*SocketImpl.cc.
* configure: Regenerated.
* Makefile.am: Moved files from java/net to gnu/java/net.
* Makefile.in: Regenerated.
From-SVN: r71274
2003-09-10 19:39:20 +02:00
|
|
|
package gnu.java.net;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2005-04-20 07:58:46 +02:00
|
|
|
import gnu.classpath.Configuration;
|
|
|
|
|
2002-11-18 14:22:55 +01:00
|
|
|
import java.io.IOException;
|
2005-04-20 07:58:46 +02:00
|
|
|
import java.io.InputStream;
|
2002-11-18 15:55:54 +01:00
|
|
|
import java.io.OutputStream;
|
DatagramSocket.java, [...]: Use gnu.java.net.Plain*SocketImpl instead of java.net.PlainSocketImpl.
2003-09-10 Michael Koch <konqueror@gmx.de>
* java/net/DatagramSocket.java,
java/net/MulticastSocket.java,
java/net/ServerSocket.java,
java/net/Socket.java:
Use gnu.java.net.Plain*SocketImpl instead of
java.net.PlainSocketImpl.
* java/net/PlainDatagramSocketImpl.java,
java/net/PlainSocketImpl.java,
java/net/SocketInputStream.java,
java/net/SocketOutputStream.java,
java/net/natPlainDatagramSocketImplNoNet.cc,
java/net/natPlainDatagramSocketImplPosix.cc,
java/net/natPlainDatagramSocketImplWin32.cc,
java/net/natPlainSocketImplNoNet.cc,
java/net/natPlainSocketImplPosix.cc,
java/net/natPlainSocketImplWin32.cc:
Removed.
* gnu/java/net/PlainDatagramSocketImpl.java,
gnu/java/net/PlainSocketImpl.java,
gnu/java/net/SocketInputStream.java,
gnu/java/net/SocketOutputStream.java,
gnu/java/net/natPlainDatagramSocketImplNoNet.cc,
gnu/java/net/natPlainDatagramSocketImplPosix.cc,
gnu/java/net/natPlainDatagramSocketImplWin32.cc,
gnu/java/net/natPlainSocketImplNoNet.cc,
gnu/java/net/natPlainSocketImplPosix.cc,
gnu/java/net/natPlainSocketImplWin32.cc:
New files (moved from java/net).
* configure.in: Create links for gnu/java/net/natPlain*SocketImpl.cc
instead of java/net/natPlain*SocketImpl.cc.
* configure: Regenerated.
* Makefile.am: Moved files from java/net to gnu/java/net.
* Makefile.in: Regenerated.
From-SVN: r71274
2003-09-10 19:39:20 +02:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.SocketAddress;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.net.SocketImpl;
|
|
|
|
import java.net.SocketOptions;
|
1999-05-12 03:51:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Written using on-line Java Platform 1.2 API Specification, as well
|
|
|
|
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|
|
|
* Status: Believed complete and correct.
|
2003-06-21 15:24:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2003-06-21 16:02:10 +02:00
|
|
|
* Unless the application installs its own SocketImplFactory, this is the
|
|
|
|
* default socket implemetation that will be used. It simply uses a
|
|
|
|
* combination of Java and native routines to implement standard BSD
|
|
|
|
* style sockets of family AF_INET and types SOCK_STREAM and SOCK_DGRAM
|
|
|
|
*
|
2005-04-20 07:58:46 +02:00
|
|
|
* @author Per Bothner (bothner@cygnus.com)
|
|
|
|
* @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
|
|
|
|
* @author Aaron M. Renn (arenn@urbanophile.com)
|
1999-05-12 03:51:23 +02:00
|
|
|
*/
|
DatagramSocket.java, [...]: Use gnu.java.net.Plain*SocketImpl instead of java.net.PlainSocketImpl.
2003-09-10 Michael Koch <konqueror@gmx.de>
* java/net/DatagramSocket.java,
java/net/MulticastSocket.java,
java/net/ServerSocket.java,
java/net/Socket.java:
Use gnu.java.net.Plain*SocketImpl instead of
java.net.PlainSocketImpl.
* java/net/PlainDatagramSocketImpl.java,
java/net/PlainSocketImpl.java,
java/net/SocketInputStream.java,
java/net/SocketOutputStream.java,
java/net/natPlainDatagramSocketImplNoNet.cc,
java/net/natPlainDatagramSocketImplPosix.cc,
java/net/natPlainDatagramSocketImplWin32.cc,
java/net/natPlainSocketImplNoNet.cc,
java/net/natPlainSocketImplPosix.cc,
java/net/natPlainSocketImplWin32.cc:
Removed.
* gnu/java/net/PlainDatagramSocketImpl.java,
gnu/java/net/PlainSocketImpl.java,
gnu/java/net/SocketInputStream.java,
gnu/java/net/SocketOutputStream.java,
gnu/java/net/natPlainDatagramSocketImplNoNet.cc,
gnu/java/net/natPlainDatagramSocketImplPosix.cc,
gnu/java/net/natPlainDatagramSocketImplWin32.cc,
gnu/java/net/natPlainSocketImplNoNet.cc,
gnu/java/net/natPlainSocketImplPosix.cc,
gnu/java/net/natPlainSocketImplWin32.cc:
New files (moved from java/net).
* configure.in: Create links for gnu/java/net/natPlain*SocketImpl.cc
instead of java/net/natPlain*SocketImpl.cc.
* configure: Regenerated.
* Makefile.am: Moved files from java/net to gnu/java/net.
* Makefile.in: Regenerated.
From-SVN: r71274
2003-09-10 19:39:20 +02:00
|
|
|
public final class PlainSocketImpl extends SocketImpl
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2003-06-21 15:24:13 +02:00
|
|
|
// Static initializer to load native library.
|
|
|
|
static
|
|
|
|
{
|
|
|
|
if (Configuration.INIT_LOAD_LIBRARY)
|
|
|
|
{
|
|
|
|
System.loadLibrary("javanet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-26 19:00:06 +02:00
|
|
|
// These fields are mirrored for use in native code to avoid cpp conflicts
|
|
|
|
// when the #defines in system header files are the same as the public fields.
|
|
|
|
static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
|
|
|
|
_Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
|
|
|
|
_Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
|
2002-09-04 Michael Koch <konqueror@gmx.de>
* java/net/DatagramSocket.java
(DatagramSocket): Added documentation.
(close): Likewise.
(getLocalAddress): Likewise.
(getLocalPort): Likewise.
(receive): Likewise.
(send): Likewise.
(setSoTimeout): Likewise.
(connect): New method.
(disconnect): New method.
(getInetAddress): New method (FIXME)
(getPort): New method.
(setReuseAddress): New method.
(getReuseAddress): New method.
(setBroadcast): New method.
(getBroadcast): New method.
(setTrafficClass): New method.
(getTrafficClass): New method.
* java/net/MulticastSocket.java):
(getTTL): Added @see in documentation.
(setTTL): Added @see in documentation.
(setLoopbackMode): New method.
(getLoopbackMode): New method.
* java/net/PlainSocketImpl.java:
Added new constants for the options SO_BROADCAST, SO_OOBINLINE,
IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS
* java/net/PlainDatagramSocketImpl.java
Added new constants for the options SO_BROADCAST, SO_OOBINLINE,
IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS
* java/net/natPlainSocketImpl.cc
(getOption): Implemented the options SO_BROADCAST, SO_OOBINLINE,
IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS
(setOption): Implemented the options SO_BROADCAST, SO_OOBINLINE,
IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS
This should also fix SO_KEEPALIVE
* java/net/natPlainDatagramSocketImpl.cc
(getOption): Implemented the options SO_BROADCAST, SO_OOBINLINE,
IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS
(setOption): Implemented the options SO_BROADCAST, SO_OOBINLINE,
IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS
From-SVN: r56801
2002-09-04 19:35:22 +02:00
|
|
|
_Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
|
|
|
|
_Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
|
|
|
|
_Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
|
|
|
|
_Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
|
|
|
|
_Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
|
|
|
|
_Jv_IP_TOS_ = SocketOptions.IP_TOS,
|
1999-05-26 19:00:06 +02:00
|
|
|
_Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
|
|
|
|
_Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
|
|
|
|
_Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
|
JarURLConnection.java (getCertificates): New method from Classpath.
* java/net/JarURLConnection.java (getCertificates): New method
from Classpath.
* java/net/URLClassLoader.java (URLClassLoader): Extends
SecureClassLoader.
(definePackage): New method from Classpath.
(getPermissions): Likewise.
(newInstance): Likewise.
(findClass): Construct CodeSource for new class (from Classpath).
* java/net/SocketImpl.java (shutdownInput, shutdownOutput): New
methods.
* java/net/URL.java (getUserInfo): New method.
(set(String,String,int,String,String,String,String,String)): New
method.
* java/net/PlainSocketImpl.java (_Jv_SO_KEEPALIVE_): Define.
(shutdownInput, shutdownOutput): Declare.
* java/net/PlainDatagramSocketImpl.java (_Jv_SO_KEEPALIVE_):
Define.
* java/net/natPlainSocketImpl.cc (setOption): Handle keepalive.
(getOption): Likewise.
(shutdownInput): New method.
(shutdownOutput): Likewise.
* java/net/natPlainDatagramSocketImpl.cc (setOption): Handle
keepalive.
(getOption): Likewise.
* java/net/SocketOptions.java (SO_KEEPALIVE): New constant.
* java/net/Socket.java (setKeepAlive): New method.
(getKeepAlive): Likewise.
(shutdownInput, shutdownOutput): New methods.
From-SVN: r56685
2002-08-30 20:16:00 +02:00
|
|
|
_Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
|
|
|
|
_Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
|
1999-05-26 19:00:06 +02:00
|
|
|
|
2002-01-08 22:14:58 +01:00
|
|
|
/**
|
|
|
|
* The OS file handle representing the socket.
|
|
|
|
* This is used for reads and writes to/from the socket and
|
|
|
|
* to close it.
|
|
|
|
*
|
|
|
|
* When the socket is closed this is reset to -1.
|
|
|
|
*/
|
2003-10-21 14:29:43 +02:00
|
|
|
int native_fd = -1;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
1999-05-28 21:29:53 +02:00
|
|
|
// This value is set/read by setOption/getOption.
|
|
|
|
int timeout = 0;
|
1999-06-17 02:21:26 +02:00
|
|
|
|
|
|
|
// localAddress cache
|
|
|
|
InetAddress localAddress;
|
1999-05-28 21:29:53 +02:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* A cached copy of the in stream for reading from the socket.
|
|
|
|
*/
|
|
|
|
private InputStream in;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A cached copy of the out stream for writing to the socket.
|
|
|
|
*/
|
|
|
|
private OutputStream out;
|
|
|
|
|
2004-01-30 14:43:21 +01:00
|
|
|
/**
|
|
|
|
* Indicates whether a channel initiated whatever operation
|
|
|
|
* is being invoked on this socket.
|
|
|
|
*/
|
|
|
|
private boolean inChannelOperation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether we should ignore whether any associated
|
|
|
|
* channel is set to non-blocking mode. Certain operations
|
|
|
|
* throw an <code>IllegalBlockingModeException</code> if the
|
|
|
|
* associated channel is in non-blocking mode, <i>except</i>
|
|
|
|
* if the operation is invoked by the channel itself.
|
|
|
|
*/
|
|
|
|
public final boolean isInChannelOperation()
|
|
|
|
{
|
|
|
|
return inChannelOperation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets our indicator of whether an I/O operation is being
|
|
|
|
* initiated by a channel.
|
|
|
|
*/
|
|
|
|
public final void setInChannelOperation(boolean b)
|
|
|
|
{
|
|
|
|
inChannelOperation = b;
|
|
|
|
}
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Default do nothing constructor
|
|
|
|
*/
|
|
|
|
public PlainSocketImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void finalize() throws Throwable
|
|
|
|
{
|
|
|
|
synchronized (this)
|
|
|
|
{
|
2003-10-21 14:29:43 +02:00
|
|
|
if (native_fd != -1)
|
2003-06-21 16:02:10 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.finalize();
|
|
|
|
}
|
|
|
|
|
2003-09-25 21:22:32 +02:00
|
|
|
public int getNativeFD()
|
|
|
|
{
|
2003-10-21 14:29:43 +02:00
|
|
|
return native_fd;
|
2003-09-25 21:22:32 +02:00
|
|
|
}
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Sets the specified option on a socket to the passed in object. For
|
|
|
|
* options that take an integer argument, the passed in object is an
|
|
|
|
* Integer. The option_id parameter is one of the defined constants in
|
|
|
|
* this interface.
|
|
|
|
*
|
|
|
|
* @param option_id The identifier of the option
|
|
|
|
* @param val The value to set the option to
|
|
|
|
*
|
|
|
|
* @exception SocketException If an error occurs
|
|
|
|
*/
|
1999-05-26 19:00:06 +02:00
|
|
|
public native void setOption(int optID, Object value) throws SocketException;
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Returns the current setting of the specified option. The Object returned
|
|
|
|
* will be an Integer for options that have integer values. The option_id
|
|
|
|
* is one of the defined constants in this interface.
|
|
|
|
*
|
|
|
|
* @param option_id The option identifier
|
|
|
|
*
|
|
|
|
* @return The current value of the option
|
|
|
|
*
|
|
|
|
* @exception SocketException If an error occurs
|
|
|
|
*/
|
1999-05-26 19:00:06 +02:00
|
|
|
public native Object getOption(int optID) throws SocketException;
|
|
|
|
|
2005-02-17 19:59:55 +01:00
|
|
|
/**
|
|
|
|
* Flushes the input stream and closes it. If you read from the input stream
|
|
|
|
* after calling this method a <code>IOException</code> will be thrown.
|
|
|
|
*
|
|
|
|
* @throws IOException if an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
public native void shutdownInput() throws IOException;
|
JarURLConnection.java (getCertificates): New method from Classpath.
* java/net/JarURLConnection.java (getCertificates): New method
from Classpath.
* java/net/URLClassLoader.java (URLClassLoader): Extends
SecureClassLoader.
(definePackage): New method from Classpath.
(getPermissions): Likewise.
(newInstance): Likewise.
(findClass): Construct CodeSource for new class (from Classpath).
* java/net/SocketImpl.java (shutdownInput, shutdownOutput): New
methods.
* java/net/URL.java (getUserInfo): New method.
(set(String,String,int,String,String,String,String,String)): New
method.
* java/net/PlainSocketImpl.java (_Jv_SO_KEEPALIVE_): Define.
(shutdownInput, shutdownOutput): Declare.
* java/net/PlainDatagramSocketImpl.java (_Jv_SO_KEEPALIVE_):
Define.
* java/net/natPlainSocketImpl.cc (setOption): Handle keepalive.
(getOption): Likewise.
(shutdownInput): New method.
(shutdownOutput): Likewise.
* java/net/natPlainDatagramSocketImpl.cc (setOption): Handle
keepalive.
(getOption): Likewise.
* java/net/SocketOptions.java (SO_KEEPALIVE): New constant.
* java/net/Socket.java (setKeepAlive): New method.
(getKeepAlive): Likewise.
(shutdownInput, shutdownOutput): New methods.
From-SVN: r56685
2002-08-30 20:16:00 +02:00
|
|
|
|
2005-02-17 19:59:55 +01:00
|
|
|
/**
|
|
|
|
* Flushes the output stream and closes it. If you write to the output stream
|
|
|
|
* after calling this method a <code>IOException</code> will be thrown.
|
|
|
|
*
|
|
|
|
* @throws IOException if an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
public native void shutdownOutput() throws IOException;
|
JarURLConnection.java (getCertificates): New method from Classpath.
* java/net/JarURLConnection.java (getCertificates): New method
from Classpath.
* java/net/URLClassLoader.java (URLClassLoader): Extends
SecureClassLoader.
(definePackage): New method from Classpath.
(getPermissions): Likewise.
(newInstance): Likewise.
(findClass): Construct CodeSource for new class (from Classpath).
* java/net/SocketImpl.java (shutdownInput, shutdownOutput): New
methods.
* java/net/URL.java (getUserInfo): New method.
(set(String,String,int,String,String,String,String,String)): New
method.
* java/net/PlainSocketImpl.java (_Jv_SO_KEEPALIVE_): Define.
(shutdownInput, shutdownOutput): Declare.
* java/net/PlainDatagramSocketImpl.java (_Jv_SO_KEEPALIVE_):
Define.
* java/net/natPlainSocketImpl.cc (setOption): Handle keepalive.
(getOption): Likewise.
(shutdownInput): New method.
(shutdownOutput): Likewise.
* java/net/natPlainDatagramSocketImpl.cc (setOption): Handle
keepalive.
(getOption): Likewise.
* java/net/SocketOptions.java (SO_KEEPALIVE): New constant.
* java/net/Socket.java (setKeepAlive): New method.
(getKeepAlive): Likewise.
(shutdownInput, shutdownOutput): New methods.
From-SVN: r56685
2002-08-30 20:16:00 +02:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Creates a new socket that is not bound to any local address/port and
|
|
|
|
* is not connected to any remote address/port. This will be created as
|
|
|
|
* a stream socket if the stream parameter is true, or a datagram socket
|
|
|
|
* if the stream parameter is false.
|
|
|
|
*
|
|
|
|
* @param stream true for a stream socket, false for a datagram socket
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected native void create(boolean stream) throws IOException;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Connects to the remote hostname and port specified as arguments.
|
|
|
|
*
|
|
|
|
* @param hostname The remote hostname to connect to
|
|
|
|
* @param port The remote port to connect to
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected void connect(String host, int port) throws IOException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2004-03-11 17:56:30 +01:00
|
|
|
connect(InetAddress.getByName(host), port);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Connects to the remote address and port specified as arguments.
|
|
|
|
*
|
|
|
|
* @param addr The remote address to connect to
|
|
|
|
* @param port The remote port to connect to
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected void connect(InetAddress host, int port) throws IOException
|
2002-09-10 20:02:02 +02:00
|
|
|
{
|
|
|
|
connect (new InetSocketAddress (host, port), 0);
|
|
|
|
}
|
|
|
|
|
2004-03-11 17:56:30 +01:00
|
|
|
/**
|
|
|
|
* Connects to the remote socket address with a specified timeout.
|
|
|
|
*
|
|
|
|
* @param timeout The timeout to use for this connect, 0 means infinite.
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
|
|
|
protected native void connect(SocketAddress addr, int timeout) throws IOException;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Binds to the specified port on the specified addr. Note that this addr
|
|
|
|
* must represent a local IP address. **** How bind to INADDR_ANY? ****
|
|
|
|
*
|
|
|
|
* @param addr The address to bind to
|
|
|
|
* @param port The port number to bind to
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected native void bind(InetAddress host, int port)
|
|
|
|
throws IOException;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Starts listening for connections on a socket. The queuelen parameter
|
|
|
|
* is how many pending connections will queue up waiting to be serviced
|
|
|
|
* before being accept'ed. If the queue of pending requests exceeds this
|
|
|
|
* number, additional connections will be refused.
|
|
|
|
*
|
|
|
|
* @param queuelen The length of the pending connection queue
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected native void listen(int queuelen)
|
|
|
|
throws IOException;
|
2002-01-08 22:14:58 +01:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Accepts a new connection on this socket and returns in in the
|
|
|
|
* passed in SocketImpl.
|
|
|
|
*
|
|
|
|
* @param impl The SocketImpl object to accept this connection.
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected void accept(SocketImpl impl)
|
|
|
|
throws IOException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2004-03-11 17:56:30 +01:00
|
|
|
accept((PlainSocketImpl) impl);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2004-03-11 17:56:30 +01:00
|
|
|
private native void accept(PlainSocketImpl impl)
|
|
|
|
throws IOException;
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes that the caller can read from this socket
|
|
|
|
* without blocking.
|
|
|
|
*
|
|
|
|
* @return The number of readable bytes before blocking
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2002-01-08 22:14:58 +01:00
|
|
|
protected native int available() throws IOException;
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Closes the socket. This will cause any InputStream or OutputStream
|
|
|
|
* objects for this Socket to be closed as well.
|
|
|
|
* <p>
|
|
|
|
* Note that if the SO_LINGER option is set on this socket, then the
|
|
|
|
* operation could block.
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2004-03-11 17:56:30 +01:00
|
|
|
protected native void close() throws IOException;
|
2002-01-08 22:14:58 +01:00
|
|
|
|
2004-03-11 17:56:30 +01:00
|
|
|
protected native void sendUrgentData(int data) throws IOException;
|
2002-01-08 22:14:58 +01:00
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Returns an InputStream object for reading from this socket. This will
|
|
|
|
* be an instance of SocketInputStream.
|
|
|
|
*
|
|
|
|
* @return An input stream attached to the socket.
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
2002-01-08 22:14:58 +01:00
|
|
|
*/
|
2003-06-21 16:02:10 +02:00
|
|
|
protected synchronized InputStream getInputStream() throws IOException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
if (in == null)
|
2002-01-08 22:14:58 +01:00
|
|
|
in = new SocketInputStream();
|
2003-06-21 16:02:10 +02:00
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2003-06-21 16:02:10 +02:00
|
|
|
/**
|
|
|
|
* Returns an OutputStream object for writing to this socket. This will
|
|
|
|
* be an instance of SocketOutputStream.
|
|
|
|
*
|
|
|
|
* @return An output stream attached to the socket.
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
2002-01-08 22:14:58 +01:00
|
|
|
*/
|
2003-06-21 16:02:10 +02:00
|
|
|
protected synchronized OutputStream getOutputStream() throws IOException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
if (out == null)
|
2002-01-08 22:14:58 +01:00
|
|
|
out = new SocketOutputStream();
|
2003-06-21 16:02:10 +02:00
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2002-01-08 22:14:58 +01:00
|
|
|
/**
|
2004-03-11 17:56:30 +01:00
|
|
|
* This class contains an implementation of <code>InputStream</code> for
|
|
|
|
* sockets. It in an internal only class used by <code>PlainSocketImpl</code>.
|
2002-01-08 22:14:58 +01:00
|
|
|
*
|
|
|
|
* @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
|
|
|
|
*/
|
2003-09-24 07:38:36 +02:00
|
|
|
final class SocketInputStream
|
2002-01-08 22:14:58 +01:00
|
|
|
extends InputStream
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2004-03-11 17:56:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes available to be read before blocking
|
|
|
|
*/
|
|
|
|
public int available() throws IOException
|
2002-01-08 22:14:58 +01:00
|
|
|
{
|
2004-03-11 17:56:30 +01:00
|
|
|
return PlainSocketImpl.this.available();
|
2002-01-08 22:14:58 +01:00
|
|
|
}
|
|
|
|
|
2004-03-11 17:56:30 +01:00
|
|
|
/**
|
|
|
|
* This method not only closes the stream, it closes the underlying socket
|
|
|
|
* (and thus any connection) and invalidates any other Input/Output streams
|
|
|
|
* for the underlying impl object
|
|
|
|
*/
|
|
|
|
public void close() throws IOException
|
2002-01-08 22:14:58 +01:00
|
|
|
{
|
2004-03-11 17:56:30 +01:00
|
|
|
PlainSocketImpl.this.close();
|
2002-01-08 22:14:58 +01:00
|
|
|
}
|
2004-03-11 17:56:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the next byte of data and returns it as an int.
|
|
|
|
*
|
|
|
|
* @return The byte read (as an int) or -1 if end of stream);
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs.
|
|
|
|
*/
|
|
|
|
public native int read() throws IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads up to len bytes of data into the caller supplied buffer starting
|
|
|
|
* at offset bytes from the start of the buffer
|
|
|
|
*
|
|
|
|
* @param buf The buffer
|
|
|
|
* @param offset Offset into the buffer to start reading from
|
|
|
|
* @param len The number of bytes to read
|
|
|
|
*
|
|
|
|
* @return The number of bytes actually read or -1 if end of stream
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs.
|
|
|
|
*/
|
|
|
|
public native int read(byte[] buf, int offset, int len) throws IOException;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2004-03-11 17:56:30 +01:00
|
|
|
/**
|
|
|
|
* This class is used internally by <code>PlainSocketImpl</code> to be the
|
|
|
|
* <code>OutputStream</code> subclass returned by its
|
|
|
|
* <code>getOutputStream method</code>. It expects only to be used in that
|
|
|
|
* context.
|
2002-01-08 22:14:58 +01:00
|
|
|
*
|
|
|
|
* @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
|
|
|
|
*/
|
2003-09-24 07:38:36 +02:00
|
|
|
final class SocketOutputStream
|
2002-01-08 22:14:58 +01:00
|
|
|
extends OutputStream
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2004-03-11 17:56:30 +01:00
|
|
|
/**
|
|
|
|
* This method closes the stream and the underlying socket connection. This
|
|
|
|
* action also effectively closes any other InputStream or OutputStream
|
|
|
|
* object associated with the connection.
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
2003-09-24 07:38:36 +02:00
|
|
|
public void close() throws IOException
|
2002-01-08 22:14:58 +01:00
|
|
|
{
|
2003-09-24 07:38:36 +02:00
|
|
|
PlainSocketImpl.this.close();
|
2002-01-08 22:14:58 +01:00
|
|
|
}
|
2004-03-11 17:56:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a byte (passed in as an int) to the given output stream
|
|
|
|
*
|
|
|
|
* @param b The byte to write
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs
|
|
|
|
*/
|
|
|
|
public native void write(int b) throws IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes len number of bytes from the array buf to the stream starting
|
|
|
|
* at offset bytes into the buffer.
|
|
|
|
*
|
|
|
|
* @param buf The buffer
|
|
|
|
* @param offset Offset into the buffer to start writing from
|
|
|
|
* @param len The number of bytes to write
|
|
|
|
*
|
|
|
|
* @exception IOException If an error occurs.
|
|
|
|
*/
|
|
|
|
public native void write(byte[] buf, int offset, int len) throws IOException;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
}
|