39 |
|
|
40 |
import java.io.IOException; |
import java.io.IOException; |
41 |
import java.nio.channels.DatagramChannel; |
import java.nio.channels.DatagramChannel; |
42 |
|
import java.nio.channels.IllegalBlockingModeException; |
43 |
|
|
44 |
/** |
/** |
45 |
* Written using on-line Java Platform 1.2 API Specification, as well |
* Written using on-line Java Platform 1.2 API Specification, as well |
62 |
public class DatagramSocket |
public class DatagramSocket |
63 |
{ |
{ |
64 |
/** |
/** |
65 |
|
* This is the user DatagramSocketImplFactory for this class. If this |
66 |
|
* variable is null, a default factory is used. |
67 |
|
*/ |
68 |
|
static DatagramSocketImplFactory factory; |
69 |
|
|
70 |
|
/** |
71 |
* This is the implementation object used by this socket. |
* This is the implementation object used by this socket. |
72 |
*/ |
*/ |
73 |
DatagramSocketImpl impl; |
DatagramSocketImpl impl; |
471 |
impl.send(p); |
impl.send(p); |
472 |
} |
} |
473 |
|
|
474 |
} // class DatagramSocket |
/** |
475 |
|
* Binds the socket to the given socket address. |
476 |
|
* |
477 |
|
* @param address The socket address to bind to. |
478 |
|
* |
479 |
|
* @exception SocketException If an error occurs. |
480 |
|
* @exception SecurityException If a security manager exists and |
481 |
|
* its checkListen method doesn't allow the operation. |
482 |
|
* @exception IllegalArgumentException If address type is not supported. |
483 |
|
* |
484 |
|
* @since 1.4 |
485 |
|
*/ |
486 |
|
public void bind (SocketAddress address) |
487 |
|
throws SocketException |
488 |
|
{ |
489 |
|
if (! (address instanceof InetSocketAddress)) |
490 |
|
throw new IllegalArgumentException (); |
491 |
|
|
492 |
|
InetSocketAddress tmp = (InetSocketAddress) address; |
493 |
|
|
494 |
|
SecurityManager s = System.getSecurityManager (); |
495 |
|
if (s != null) |
496 |
|
s.checkListen(tmp.getPort ()); |
497 |
|
|
498 |
|
impl.bind (tmp.getPort (), tmp.getAddress ()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
|
* Returns the datagram channel assoziated with this datagram socket. |
503 |
|
* |
504 |
|
* @since 1.4 |
505 |
|
*/ |
506 |
|
public DatagramChannel getChannel() |
507 |
|
{ |
508 |
|
return ch; |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
|
* Connects the datagram socket to a specified socket address. |
513 |
|
* |
514 |
|
* @param address The socket address to connect to. |
515 |
|
* |
516 |
|
* @exception SocketException If an error occurs. |
517 |
|
* @exception IllegalArgumentException If address type is not supported. |
518 |
|
* |
519 |
|
* @since 1.4 |
520 |
|
*/ |
521 |
|
public void connect (SocketAddress address) throws SocketException |
522 |
|
{ |
523 |
|
if ( !(address instanceof InetSocketAddress) ) |
524 |
|
throw new IllegalArgumentException ( |
525 |
|
"SocketAddress is not InetSocketAddress"); |
526 |
|
|
527 |
|
InetSocketAddress tmp = (InetSocketAddress) address; |
528 |
|
connect( tmp.getAddress(), tmp.getPort()); |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
|
* Returns the binding state of the socket. |
533 |
|
* |
534 |
|
* @since 1.4 |
535 |
|
*/ |
536 |
|
public boolean isBound() |
537 |
|
{ |
538 |
|
try |
539 |
|
{ |
540 |
|
Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR); |
541 |
|
} |
542 |
|
catch (SocketException e) |
543 |
|
{ |
544 |
|
return false; |
545 |
|
} |
546 |
|
|
547 |
|
return true; |
548 |
|
} |
549 |
|
|
550 |
|
/** |
551 |
|
* Returns the connection state of the socket. |
552 |
|
* |
553 |
|
* @since 1.4 |
554 |
|
*/ |
555 |
|
public boolean isConnected() |
556 |
|
{ |
557 |
|
return remote_addr != null; |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
|
* Returns the SocketAddress of the host this socket is conneted to |
562 |
|
* or null if this socket is not connected. |
563 |
|
* |
564 |
|
* @since 1.4 |
565 |
|
*/ |
566 |
|
public SocketAddress getRemoteSocketAddress() |
567 |
|
{ |
568 |
|
if (!isConnected ()) |
569 |
|
return null; |
570 |
|
|
571 |
|
return new InetSocketAddress (remote_addr, remote_port); |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
|
* Returns the local SocketAddress this socket is bound to |
576 |
|
* or null if it is not bound. |
577 |
|
* |
578 |
|
* @since 1.4 |
579 |
|
*/ |
580 |
|
public SocketAddress getLocalSocketAddress() |
581 |
|
{ |
582 |
|
InetAddress addr; |
583 |
|
|
584 |
|
try |
585 |
|
{ |
586 |
|
addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR); |
587 |
|
} |
588 |
|
catch (SocketException e) |
589 |
|
{ |
590 |
|
return null; |
591 |
|
} |
592 |
|
|
593 |
|
return new InetSocketAddress (local_addr, impl.localPort); |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
|
* Enables/Disables SO_REUSEADDR. |
598 |
|
* |
599 |
|
* @param on Whether or not to have SO_REUSEADDR turned on. |
600 |
|
* |
601 |
|
* @exception SocketException If an error occurs. |
602 |
|
* |
603 |
|
* @since 1.4 |
604 |
|
*/ |
605 |
|
public void setReuseAddress(boolean on) throws SocketException |
606 |
|
{ |
607 |
|
if (impl == null) |
608 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
609 |
|
|
610 |
|
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); |
611 |
|
} |
612 |
|
|
613 |
|
/** |
614 |
|
* Checks if SO_REUSEADDR is enabled. |
615 |
|
* |
616 |
|
* @exception SocketException If an error occurs. |
617 |
|
* |
618 |
|
* @since 1.4 |
619 |
|
*/ |
620 |
|
public boolean getReuseAddress() throws SocketException |
621 |
|
{ |
622 |
|
if (impl == null) |
623 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
624 |
|
|
625 |
|
Object obj = impl.getOption (SocketOptions.SO_REUSEADDR); |
626 |
|
|
627 |
|
if (obj instanceof Boolean) |
628 |
|
return(((Boolean) obj).booleanValue ()); |
629 |
|
else |
630 |
|
throw new SocketException ("Unexpected type"); |
631 |
|
} |
632 |
|
|
633 |
|
/** |
634 |
|
* Enables/Disables SO_BROADCAST |
635 |
|
* |
636 |
|
* @param on Whether or not to have SO_BROADCAST turned on |
637 |
|
* |
638 |
|
* @exception SocketException If an error occurs |
639 |
|
* |
640 |
|
* @since 1.4 |
641 |
|
*/ |
642 |
|
public void setBroadcast(boolean on) throws SocketException |
643 |
|
{ |
644 |
|
if (impl == null) |
645 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
646 |
|
|
647 |
|
impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on)); |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
|
* Checks if SO_BROADCAST is enabled |
652 |
|
* |
653 |
|
* @exception SocketException If an error occurs |
654 |
|
* |
655 |
|
* @since 1.4 |
656 |
|
*/ |
657 |
|
public boolean getBroadcast() throws SocketException |
658 |
|
{ |
659 |
|
if (impl == null) |
660 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
661 |
|
|
662 |
|
Object obj = impl.getOption (SocketOptions.SO_BROADCAST); |
663 |
|
|
664 |
|
if (obj instanceof Boolean) |
665 |
|
return ((Boolean) obj).booleanValue (); |
666 |
|
else |
667 |
|
throw new SocketException ("Unexpected type"); |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
|
* Sets the traffic class value |
672 |
|
* |
673 |
|
* @param tc The traffic class |
674 |
|
* |
675 |
|
* @exception SocketException If an error occurs |
676 |
|
* @exception IllegalArgumentException If tc value is illegal |
677 |
|
* |
678 |
|
* @see DatagramSocket:getTrafficClass |
679 |
|
* |
680 |
|
* @since 1.4 |
681 |
|
*/ |
682 |
|
public void setTrafficClass(int tc) |
683 |
|
throws SocketException |
684 |
|
{ |
685 |
|
if (impl == null) |
686 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
687 |
|
|
688 |
|
if (tc < 0 || tc > 255) |
689 |
|
throw new IllegalArgumentException(); |
690 |
|
|
691 |
|
impl.setOption (SocketOptions.IP_TOS, new Integer (tc)); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
|
* Returns the current traffic class |
696 |
|
* |
697 |
|
* @see DatagramSocket:setTrafficClass |
698 |
|
* |
699 |
|
* @exception SocketException If an error occurs |
700 |
|
* |
701 |
|
* @since 1.4 |
702 |
|
*/ |
703 |
|
public int getTrafficClass() throws SocketException |
704 |
|
{ |
705 |
|
if (impl == null) |
706 |
|
throw new SocketException( "Cannot initialize Socket implementation"); |
707 |
|
|
708 |
|
Object obj = impl.getOption(SocketOptions.IP_TOS); |
709 |
|
|
710 |
|
if (obj instanceof Integer) |
711 |
|
return ((Integer) obj).intValue (); |
712 |
|
else |
713 |
|
throw new SocketException ("Unexpected type"); |
714 |
|
} |
715 |
|
|
716 |
|
/** |
717 |
|
* Sets the datagram socket implementation factory for the application |
718 |
|
* |
719 |
|
* @param fac The factory to set |
720 |
|
* |
721 |
|
* @exception IOException If an error occurs |
722 |
|
* @exception SocketException If the factory is already defined |
723 |
|
* @exception SecurityException If a security manager exists and its |
724 |
|
* checkSetFactory method doesn't allow the operation |
725 |
|
*/ |
726 |
|
public static void setDatagramSocketImplFactory |
727 |
|
(DatagramSocketImplFactory fac) throws IOException |
728 |
|
{ |
729 |
|
if (factory != null) |
730 |
|
throw new SocketException ("DatagramSocketImplFactory already defined"); |
731 |
|
|
732 |
|
SecurityManager sm = System.getSecurityManager(); |
733 |
|
if (sm != null) |
734 |
|
sm.checkSetFactory(); |
735 |
|
|
736 |
|
factory = fac; |
737 |
|
} |
738 |
|
} // class DatagramSocket |