151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 27f4b1b8935a58d3f44351083cf5ef19045761de3Yi Kong * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.net; 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.FileDescriptor; 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.IOException; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.InterruptedIOException; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Abstract datagram and multicast socket implementation base class. 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Pavani Diwanji 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic abstract class DatagramSocketImpl implements SocketOptions { 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The local port number. 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected int localPort; 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The file descriptor object. 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected FileDescriptor fd; 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 506031a510d88327487d4a22bdfef0c6ba17a7b2a6Yi Kong int dataAvailable() { 516031a510d88327487d4a22bdfef0c6ba17a7b2a6Yi Kong // default impl returns zero, which disables the calling 526031a510d88327487d4a22bdfef0c6ba17a7b2a6Yi Kong // functionality 536031a510d88327487d4a22bdfef0c6ba17a7b2a6Yi Kong return 0; 546031a510d88327487d4a22bdfef0c6ba17a7b2a6Yi Kong } 556031a510d88327487d4a22bdfef0c6ba17a7b2a6Yi Kong 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57e573e88e89daf5efb323719c54117c5a423eb245Yi Kong * The DatagramSocket or MulticastSocket 58e573e88e89daf5efb323719c54117c5a423eb245Yi Kong * that owns this impl 59e573e88e89daf5efb323719c54117c5a423eb245Yi Kong */ 60e573e88e89daf5efb323719c54117c5a423eb245Yi Kong DatagramSocket socket; 61e573e88e89daf5efb323719c54117c5a423eb245Yi Kong 62e573e88e89daf5efb323719c54117c5a423eb245Yi Kong void setDatagramSocket(DatagramSocket socket) { 63e573e88e89daf5efb323719c54117c5a423eb245Yi Kong this.socket = socket; 64e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 65e573e88e89daf5efb323719c54117c5a423eb245Yi Kong 66e573e88e89daf5efb323719c54117c5a423eb245Yi Kong DatagramSocket getDatagramSocket() { 67e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return socket; 68e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 69e573e88e89daf5efb323719c54117c5a423eb245Yi Kong 70e573e88e89daf5efb323719c54117c5a423eb245Yi Kong /** 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a datagram socket. 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SocketException if there is an error in the 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * underlying protocol, such as a TCP error. 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void create() throws SocketException; 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Binds a datagram socket to a local port and address. 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param lport the local port 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param laddr the local address 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SocketException if there is an error in the 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * underlying protocol, such as a TCP error. 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void bind(int lport, InetAddress laddr) throws SocketException; 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sends a datagram packet. The packet contains the data and the 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * destination address to send the packet to. 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param p the packet to be sent. 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs while sending the 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * datagram packet. 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception PortUnreachableException may be thrown if the socket is connected 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to a currently unreachable destination. Note, there is no guarantee that 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the exception will be thrown. 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void send(DatagramPacket p) throws IOException; 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Connects a datagram socket to a remote destination. This associates the remote 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * address with the local socket so that datagrams may only be sent to this destination 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and received from this destination. This may be overridden to call a native 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * system connect. 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the remote destination to which the socket is connected does not 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exist, or is otherwise unreachable, and if an ICMP destination unreachable 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * packet has been received for that address, then a subsequent call to 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * send or receive may throw a PortUnreachableException. 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note, there is no guarantee that the exception will be thrown. 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param address the remote InetAddress to connect to 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param port the remote port number 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SocketException may be thrown if the socket cannot be 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * connected to the remote destination 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected void connect(InetAddress address, int port) throws SocketException {} 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Disconnects a datagram socket from its remote destination. 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected void disconnect() {} 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 1243a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * Peek at the packet to see who it is from. Updates the specified {@code InetAddress} 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the address which the packet came from. 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an InetAddress object 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the port number which the packet came from. 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception PortUnreachableException may be thrown if the socket is connected 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to a currently unreachable destination. Note, there is no guarantee that the 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exception will be thrown. 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract int peek(InetAddress i) throws IOException; 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Peek at the packet to see who it is from. The data is copied into the specified 1373a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * {@code DatagramPacket}. The data is returned, 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * but not consumed, so that a subsequent peekData/receive operation 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will see the same data. 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param p the Packet Received. 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the port number which the packet came from. 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception PortUnreachableException may be thrown if the socket is connected 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to a currently unreachable destination. Note, there is no guarantee that the 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exception will be thrown. 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract int peekData(DatagramPacket p) throws IOException; 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Receive the datagram packet. 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param p the Packet Received. 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while receiving the datagram packet. 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception PortUnreachableException may be thrown if the socket is connected 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to a currently unreachable destination. Note, there is no guarantee that the 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exception will be thrown. 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void receive(DatagramPacket p) throws IOException; 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Set the TTL (time-to-live) option. 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param ttl a byte specifying the TTL value 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated use setTimeToLive instead. 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs while setting 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time-to-live option. 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getTTL() 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void setTTL(byte ttl) throws IOException; 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Retrieve the TTL (time-to-live) option. 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while retrieving the time-to-live option 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated use getTimeToLive instead. 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a byte representing the TTL value 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setTTL(byte) 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract byte getTTL() throws IOException; 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Set the TTL (time-to-live) option. 1863a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * @param ttl an {@code int} specifying the time-to-live value 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while setting the time-to-live option. 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getTimeToLive() 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void setTimeToLive(int ttl) throws IOException; 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Retrieve the TTL (time-to-live) option. 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while retrieving the time-to-live option 1973a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * @return an {@code int} representing the time-to-live value 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setTimeToLive(int) 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract int getTimeToLive() throws IOException; 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Join the multicast group. 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param inetaddr multicast address to join. 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while joining the multicast group. 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void join(InetAddress inetaddr) throws IOException; 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Leave the multicast group. 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param inetaddr multicast address to leave. 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O exception occurs 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while leaving the multicast group. 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void leave(InetAddress inetaddr) throws IOException; 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Join the multicast group. 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param mcastaddr address to join. 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param netIf specifies the local interface to receive multicast 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * datagram packets 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IOException if an I/O exception occurs while joining 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the multicast group 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void joinGroup(SocketAddress mcastaddr, 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski NetworkInterface netIf) 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws IOException; 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Leave the multicast group. 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param mcastaddr address to leave. 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param netIf specified the local interface to leave the group at 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IOException if an I/O exception occurs while leaving 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the multicast group 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void leaveGroup(SocketAddress mcastaddr, 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski NetworkInterface netIf) 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws IOException; 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Close the socket. 24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected abstract void close(); 24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the local port. 2503a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * @return an {@code int} representing the local port value 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected int getLocalPort() { 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return localPort; 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 256e573e88e89daf5efb323719c54117c5a423eb245Yi Kong <T> void setOption(SocketOption<T> name, T value) throws IOException { 257e573e88e89daf5efb323719c54117c5a423eb245Yi Kong if (name == StandardSocketOptions.SO_SNDBUF) { 258e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setOption(SocketOptions.SO_SNDBUF, value); 259e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.SO_RCVBUF) { 260e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setOption(SocketOptions.SO_RCVBUF, value); 261e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.SO_REUSEADDR) { 262e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setOption(SocketOptions.SO_REUSEADDR, value); 263e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_TOS) { 264e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setOption(SocketOptions.IP_TOS, value); 265e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_MULTICAST_IF && 266e573e88e89daf5efb323719c54117c5a423eb245Yi Kong (getDatagramSocket() instanceof MulticastSocket)) { 267e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setOption(SocketOptions.IP_MULTICAST_IF2, value); 268e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_MULTICAST_TTL && 269e573e88e89daf5efb323719c54117c5a423eb245Yi Kong (getDatagramSocket() instanceof MulticastSocket)) { 270e573e88e89daf5efb323719c54117c5a423eb245Yi Kong if (! (value instanceof Integer)) { 271e573e88e89daf5efb323719c54117c5a423eb245Yi Kong throw new IllegalArgumentException("not an integer"); 272e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 273e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setTimeToLive((Integer)value); 274e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP && 275e573e88e89daf5efb323719c54117c5a423eb245Yi Kong (getDatagramSocket() instanceof MulticastSocket)) { 276e573e88e89daf5efb323719c54117c5a423eb245Yi Kong setOption(SocketOptions.IP_MULTICAST_LOOP, value); 277e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else { 278e573e88e89daf5efb323719c54117c5a423eb245Yi Kong throw new UnsupportedOperationException("unsupported option"); 279e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 280e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 281e573e88e89daf5efb323719c54117c5a423eb245Yi Kong 282e573e88e89daf5efb323719c54117c5a423eb245Yi Kong <T> T getOption(SocketOption<T> name) throws IOException { 283e573e88e89daf5efb323719c54117c5a423eb245Yi Kong if (name == StandardSocketOptions.SO_SNDBUF) { 284e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T) getOption(SocketOptions.SO_SNDBUF); 285e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.SO_RCVBUF) { 286e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T) getOption(SocketOptions.SO_RCVBUF); 287e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.SO_REUSEADDR) { 288e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T) getOption(SocketOptions.SO_REUSEADDR); 289e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_TOS) { 290e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T) getOption(SocketOptions.IP_TOS); 291e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_MULTICAST_IF && 292e573e88e89daf5efb323719c54117c5a423eb245Yi Kong (getDatagramSocket() instanceof MulticastSocket)) { 293e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T) getOption(SocketOptions.IP_MULTICAST_IF2); 294e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_MULTICAST_TTL && 295e573e88e89daf5efb323719c54117c5a423eb245Yi Kong (getDatagramSocket() instanceof MulticastSocket)) { 296e573e88e89daf5efb323719c54117c5a423eb245Yi Kong Integer ttl = getTimeToLive(); 297e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T)ttl; 298e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP && 299e573e88e89daf5efb323719c54117c5a423eb245Yi Kong (getDatagramSocket() instanceof MulticastSocket)) { 300e573e88e89daf5efb323719c54117c5a423eb245Yi Kong return (T) getOption(SocketOptions.IP_MULTICAST_LOOP); 301e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } else { 302e573e88e89daf5efb323719c54117c5a423eb245Yi Kong throw new UnsupportedOperationException("unsupported option"); 303e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 304e573e88e89daf5efb323719c54117c5a423eb245Yi Kong } 305e573e88e89daf5efb323719c54117c5a423eb245Yi Kong 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the datagram socket file descriptor. 3083a6411ec91b24e73f36301d0075bc7b052894ae9Yi Kong * @return a {@code FileDescriptor} object representing the datagram socket 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file descriptor 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected FileDescriptor getFileDescriptor() { 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return fd; 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 315