DatagramSocket.java 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/* DatagramSocket.java -- A class to model UDP sockets
   Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.

This file is part of GNU Classpath.

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
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

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. */
37 38

package java.net;
39

40
import java.io.IOException;
41
import java.nio.channels.DatagramChannel;
42
import java.nio.channels.IllegalBlockingModeException;
43 44 45 46 47 48 49

/**
 * 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.
 */

50 51 52 53 54 55 56 57 58 59 60 61
/**
 * This class models a connectionless datagram socket that sends 
 * individual packets of data across the network.  In the TCP/IP world,
 * this means UDP.  Datagram packets do not have guaranteed delivery,
 * or any guarantee about the order the data will be received on the
 * remote host.
 * 
 * @author Aaron M. Renn (arenn@urbanophile.com)
 * @author Warren Levy (warrenl@cygnus.com)
 * @date May 3, 1999.
 */

62 63
public class DatagramSocket
{
64 65 66 67 68 69
  /**
   * This is the user DatagramSocketImplFactory for this class.  If this
   * variable is null, a default factory is used.
   */
  static DatagramSocketImplFactory factory;
	  
70 71 72
  /**
   * This is the implementation object used by this socket.
   */
73 74
  DatagramSocketImpl impl;

75 76 77 78
  /**
   * The unique DatagramChannel object associated with this datagram socket,
   * or null.
   */
79 80
  DatagramChannel ch;

81 82 83
  /**
   * This is the address we are "connected" to
   */
84 85
  private InetAddress remoteAddress;

86
  /**
87
   * This is the port we are "connected" to
88
   */
89
  private int remotePort = -1;
90

91
  /**
92 93 94 95 96 97 98 99 100
   * 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;
101 102
    this.remoteAddress = null;
    this.remotePort = -1;
103 104 105
  }

  /**
106 107
   * Initializes a new instance of <code>DatagramSocket</code> that binds to 
   * a random port and every address on the local machine.
108
   *
109
   * @exception SocketException If an error occurs.
110
   * @exception SecurityException If a security manager exists and
111
   * its checkListen method doesn't allow the operation.
112
   */
113
  public DatagramSocket() throws SocketException
114
  {
115
    this(0, null);
116 117 118
  }

  /**
119 120
   * Initializes a new instance of <code>DatagramSocket</code> that binds to 
   * the specified port and every address on the local machine.
121
   *
122
   * @param port The local port number to bind to.
123
   *
124 125 126
   * @exception SecurityException If a security manager exists and its
   * checkListen method doesn't allow the operation.
   * @exception SocketException If an error occurs.
127
   */
128 129
  public DatagramSocket(int port) throws SocketException
  {
130
    this(port, null);
131 132
  }

133
  /**
134 135
   * Initializes a new instance of <code>DatagramSocket</code> that binds to 
   * the specified local port and address.
136
   *
137 138
   * @param port The local port number to bind to.
   * @param laddr The local address to bind to.
139
   *
140 141 142
   * @exception SecurityException If a security manager exists and its
   * checkListen method doesn't allow the operation.
   * @exception SocketException If an error occurs.
143
   */
144 145 146 147 148 149 150 151 152 153 154
  public DatagramSocket(int port, InetAddress laddr) throws SocketException
  {
    if (port < 0 || port > 65535)
      throw new IllegalArgumentException("Invalid port: " + port);

    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkListen(port);

    String propVal = System.getProperty("impl.prefix");
    if (propVal == null || propVal.equals(""))
155 156 157 158
      impl = new PlainDatagramSocketImpl();
    else
      try
	{
159 160
          impl = (DatagramSocketImpl) Class.forName
            ("java.net." + propVal + "DatagramSocketImpl").newInstance();
161 162 163 164 165 166 167
	}
      catch (Exception e)
	{
	  System.err.println("Could not instantiate class: java.net." +
	    propVal + "DatagramSocketImpl");
	  impl = new PlainDatagramSocketImpl();
	}
168
    impl.create();
Warren Levy committed
169 170 171 172 173

    // For multicasting, set the socket to be reused (Stevens pp. 195-6).
    if (this instanceof MulticastSocket)
      impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(true));

174
    impl.bind(port, laddr == null ? InetAddress.ANY_IF : laddr);
175 176 177
    
    remoteAddress = null;
    remotePort = -1;
178 179
  }

180
  /**
181 182
   * Initializes a new instance of <code>DatagramSocket</code> that binds to 
   * the specified local port and address.
183
   *
184 185
   * @param port The local port number to bind to.
   * @param laddr The local address to bind to.
186
   *
187 188 189
   * @exception SecurityException If a security manager exists and its
   * checkListen method doesn't allow the operation.
   * @exception SocketException If an error occurs.
190 191 192
   *
   * @since 1.4
   */
193
  public DatagramSocket (SocketAddress address) throws SocketException
194
  {
195 196
    this (((InetSocketAddress) address).getPort (),
          ((InetSocketAddress) address).getAddress ());
197 198 199
  }
  
  /**
200
   * Closes this datagram socket.
201
   */
202 203 204
  public void close()
  {
    impl.close();
205 206
    remoteAddress = null;
    remotePort = -1;
207 208
  }

209
  /**
210 211 212
   * This method returns the remote address to which this socket is 
   * connected.  If this socket is not connected, then this method will
   * return <code>null</code>.
213
   * 
214 215 216
   * @return The remote address.
   *
   * @since 1.2
217
   */
218
  public InetAddress getInetAddress()
219
  {
220 221 222 223
    if (!isConnected ())
      return null;

    return remoteAddress;
224 225 226
  }

  /**
227 228 229
   * This method returns the remote port to which this socket is
   * connected.  If this socket is not connected, then this method will
   * return -1.
230
   * 
231 232 233
   * @return The remote port.
   *
   * @since 1.2
234
   */
235
  public int getPort()
236
  {
237 238 239 240
    if (!isConnected ())
      return -1;
    
    return remotePort;
241 242 243
  }

  /**
244
   * Returns the local address this datagram socket is bound to.
245 246 247
   * 
   * @since 1.1
   */
248 249
  public InetAddress getLocalAddress()
  {
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    // FIXME: JCL p. 510 says this should call checkConnect.  But what
    // string should be used as the hostname?  Maybe this is just a side
    // effect of calling InetAddress.getLocalHost.
    //
    // And is getOption with SO_BINDADDR the right way to get the address?
    // Doesn't seem to be since this method doesn't throw a SocketException
    // and SO_BINADDR can throw one.
    //
    // Also see RETURNS section in JCL p. 510 about returning any local
    // addr "if the current execution context is not allowed to connect to
    // the network interface that is actually bound to this datagram socket."
    // How is that done?  via InetAddress.getLocalHost?  But that throws
    // an UnknownHostException and this method doesn't.
    //
    // if (s != null)
    //   s.checkConnect("localhost", -1);
    try
      {
	return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
      }
    catch (SocketException ex)
      {
      }

    try
      {
	return InetAddress.getLocalHost();
      }
    catch (UnknownHostException ex)
      {
	// FIXME: This should never happen, so how can we avoid this construct?
	return null;
      }
283 284
  }

285
  /**
286
   * Returns the local port this socket is bound to.
287
   *
288
   * @return The local port number.
289
   */
290 291
  public int getLocalPort()
  {
292 293 294
    if (!isBound ())
      return -1;

295 296 297
    return impl.getLocalPort();
  }

298
  /**
299 300
   * Returns the value of the socket's SO_TIMEOUT setting.  If this method
   * returns 0 then SO_TIMEOUT is disabled.
301
   *
302
   * @return The current timeout in milliseconds.
303
   *
304
   * @exception SocketException If an error occurs.
305
   * 
306 307
   * @since 1.1
   */
308 309
  public synchronized int getSoTimeout() throws SocketException
  {
310 311 312
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

Warren Levy committed
313
    Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
314

Warren Levy committed
315 316 317 318
    if (timeout instanceof Integer) 
      return ((Integer)timeout).intValue();
    else
      return 0;
319 320
  }

321
  /**
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
   * Sets the value of the socket's SO_TIMEOUT value.  A value of 0 will
   * disable SO_TIMEOUT.  Any other value is the number of milliseconds
   * a socket read/write will block before timing out.
   *
   * @param timeout The new SO_TIMEOUT value in milliseconds.
   *
   * @exception SocketException If an error occurs.
   *
   * @since 1.1
   */
  public synchronized void setSoTimeout(int timeout) throws SocketException
  {
    if (timeout < 0)
      throw new IllegalArgumentException("Invalid timeout: " + timeout);

    impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  }

  /**
   * This method returns the value of the system level socket option
   * SO_SNDBUF, which is used by the operating system to tune buffer
   * sizes for data transfers.
   *
   * @return The send buffer size.
   *
   * @exception SocketException If an error occurs.
   *
   * @since 1.2
   */
  public int getSendBufferSize() throws SocketException
  {
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

    Object obj = impl.getOption(SocketOptions.SO_SNDBUF);

    if (obj instanceof Integer)
      return(((Integer)obj).intValue());
    else
      throw new SocketException("Unexpected type");
  }

  /**
   * This method sets the value for the system level socket option
   * SO_SNDBUF to the specified value.  Note that valid values for this
   * option are specific to a given operating system.
   *
   * @param size The new send buffer size.
   *
   * @exception SocketException If an error occurs.
   * @exception IllegalArgumentException If size is 0 or negative.
   *
   * @since 1.2
   */
  public void setSendBufferSize(int size) throws SocketException
  {
    if (size < 0)
      throw new IllegalArgumentException("Buffer size is less than 0");
  
    impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
  }

  /**
   * This method returns the value of the system level socket option
   * SO_RCVBUF, which is used by the operating system to tune buffer
   * sizes for data transfers.
   *
   * @return The receive buffer size.
   *
   * @exception SocketException If an error occurs.
392
   *
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
   * @since 1.2
   */
  public int getReceiveBufferSize() throws SocketException
  {
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

    Object obj = impl.getOption(SocketOptions.SO_RCVBUF);
  
    if (obj instanceof Integer)
      return(((Integer)obj).intValue());
    else 
      throw new SocketException("Unexpected type");
  }

  /**
   * This method sets the value for the system level socket option
   * SO_RCVBUF to the specified value.  Note that valid values for this
   * option are specific to a given operating system.
   *
   * @param size The new receive buffer size.
   *
   * @exception SocketException If an error occurs.
   * @exception IllegalArgumentException If size is 0 or negative.
417
   * 
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
   * @since 1.2
   */
  public void setReceiveBufferSize(int size) throws SocketException
  {
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

    if (size < 0)
      throw new IllegalArgumentException("Buffer size is less than 0");

    impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
  }

  /**
   * Connects the datagram socket to a specified address/port.
   *
   * @param address The address to connect to.
   * @param port The port to connect to.
   *
   * @exception SocketException If an error occurs.
   * @exception IllegalArgumentException If address is null
   * or the port number is illegal.
   * @exception SecurityException If the caller is not allowed to send
   * datagrams to and receive datagrams from the address and port.
   *
   * @since 1.2
   */
  public void connect(InetAddress address, int port)
  {
    if (address == null)
      throw new IllegalArgumentException ("Address may not be null");

    if ((port < 1) || (port > 65535))
      throw new IllegalArgumentException ("Port number is illegal");

    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkAccept(address.getHostName (), port);

457 458
    try
      {
459 460 461
    impl.connect (address, port);
    remoteAddress = address;
    remotePort = port;
462 463 464 465
      }
    catch (SocketException e)
      {
      }
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  }

  /**
   * Disconnects the datagram socket.
   *
   * @since 1.2
   */
  public void disconnect()
  {
    impl.disconnect();
  }

  /**
   * Receive a datagram packet.
   *
   * @param p The datagram packet to put the incoming data into.
   * 
   * @exception IOException If an error occurs.
484
   * @exception SocketTimeoutException If setSoTimeout was previously called
485
   * and the timeout has expired.
486 487
   * @exception PortUnreachableException If the socket is connected to a
   * currently unreachable destination. Note, there is no guarantee that the
488
   * exception will be thrown.
489
   * @exception IllegalBlockingModeException If this socket has an associated
490
   * channel, and the channel is in non-blocking mode.
491
   * @exception SecurityException If a security manager exists and its
492
   * checkAccept ethod doesn't allow the receive.
493
   */
494 495
  public synchronized void receive(DatagramPacket p) throws IOException
  {
496 497 498
    if (impl == null)
      throw new IOException ("Cannot initialize Socket implementation");

499 500 501 502
    if (remoteAddress != null && remoteAddress.isMulticastAddress ())
      throw new IOException (
        "Socket connected to a multicast address my not receive");

503 504
    if (ch != null && !ch.isBlocking ())
      throw new IllegalBlockingModeException ();
505 506

    impl.receive(p);
507 508 509 510

    SecurityManager s = System.getSecurityManager();
    if (s != null && isConnected ())
      s.checkAccept (p.getAddress().getHostName (), p.getPort ());
511 512
  }

513
  /**
514
   * Sends a datagram packet.
515
   *
516
   * @param p The datagram packet to send.
517
   *
518
   * @exception IOException If an error occurs.
519
   * @exception SecurityException If a security manager exists and its
520
   * checkMulticast or checkConnect method doesn't allow the send.
521 522
   * @exception PortUnreachableException If the socket is connected to a
   * currently unreachable destination. Note, there is no guarantee that the
523
   * exception will be thrown.
524
   * @exception IllegalBlockingModeException If this socket has an associated
525
   * channel, and the channel is in non-blocking mode.
526
   */
527 528 529 530
  public void send(DatagramPacket p) throws IOException
  {
    // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.
    SecurityManager s = System.getSecurityManager();
531
    if (s != null && !isConnected ())
532
      {
533 534 535 536 537
        InetAddress addr = p.getAddress();
        if (addr.isMulticastAddress())
          s.checkMulticast(addr);
        else
          s.checkConnect(addr.getHostAddress(), p.getPort());
538
      }
539 540 541 542 543 544 545 546

    if (isConnected ())
      {
        if (p.getAddress () != null && (remoteAddress != p.getAddress () ||
                                        remotePort != p.getPort ()))
          throw new IllegalArgumentException (
            "DatagramPacket address does not match remote address" );
      }
547
	    
548 549
    // FIXME: if this is a subclass of MulticastSocket,
    // use getTimeToLive for TTL val.
550 551 552 553

    if (ch != null && !ch.isBlocking ())
      throw new IllegalBlockingModeException ();

554 555 556
    impl.send(p);
  }

557
  /**
558
   * Binds the socket to the given socket address.
559
   *
560
   * @param address The socket address to bind to.
561
   *
562 563 564 565
   * @exception SocketException If an error occurs.
   * @exception SecurityException If a security manager exists and
   * its checkListen method doesn't allow the operation.
   * @exception IllegalArgumentException If address type is not supported.
566
   *
567
   * @since 1.4
568
   */
569 570
  public void bind (SocketAddress address)
    throws SocketException
571
  {
572 573
    if (! (address instanceof InetSocketAddress))
      throw new IllegalArgumentException ();
Warren Levy committed
574

575 576 577 578 579 580 581
    InetSocketAddress tmp = (InetSocketAddress) address;

    SecurityManager s = System.getSecurityManager ();
    if (s != null)
      s.checkListen(tmp.getPort ());

    impl.bind (tmp.getPort (), tmp.getAddress ());
582 583
  }

584
  /**
585
   * Checks if the datagram socket is closed.
586
   *
587
   * @since 1.4
588
   */
589
  public boolean isClosed()
590
  {
591 592
    return !impl.getFileDescriptor().valid();
  }
593

594 595 596 597 598 599 600 601
  /**
   * Returns the datagram channel assoziated with this datagram socket.
   * 
   * @since 1.4
   */
  public DatagramChannel getChannel()
  {
    return ch;
602
  }
603

604
  /**
605 606
   * Connects the datagram socket to a specified socket address.
   *
607
   * @param address The socket address to connect to.
608
   *
609 610
   * @exception SocketException If an error occurs.
   * @exception IllegalArgumentException If address type is not supported.
611 612 613 614 615 616 617 618 619 620 621 622 623 624
   *
   * @since 1.4
   */
  public void connect (SocketAddress address) throws SocketException
  {
    if ( !(address instanceof InetSocketAddress) )
      throw new IllegalArgumentException (
		      "SocketAddress is not InetSocketAddress");

    InetSocketAddress tmp = (InetSocketAddress) address;
    connect( tmp.getAddress(), tmp.getPort());
  }
  
  /**
625
   * Returns the binding state of the socket.
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
   * 
   * @since 1.4
   */
  public boolean isBound()
  {
    try
      {
        Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR);
      }
    catch (SocketException e)
      {
        return false;
      }

    return true;
  }

  /**
644
   * Returns the connection state of the socket.
645 646 647 648 649 650 651 652 653 654
   * 
   * @since 1.4
   */
  public boolean isConnected()
  {
    return remoteAddress != null;
  }

  /**
   * Returns the SocketAddress of the host this socket is conneted to
655
   * or null if this socket is not connected.
656 657 658 659 660 661 662 663 664
   * 
   * @since 1.4
   */
  public SocketAddress getRemoteSocketAddress()
  {
    if (!isConnected ())
      return null;

    return new InetSocketAddress (remoteAddress, remotePort);
665
  }
666

667
  /**
668
   * Returns the local SocketAddress this socket is bound to
669
   * or null if it is not bound.
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
   * 
   * @since 1.4
   */
  public SocketAddress getLocalSocketAddress()
  {
    InetAddress addr;
    
    try
      {
        addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
      }
    catch (SocketException e)
      {
        return null;
      }

    return new InetSocketAddress (addr, impl.localPort);
  }

  /**
690
   * Enables/Disables SO_REUSEADDR.
691
   *
692
   * @param on Whether or not to have SO_REUSEADDR turned on.
693 694 695
   *
   * @exception SocketException If an error occurs.
   *
696 697 698 699
   * @since 1.4
   */
  public void setReuseAddress(boolean on) throws SocketException
  {
700 701 702
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

703 704 705 706
    impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
  }

  /**
707
   * Checks if SO_REUSEADDR is enabled.
708
   *
709
   * @exception SocketException If an error occurs.
710 711 712 713 714
   * 
   * @since 1.4
   */
  public boolean getReuseAddress() throws SocketException
  {
715 716 717
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    Object obj = impl.getOption (SocketOptions.SO_REUSEADDR);
  
    if (obj instanceof Boolean)
      return(((Boolean) obj).booleanValue ());
    else 
      throw new SocketException ("Unexpected type");
  }

  /**
   * Enables/Disables SO_BROADCAST
   * 
   * @param on Whether or not to have SO_BROADCAST turned on
   *
   * @exception SocketException If an error occurs
   *
   * @since 1.4
   */
  public void setBroadcast(boolean on) throws SocketException
  {
737 738 739
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

740 741 742 743 744 745 746 747 748 749 750 751
    impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on));
  }

  /**
   * Checks if SO_BROADCAST is enabled
   * 
   * @exception SocketException If an error occurs
   * 
   * @since 1.4
   */
  public boolean getBroadcast() throws SocketException
  {
752 753 754
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

755 756 757 758 759 760 761 762 763 764 765 766 767 768
    Object obj = impl.getOption (SocketOptions.SO_BROADCAST);
  
    if (obj instanceof Boolean)
      return ((Boolean) obj).booleanValue ();
    else 
      throw new SocketException ("Unexpected type");
  }

  /**
   * Sets the traffic class value
   *
   * @param tc The traffic class
   *
   * @exception SocketException If an error occurs
769
   * @exception IllegalArgumentException If tc value is illegal
770 771 772 773 774 775 776 777
   *
   * @see DatagramSocket:getTrafficClass
   * 
   * @since 1.4
   */
  public void setTrafficClass(int tc)
    throws SocketException
  {
778 779 780
    if (impl == null)
      throw new SocketException ("Cannot initialize Socket implementation");

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
    if (tc < 0 || tc > 255)
      throw new IllegalArgumentException();

    impl.setOption (SocketOptions.IP_TOS, new Integer (tc));
  }
  
  /**
   * Returns the current traffic class
   * 
   * @see DatagramSocket:setTrafficClass
   *
   * @exception SocketException If an error occurs
   * 
   * @since 1.4
   */
  public int getTrafficClass() throws SocketException
  {
798 799 800
    if (impl == null)
      throw new SocketException( "Cannot initialize Socket implementation");

801 802 803 804 805 806 807 808 809
    Object obj = impl.getOption(SocketOptions.IP_TOS);

    if (obj instanceof Integer)
      return ((Integer) obj).intValue ();
    else
      throw new SocketException ("Unexpected type");
  }
  
  /**
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
   * Sets the datagram socket implementation factory for the application
   *
   * @param fac The factory to set
   *
   * @exception IOException If an error occurs
   * @exception SocketException If the factory is already defined
   * @exception SecurityException If a security manager exists and its
   * checkSetFactory method doesn't allow the operation
   */
  public static void setDatagramSocketImplFactory
    (DatagramSocketImplFactory fac) throws IOException
  {
    if (factory != null)
      throw new SocketException ("DatagramSocketImplFactory already defined");

    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkSetFactory();

    factory = fac;
  }
831
}