362 |
} |
} |
363 |
|
|
364 |
/** |
/** |
365 |
|
* If the socket is already bound this returns the local SocketAddress, |
366 |
|
* otherwise null |
367 |
|
* |
368 |
|
* @since 1.4 |
369 |
|
*/ |
370 |
|
public SocketAddress getLocalSocketAddress() |
371 |
|
{ |
372 |
|
InetAddress addr = getLocalAddress (); |
373 |
|
|
374 |
|
if (addr == null) |
375 |
|
return null; |
376 |
|
|
377 |
|
return new InetSocketAddress (addr, impl.getLocalPort()); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
381 |
|
* If the socket is already connected this returns the remote SocketAddress, |
382 |
|
* otherwise null |
383 |
|
* |
384 |
|
* @since 1.4 |
385 |
|
*/ |
386 |
|
public SocketAddress getRemoteSocketAddress() |
387 |
|
{ |
388 |
|
if (!isConnected ()) |
389 |
|
return null; |
390 |
|
|
391 |
|
return new InetSocketAddress (impl.getInetAddress (), impl.getPort ()); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
* Returns an InputStream for reading from this socket. |
* Returns an InputStream for reading from this socket. |
396 |
* |
* |
397 |
* @return The InputStream object |
* @return The InputStream object |
522 |
} |
} |
523 |
|
|
524 |
/** |
/** |
525 |
|
* Enables/disables the SO_OOBINLINE option |
526 |
|
* |
527 |
|
* @param on True if SO_OOBLINE should be enabled |
528 |
|
* |
529 |
|
* @exception SocketException If an error occurs |
530 |
|
* |
531 |
|
* @since 1.4 |
532 |
|
*/ |
533 |
|
public void setOOBInline (boolean on) throws SocketException |
534 |
|
{ |
535 |
|
if (impl == null) |
536 |
|
throw new SocketException("Not connected"); |
537 |
|
|
538 |
|
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on)); |
539 |
|
} |
540 |
|
|
541 |
|
/** |
542 |
|
* Returns the current setting of the SO_OOBINLINE option for this socket |
543 |
|
* |
544 |
|
* @exception SocketException If an error occurs |
545 |
|
* |
546 |
|
* @since 1.4 |
547 |
|
*/ |
548 |
|
public boolean getOOBInline () throws SocketException |
549 |
|
{ |
550 |
|
if (impl == null) |
551 |
|
throw new SocketException("Not connected"); |
552 |
|
|
553 |
|
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE); |
554 |
|
|
555 |
|
if (buf instanceof Boolean) |
556 |
|
return(((Boolean)buf).booleanValue()); |
557 |
|
else |
558 |
|
throw new SocketException("Internal Error: Unexpected type"); |
559 |
|
} |
560 |
|
|
561 |
|
/** |
562 |
* Sets the value of the SO_TIMEOUT option on the socket. If this value |
* Sets the value of the SO_TIMEOUT option on the socket. If this value |
563 |
* is set, and an read/write is performed that does not complete within |
* is set, and an read/write is performed that does not complete within |
564 |
* the timeout period, a short count is returned (or an EWOULDBLOCK signal |
* the timeout period, a short count is returned (or an EWOULDBLOCK signal |
706 |
} |
} |
707 |
|
|
708 |
/** |
/** |
709 |
|
* This method sets the value for the socket level socket option |
710 |
|
* SO_KEEPALIVE. |
711 |
|
* |
712 |
|
* @param on True if SO_KEEPALIVE should be enabled |
713 |
|
* |
714 |
|
* @exception SocketException If an error occurs or Socket is not connected |
715 |
|
* |
716 |
|
* @since Java 1.3 |
717 |
|
*/ |
718 |
|
public void setKeepAlive (boolean on) throws SocketException |
719 |
|
{ |
720 |
|
if (impl == null) |
721 |
|
throw new SocketException("Not connected"); |
722 |
|
|
723 |
|
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on)); |
724 |
|
} |
725 |
|
|
726 |
|
/** |
727 |
|
* This method returns the value of the socket level socket option |
728 |
|
* SO_KEEPALIVE. |
729 |
|
* |
730 |
|
* @return The setting |
731 |
|
* |
732 |
|
* @exception SocketException If an error occurs or Socket is not connected |
733 |
|
* |
734 |
|
* @since Java 1.3 |
735 |
|
*/ |
736 |
|
public boolean getKeepAlive () throws SocketException |
737 |
|
{ |
738 |
|
if (impl == null) |
739 |
|
throw new SocketException("Not connected"); |
740 |
|
|
741 |
|
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE); |
742 |
|
|
743 |
|
if (buf instanceof Boolean) |
744 |
|
return(((Boolean)buf).booleanValue()); |
745 |
|
else |
746 |
|
throw new SocketException("Internal Error: Unexpected type"); |
747 |
|
} |
748 |
|
|
749 |
|
/** |
750 |
* Closes the socket. |
* Closes the socket. |
751 |
* |
* |
752 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
826 |
{ |
{ |
827 |
return ch; |
return ch; |
828 |
} |
} |
829 |
|
|
830 |
|
/** |
831 |
|
* Checks if the SO_REUSEADDR option is enabled |
832 |
|
* |
833 |
|
* @exception SocketException If an error occurs |
834 |
|
* |
835 |
|
* @since 1.4 |
836 |
|
*/ |
837 |
|
public boolean getReuseAddress () throws SocketException |
838 |
|
{ |
839 |
|
if (impl == null) |
840 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
841 |
|
|
842 |
|
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); |
843 |
|
|
844 |
|
if (!(reuseaddr instanceof Boolean)) |
845 |
|
throw new SocketException ("Internal Error"); |
846 |
|
|
847 |
|
return ((Boolean) reuseaddr).booleanValue (); |
848 |
|
} |
849 |
|
|
850 |
|
/** |
851 |
|
* Enables/Disables the SO_REUSEADDR option |
852 |
|
* |
853 |
|
* @exception SocketException If an error occurs |
854 |
|
* |
855 |
|
* @since 1.4 |
856 |
|
*/ |
857 |
|
public void setReuseAddress (boolean on) throws SocketException |
858 |
|
{ |
859 |
|
if (impl == null) |
860 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
861 |
|
|
862 |
|
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); |
863 |
|
} |
864 |
|
|
865 |
|
/** |
866 |
|
* Returns the current traffic class |
867 |
|
* |
868 |
|
* @exception SocketException If an error occurs |
869 |
|
* |
870 |
|
* @see Socket:setTrafficClass |
871 |
|
* |
872 |
|
* @since 1.4 |
873 |
|
*/ |
874 |
|
public int getTrafficClass () throws SocketException |
875 |
|
{ |
876 |
|
if (impl == null) |
877 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
878 |
|
|
879 |
|
Object obj = impl.getOption(SocketOptions.IP_TOS); |
880 |
|
|
881 |
|
if (obj instanceof Integer) |
882 |
|
return ((Integer) obj).intValue (); |
883 |
|
else |
884 |
|
throw new SocketException ("Unexpected type"); |
885 |
|
} |
886 |
|
|
887 |
|
/** |
888 |
|
* Sets the traffic class value |
889 |
|
* |
890 |
|
* @param tc The traffic class |
891 |
|
* |
892 |
|
* @exception SocketException If an error occurs |
893 |
|
* @exception IllegalArgumentException If tc value is illegal |
894 |
|
* |
895 |
|
* @see Socket:getTrafficClass |
896 |
|
* |
897 |
|
* @since 1.4 |
898 |
|
*/ |
899 |
|
public void setTrafficClass (int tc) throws SocketException |
900 |
|
{ |
901 |
|
if (impl == null) |
902 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
903 |
|
|
904 |
|
if (tc < 0 || tc > 255) |
905 |
|
throw new IllegalArgumentException(); |
906 |
|
|
907 |
|
impl.setOption (SocketOptions.IP_TOS, new Integer (tc)); |
908 |
|
} |
909 |
|
|
910 |
|
/** |
911 |
|
* Checks if the socket is connected |
912 |
|
*/ |
913 |
|
public boolean isConnected () |
914 |
|
{ |
915 |
|
return impl.getInetAddress () != null; |
916 |
|
} |
917 |
|
|
918 |
|
/** |
919 |
|
* Checks if the socket is already bound. |
920 |
|
*/ |
921 |
|
public boolean isBound () |
922 |
|
{ |
923 |
|
return getLocalAddress () != null; |
924 |
|
} |
925 |
} |
} |