Commit 9566a90c by Mark Wielaard Committed by Mark Wielaard

InetAddress.java (ANY_IF): moved from ServerSocket.

        * java/net/InetAddress.java (ANY_IF): moved from ServerSocket.
        * java/net/DatagramSocket.java (DatagramSocket): use ANY_IF from
        InetAddress.
        * java/net/MulticastSocket.java (MulticastSocket): Likewise.
        * java/net/Socket.java: Merge with Classpath.
        * java/net/ServerSocket.java: Likewise.

From-SVN: r48797
parent e37af218
2002-01-11 Mark Wielaard <mark@klomp.org>
* java/net/InetAddress.java (ANY_IF): moved from ServerSocket.
* java/net/DatagramSocket.java (DatagramSocket): use ANY_IF from
InetAddress.
* java/net/MulticastSocket.java (MulticastSocket): Likewise.
* java/net/Socket.java: Merge with Classpath.
* java/net/ServerSocket.java: Likewise.
2002-01-11 Chris Sears <cbsears_sf@yahoo.com> 2002-01-11 Chris Sears <cbsears_sf@yahoo.com>
* interpret.cc (NULLARRAYCHECK): New macro. * interpret.cc (NULLARRAYCHECK): New macro.
......
...@@ -28,12 +28,12 @@ public class DatagramSocket ...@@ -28,12 +28,12 @@ public class DatagramSocket
public DatagramSocket() throws SocketException public DatagramSocket() throws SocketException
{ {
this(0, ServerSocket.ANY_IF); this(0, null);
} }
public DatagramSocket(int port) throws SocketException public DatagramSocket(int port) throws SocketException
{ {
this(port, ServerSocket.ANY_IF); this(port, null);
} }
public DatagramSocket(int port, InetAddress laddr) throws SocketException public DatagramSocket(int port, InetAddress laddr) throws SocketException
...@@ -66,7 +66,7 @@ public class DatagramSocket ...@@ -66,7 +66,7 @@ public class DatagramSocket
if (this instanceof MulticastSocket) if (this instanceof MulticastSocket)
impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(true)); impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(true));
impl.bind(port, laddr == null ? ServerSocket.ANY_IF : laddr); impl.bind(port, laddr == null ? InetAddress.ANY_IF : laddr);
} }
public void close() public void close()
......
...@@ -240,6 +240,10 @@ public final class InetAddress implements java.io.Serializable ...@@ -240,6 +240,10 @@ public final class InetAddress implements java.io.Serializable
return lookup(host, null, true); return lookup(host, null, true);
} }
static final byte[] zeros = {0,0,0,0};
/* dummy InetAddress, used to bind socket to any (all) network interfaces */
static final InetAddress ANY_IF = new InetAddress(zeros, null);
private static final byte[] localhostAddress = { 127, 0, 0, 1 }; private static final byte[] localhostAddress = { 127, 0, 0, 1 };
private static native String getLocalHostname (); private static native String getLocalHostname ();
......
...@@ -64,7 +64,7 @@ public class MulticastSocket extends DatagramSocket ...@@ -64,7 +64,7 @@ public class MulticastSocket extends DatagramSocket
*/ */
public MulticastSocket() throws IOException public MulticastSocket() throws IOException
{ {
super(0, ServerSocket.ANY_IF); super(0, null);
} }
/** /**
...@@ -76,7 +76,7 @@ public class MulticastSocket extends DatagramSocket ...@@ -76,7 +76,7 @@ public class MulticastSocket extends DatagramSocket
*/ */
public MulticastSocket(int port) throws IOException public MulticastSocket(int port) throws IOException
{ {
super(port, ServerSocket.ANY_IF); super(port, null);
} }
/** /**
......
// ServerSocket.java /* ServerSocket.java -- Class for implementing server side sockets
Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
/* Copyright (C) 1999 Free Software Foundation This file is part of GNU Classpath.
This file is part of libgcj. 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.
This software is copyrighted work licensed under the terms of the You should have received a copy of the GNU General Public License
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for along with GNU Classpath; see the file COPYING. If not, write to the
details. */ Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
/** As a special exception, if you link this library with other files to
* @author Per Bothner <bothner@cygnus.com> produce an executable, this library does not by itself cause the
* @date January 6, 1999. resulting executable to be covered by the GNU General Public License.
*/ This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
/** Written using on-line Java Platform 1.2 API Specification.
* Status: I believe all methods are implemented.
*/
package java.net; package java.net;
import java.io.*;
import java.io.IOException;
/* Written using on-line Java Platform 1.2 API Specification.
* Status: I believe all methods are implemented.
*/
/**
* This class models server side sockets. The basic model is that the
* server socket is created and bound to some well known port. It then
* listens for and accepts connections. At that point the client and
* server sockets are ready to communicate with one another utilizing
* whatever application layer protocol they desire.
* <p>
* As with the <code>Socket</code> class, most instance methods of this class
* simply redirect their calls to an implementation class.
*
* @author Aaron M. Renn (arenn@urbanophile.com)
* @author Per Bothner (bothner@cygnus.com)
*/
public class ServerSocket public class ServerSocket
{ {
static SocketImplFactory factory;
SocketImpl impl;
static final byte[] zeros = {0,0,0,0}; // Class Variables
/* dummy InetAddress, used to bind socket to any (all) network interfaces */
static final InetAddress ANY_IF = new InetAddress(zeros, null); /**
* This is the user defined SocketImplFactory, if one is supplied
*/
private static SocketImplFactory factory;
// Instance Variables
/**
* This is the SocketImp object to which most instance methods in this
* class are redirected
*/
private SocketImpl impl;
/**
* Private constructor that simply sets the implementation.
*/
private ServerSocket()
{
if (factory != null)
impl = factory.createSocketImpl();
else
impl = new PlainSocketImpl();
}
/**
* Creates a server socket and binds it to the specified port. If the
* port number is 0, a random free port will be chosen. The pending
* connection queue on this socket will be set to 50.
*
* @param port The port number to bind to
*
* @exception IOException If an error occurs
*/
public ServerSocket (int port) public ServerSocket (int port)
throws java.io.IOException throws java.io.IOException
{ {
this(port, 50); this(port, 50);
} }
/**
* Creates a server socket and binds it to the specified port. If the
* port number is 0, a random free port will be chosen. The pending
* connection queue on this socket will be set to the value passed as
* arg2.
*
* @param port The port number to bind to
* @param backlog The length of the pending connection queue
*
* @exception IOException If an error occurs
*/
public ServerSocket (int port, int backlog) public ServerSocket (int port, int backlog)
throws java.io.IOException throws java.io.IOException
{ {
this(port, backlog, ANY_IF); this(port, backlog, null);
} }
/**
* Creates a server socket and binds it to the specified port. If the
* port number is 0, a random free port will be chosen. The pending
* connection queue on this socket will be set to the value passed as
* backlog. The third argument specifies a particular local address to
* bind t or null to bind to all local address.
*
* @param port The port number to bind to
* @param backlog The length of the pending connection queue
* @param bindAddr The address to bind to, or null to bind to all addresses
*
* @exception IOException If an error occurs
*/
public ServerSocket (int port, int backlog, InetAddress bindAddr) public ServerSocket (int port, int backlog, InetAddress bindAddr)
throws java.io.IOException throws java.io.IOException
{ {
if (factory == null) this();
this.impl = new PlainSocketImpl(); if (impl == null)
else throw new IOException("Cannot initialize Socket implementation");
this.impl = factory.createSocketImpl();
SecurityManager s = System.getSecurityManager(); SecurityManager s = System.getSecurityManager();
if (s != null) if (s != null)
s.checkListen(port); s.checkListen(port);
if (bindAddr == null)
bindAddr = InetAddress.ANY_IF;
impl.create(true); impl.create(true);
impl.bind(bindAddr == null ? ANY_IF : bindAddr, port); impl.bind(bindAddr, port);
impl.listen(backlog); impl.listen(backlog);
} }
/**
* This method returns the local address to which this socket is bound
*
* @return The socket's local address
*/
public InetAddress getInetAddress() public InetAddress getInetAddress()
{ {
return impl.getInetAddress(); return impl.getInetAddress();
} }
/**
* This method returns the local port number to which this socket is bound
*
* @return The socket's port number
*/
public int getLocalPort() public int getLocalPort()
{ {
return impl.getLocalPort(); return impl.getLocalPort();
} }
/**
* Accepts a new connection and returns a connected <code>Socket</code>
* instance representing that connection. This method will block until a
* connection is available.
*
* @exception IOException If an error occurs
*/
public Socket accept () throws IOException public Socket accept () throws IOException
{ {
Socket s = new Socket(Socket.factory == null ? new PlainSocketImpl() Socket s = new Socket();
: Socket.factory.createSocketImpl());
implAccept (s); implAccept (s);
return s; return s;
} }
/**
* This protected method is used to help subclasses override
* <code>ServerSocket.accept()</code>. The passed in socket will be
* connected when this method returns.
*
* @param socket The socket that is used for the accepted connection
*
* @exception IOException If an error occurs
*/
protected final void implAccept (Socket s) throws IOException protected final void implAccept (Socket s) throws IOException
{ {
impl.accept(s.impl); impl.accept(s.impl);
} }
/**
* Closes this socket and stops listening for connections
*
* @exception IOException If an error occurs
*/
public void close () throws IOException public void close () throws IOException
{ {
impl.close(); impl.close();
} }
public synchronized void setSoTimeout (int timeout) throws SocketException /**
* Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is
* disabled (ie, operations never time out). This is the number of
* milliseconds a socket operation can block before an
* InterruptedIOException is thrown.
*
* @param timeout The new SO_TIMEOUT value
*
* @exception IOException If an error occurs
*/
public void setSoTimeout (int timeout) throws SocketException
{ {
if (timeout < 0) if (timeout < 0)
throw new IllegalArgumentException("Invalid timeout: " + timeout); throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout)); impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
} }
public synchronized int getSoTimeout () throws SocketException /**
* Retrieves the current value of the SO_TIMEOUT setting. A value of 0
* implies that SO_TIMEOUT is disabled (ie, operations never time out).
* This is the number of milliseconds a socket operation can block before
* an InterruptedIOException is thrown.
*
* @return The value of SO_TIMEOUT
*
* @exception IOException If an error occurs
*/
public int getSoTimeout () throws IOException
{ {
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer)
return ((Integer)timeout).intValue(); if (!(timeout instanceof Integer))
else throw new IOException("Internal Error");
return 0;
return ((Integer)timeout).intValue();
} }
/**
* Returns the value of this socket as a <code>String</code>.
*
* @return This socket represented as a <code>String</code>.
*/
public String toString () public String toString ()
{ {
return "ServerSocket" + impl.toString(); return "ServerSocket " + impl.toString();
} }
// Class methods
/**
* Sets the <code>SocketImplFactory</code> for all
* <code>ServerSocket</code>'s. This may only be done
* once per virtual machine. Subsequent attempts will generate an
* exception. Note that a <code>SecurityManager</code> check is made prior
* to setting the factory. If insufficient privileges exist to set the
* factory, an exception will be thrown
*
* @exception SecurityException If this operation is not allowed by the
* <code>SecurityManager</code>.
* @exception SocketException If the factory object is already defined
* @exception IOException If any other error occurs
*/
public static synchronized void setSocketFactory (SocketImplFactory fac) public static synchronized void setSocketFactory (SocketImplFactory fac)
throws IOException throws IOException
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment