From be362a0d5ba42bee36bd339e25374100a9f1942c Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Sat, 21 Sep 2002 06:59:20 +0000 Subject: [PATCH] 2002-09-21 Michael Koch * java/net/Socket.java (sendUrgentData): New method. (getChannel): New method. * java/net/ServerSocket.java (getChannel): New method. (isBound): New method. * java/net/DatagramSocket.java (DatagramSocket): Two new methods. (bind): New method. (getChannel): New method. (isBound): New method. (send): Added newline to to make shorter lines. * java/net/PlainDatagramSocketImpl.java (mcastGrp): Added argument. (join): Use new mcastGrp. (leave): Use new mcastGrp. (joinGroup): New method. (leaveGroup): New method. * java/net/natPlainDatagramSocketImpl.cc (mcastGrp): Added argument, no yet really implemented. (getOption): Added newline for shorter lines. * java/net/natPlainSocketImpl.cc (read, setOption, getOption): Added newline for shorter lines. From-SVN: r57380 --- libjava/ChangeLog | 26 ++++++ libjava/java/net/DatagramSocket.java | 81 ++++++++++++++++++- libjava/java/net/PlainDatagramSocketImpl.java | 20 ++++- libjava/java/net/ServerSocket.java | 40 +++++++++ libjava/java/net/Socket.java | 28 +++++++ .../java/net/natPlainDatagramSocketImpl.cc | 8 +- libjava/java/net/natPlainSocketImpl.cc | 15 ++-- 7 files changed, 205 insertions(+), 13 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 24bd7ac9444..7f50578f476 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,29 @@ +2002-09-21 Michael Koch + + * java/net/Socket.java + (sendUrgentData): New method. + (getChannel): New method. + * java/net/ServerSocket.java + (getChannel): New method. + (isBound): New method. + * java/net/DatagramSocket.java + (DatagramSocket): Two new methods. + (bind): New method. + (getChannel): New method. + (isBound): New method. + (send): Added newline to to make shorter lines. + * java/net/PlainDatagramSocketImpl.java + (mcastGrp): Added argument. + (join): Use new mcastGrp. + (leave): Use new mcastGrp. + (joinGroup): New method. + (leaveGroup): New method. + * java/net/natPlainDatagramSocketImpl.cc + (mcastGrp): Added argument, no yet really implemented. + (getOption): Added newline for shorter lines. + * java/net/natPlainSocketImpl.cc + (read, setOption, getOption): Added newline for shorter lines. + 2002-09-19 Tom Tromey * java/lang/ClassLoader.java (resolveClass0): Set cause for diff --git a/libjava/java/net/DatagramSocket.java b/libjava/java/net/DatagramSocket.java index 93aea0754ed..da97d6115c3 100644 --- a/libjava/java/net/DatagramSocket.java +++ b/libjava/java/net/DatagramSocket.java @@ -1,6 +1,6 @@ // DatagramSocket.java -/* Copyright (C) 1999, 2000 Free Software Foundation +/* Copyright (C) 1999, 2000, 2002 Free Software Foundation This file is part of libgcj. @@ -10,6 +10,7 @@ details. */ package java.net; import java.io.IOException; +import java.nio.channels.DatagramChannel; /** * @author Warren Levy @@ -26,11 +27,41 @@ public class DatagramSocket { DatagramSocketImpl impl; + DatagramChannel ch; + public DatagramSocket() throws SocketException { this(0, null); } + /** + * Creates a DatagramSocket from a specified DatagramSocketImpl instance + * + * @param impl The DatagramSocketImpl the socket will be created from + * + * @since 1.4 + */ + protected DatagramSocket (DatagramSocketImpl impl) + { + this.impl = impl; + } + + /** + * Creates a datagram socket that is bound to a given socket address + * + * @param bindaddr The socket address to bind to + * + * @exception SocketException If an error occurs + * + * @since 1.4 + */ + public DatagramSocket (SocketAddress bindaddr) + throws SocketException + { + this (((InetSocketAddress) bindaddr).getPort (), + ((InetSocketAddress) bindaddr).getAddress ()); + } + /** * Creates a datagram socket that is bound to a specific port * @@ -84,6 +115,22 @@ public class DatagramSocket impl.bind(port, laddr == null ? InetAddress.ANY_IF : laddr); } + /** + * Binds the socket to the given socket addres + * + * @param address The socket address to bind to + * + * @exception SocketException If an error occurs + * + * @since 1.4 + */ + public void bind (SocketAddress address) + throws SocketException + { + InetSocketAddress tmp = (InetSocketAddress) address; + impl.bind (tmp.getPort (), tmp.getAddress ()); + } + /** * Closes the datagram socket */ @@ -92,6 +139,16 @@ public class DatagramSocket impl.close(); } + /** + * Gets a datagram channel assoziated with the socket + * + * @since 1.4 + */ + public DatagramChannel getChannel() + { + return ch; + } + /** * Returns the local address of the datagram socket * @@ -199,7 +256,8 @@ public class DatagramSocket s.checkConnect(addr.getHostAddress(), p.getPort()); } - // FIXME: if this is a subclass of MulticastSocket, use getTimeToLive for TTL val. + // FIXME: if this is a subclass of MulticastSocket, + // use getTimeToLive for TTL val. impl.send(p); } @@ -246,6 +304,25 @@ public class DatagramSocket //impl.disconnect(); } + /** + * Returns the binding state of the socket + * + * @since 1.4 + */ + public boolean isBound() + { + try + { + Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR); + } + catch (SocketException e) + { + return false; + } + + return true; + } + /** * Returns the InetAddress the socket is connected to * or null if the socket is not connected diff --git a/libjava/java/net/PlainDatagramSocketImpl.java b/libjava/java/net/PlainDatagramSocketImpl.java index 3a8db03627e..54f5c2eeb00 100644 --- a/libjava/java/net/PlainDatagramSocketImpl.java +++ b/libjava/java/net/PlainDatagramSocketImpl.java @@ -72,8 +72,8 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl protected native void receive(DatagramPacket p) throws IOException; public native void setOption(int optID, Object value) throws SocketException; public native Object getOption(int optID) throws SocketException; - private native void mcastGrp(InetAddress inetaddr, boolean join) - throws IOException; + private native void mcastGrp(InetAddress inetaddr, NetworkInterface netIf, + boolean join) throws IOException; protected native void close(); // Deprecated in JDK 1.2. @@ -90,12 +90,24 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl protected void join(InetAddress inetaddr) throws IOException { - mcastGrp(inetaddr, true); + mcastGrp(inetaddr, null, true); } protected void leave(InetAddress inetaddr) throws IOException { - mcastGrp(inetaddr, false); + mcastGrp(inetaddr, null, false); + } + + protected void joinGroup (SocketAddress mcastaddr, NetworkInterface netIf) + throws IOException + { + mcastGrp(((InetSocketAddress)mcastaddr).getAddress(), netIf, true); + } + + protected void leaveGroup (SocketAddress mcastaddr, NetworkInterface netIf) + throws IOException + { + mcastGrp(((InetSocketAddress)mcastaddr).getAddress(), netIf, false); } protected void finalize() throws Throwable diff --git a/libjava/java/net/ServerSocket.java b/libjava/java/net/ServerSocket.java index c6b187071ec..b706acca58c 100644 --- a/libjava/java/net/ServerSocket.java +++ b/libjava/java/net/ServerSocket.java @@ -38,6 +38,7 @@ exception statement from your version. */ package java.net; import java.io.IOException; +import java.nio.channels.ServerSocketChannel; /* Written using on-line Java Platform 1.2 API Specification. * Status: I believe all methods are implemented. @@ -74,6 +75,12 @@ public class ServerSocket */ private SocketImpl impl; + /** + * ServerSocketChannel of this ServerSocket. This channel only exists + * when the socket is created by ServerSocketChannel.open(). + */ + private ServerSocketChannel ch; + /** * Private constructor that simply sets the implementation. */ @@ -281,6 +288,39 @@ public class ServerSocket impl.close(); } + /** + * Returns the unique ServerSocketChannel object + * associated with this socket, if any. + * + * The socket only has a ServerSocketChannel if its created + * by ServerSocketChannel.open. + * + * @since 1.4 + */ + public ServerSocketChannel getChannel() + { + return ch; + } + + /** + * Returns true then the socket is bound, otherwise false + * + * @since 1.4 + */ + public boolean isBound() + { + try + { + Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR); + } + catch (SocketException e) + { + return false; + } + + return true; + } + /** * 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 diff --git a/libjava/java/net/Socket.java b/libjava/java/net/Socket.java index 078bfff7a03..25f077b8013 100644 --- a/libjava/java/net/Socket.java +++ b/libjava/java/net/Socket.java @@ -38,6 +38,7 @@ exception statement from your version. */ package java.net; import java.io.*; +import java.nio.channels.SocketChannel; /* Written using on-line Java Platform 1.2 API Specification. * Status: I believe all methods are implemented. @@ -78,6 +79,8 @@ public class Socket */ SocketImpl impl; + SocketChannel ch; // this field must have been set if created by SocketChannel + // Constructors /** @@ -524,6 +527,21 @@ public class Socket return -1; } + /** + * Sends urgent data through the socket + * + * @param data The data to send. + * Only the lowest eight bits of data are sent + * + * @exception IOException If an error occurs + * + * @since 1.4 + */ + public void sendUrgentData (int data) throws IOException + { + impl.sendUrgentData (data); + } + /** * Enables/disables the SO_OOBINLINE option * @@ -819,4 +837,14 @@ public class Socket if (impl != null) impl.shutdownOutput(); } + + /** + * Returns the socket channel associated with this socket. + * + * It returns null if no associated socket exists. + */ + public SocketChannel getChannel() + { + return ch; + } } diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc index 12eaf3be9d9..cf119258c11 100644 --- a/libjava/java/net/natPlainDatagramSocketImpl.cc +++ b/libjava/java/net/natPlainDatagramSocketImpl.cc @@ -63,6 +63,7 @@ _Jv_bind (int fd, struct sockaddr *addr, int addrlen) #include #include #include +#include #include #include #include @@ -136,6 +137,7 @@ java::net::PlainDatagramSocketImpl::getTimeToLive () void java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *, + java::net::NetworkInterface *, jboolean) { throw new java::io::IOException ( @@ -504,8 +506,11 @@ java::net::PlainDatagramSocketImpl::getTimeToLive () void java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *inetaddr, + java::net::NetworkInterface *, jboolean join) { + // FIXME: implement use of NetworkInterface + union McastReq u; jbyteArray haddress = inetaddr->addr; jbyte *bytes = elements (haddress); @@ -769,7 +774,8 @@ java::net::PlainDatagramSocketImpl::getOption (jint optID) } #endif else - throw new java::net::SocketException (JvNewStringUTF ("invalid family")); + throw new java::net::SocketException ( + JvNewStringUTF ("invalid family")); localAddress = new java::net::InetAddress (laddr, NULL); } return localAddress; diff --git a/libjava/java/net/natPlainSocketImpl.cc b/libjava/java/net/natPlainSocketImpl.cc index 9d5b4d20a58..a1e967eb8b3 100644 --- a/libjava/java/net/natPlainSocketImpl.cc +++ b/libjava/java/net/natPlainSocketImpl.cc @@ -381,7 +381,7 @@ java::net::PlainSocketImpl::connect (java::net::SocketAddress *addr, throw new java::net::SocketTimeoutException ( JvNewStringUTF("Connect timed out")); } - else + else #endif { if (_Jv_connect (fnum, ptr, len) != 0) @@ -588,7 +588,8 @@ java::net::PlainSocketImpl::read(void) timeout_value.tv_sec = timeout / 1000; timeout_value.tv_usec = (timeout % 1000) * 1000; // Select on the fds. - int sel_retval = _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value); + int sel_retval = + _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value); // If select returns 0 we've waited without getting data... // that means we've timed out. if (sel_retval == 0) @@ -647,7 +648,8 @@ java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count) timeout_value.tv_sec = timeout / 1000; timeout_value.tv_usec =(timeout % 1000) * 1000; // Select on the fds. - int sel_retval = _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value); + int sel_retval = + _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value); // We're only interested in the 0 return. // error returns still require us to try to read // the socket to see what happened. @@ -776,7 +778,8 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value) } else { - throw new java::lang::IllegalArgumentException (JvNewStringLatin1 ("`value' must be Boolean or Integer")); + throw new java::lang::IllegalArgumentException ( + JvNewStringLatin1 ("`value' must be Boolean or Integer")); } switch (optID) @@ -968,8 +971,8 @@ java::net::PlainSocketImpl::getOption (jint optID) } #endif else - throw - new java::net::SocketException (JvNewStringUTF ("invalid family")); + throw new java::net::SocketException ( + JvNewStringUTF ("invalid family")); localAddress = new java::net::InetAddress (laddr, NULL); } return localAddress; -- 2.30.2