2002-09-25 Michael Koch <konqueror@gmx.de>
[gcc.git] / libjava / java / net / Socket.java
1 /* Socket.java -- Client socket implementation
2 Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package java.net;
39
40 import java.io.*;
41 import java.nio.channels.SocketChannel;
42
43 /* Written using on-line Java Platform 1.2 API Specification.
44 * Status: I believe all methods are implemented.
45 */
46
47 /**
48 * This class models a client site socket. A socket is a TCP/IP endpoint
49 * for network communications conceptually similar to a file handle.
50 * <p>
51 * This class does not actually do any work. Instead, it redirects all of
52 * its calls to a socket implementation object which implements the
53 * <code>SocketImpl</code> interface. The implementation class is
54 * instantiated by factory class that implements the
55 * <code>SocketImplFactory interface</code>. A default
56 * factory is provided, however the factory may be set by a call to
57 * the <code>setSocketImplFactory</code> method. Note that this may only be
58 * done once per virtual machine. If a subsequent attempt is made to set the
59 * factory, a <code>SocketException</code> will be thrown.
60 *
61 * @author Aaron M. Renn (arenn@urbanophile.com)
62 * @author Per Bothner (bothner@cygnus.com)
63 */
64 public class Socket
65 {
66
67 // Class Variables
68
69 /**
70 * This is the user SocketImplFactory for this class. If this variable is
71 * null, a default factory is used.
72 */
73 static SocketImplFactory factory;
74
75 // Instance Variables
76
77 /**
78 * The implementation object to which calls are redirected
79 */
80 SocketImpl impl;
81
82 SocketChannel ch; // this field must have been set if created by SocketChannel
83
84 // Constructors
85
86 /**
87 * Initializes a new instance of <code>Socket</code> object without
88 * connecting to a remote host. This useful for subclasses of socket that
89 * might want this behavior.
90 *
91 * @specnote This constructor is public since JDK 1.4
92 */
93 public Socket ()
94 {
95 if (factory != null)
96 impl = factory.createSocketImpl();
97 else
98 impl = new PlainSocketImpl();
99 }
100
101 /**
102 * Initializes a new instance of <code>Socket</code> object without
103 * connecting to a remote host. This is useful for subclasses of socket
104 * that might want this behavior.
105 * <p>
106 * Additionally, this socket will be created using the supplied
107 * implementation class instead the default class or one returned by a
108 * factory. This value can be <code>null</code>, but if it is, all instance
109 * methods in <code>Socket</code> should be overridden because most of them
110 * rely on this value being populated.
111 *
112 * @param impl The <code>SocketImpl</code> to use for this
113 * <code>Socket</code>
114 *
115 * @exception SocketException If an error occurs
116 */
117 protected Socket (SocketImpl impl) throws SocketException
118 {
119 this.impl = impl;
120 }
121
122 /**
123 * Initializes a new instance of <code>Socket</code> and connects to the
124 * hostname and port specified as arguments.
125 *
126 * @param host The name of the host to connect to
127 * @param port The port number to connect to
128 *
129 * @exception UnknownHostException If the hostname cannot be resolved to a
130 * network address.
131 * @exception IOException If an error occurs
132 * @exception SecurityException If a security manager exists and its
133 * checkConnect method doesn't allow the operation
134 */
135 public Socket (String host, int port)
136 throws UnknownHostException, IOException
137 {
138 this(InetAddress.getByName(host), port, null, 0, true);
139 }
140
141 /**
142 * Initializes a new instance of <code>Socket</code> and connects to the
143 * address and port number specified as arguments.
144 *
145 * @param address The address to connect to
146 * @param port The port number to connect to
147 *
148 * @exception IOException If an error occurs
149 * @exception SecurityException If a security manager exists and its
150 * checkConnect method doesn't allow the operation
151 */
152 public Socket (InetAddress address, int port)
153 throws IOException
154 {
155 this(address, port, null, 0, true);
156 }
157
158 /**
159 * Initializes a new instance of <code>Socket</code> that connects to the
160 * named host on the specified port and binds to the specified local address
161 * and port.
162 *
163 * @param host The name of the remote host to connect to.
164 * @param port The remote port to connect to.
165 * @param loadAddr The local address to bind to.
166 * @param localPort The local port to bind to.
167 *
168 * @exception SecurityException If the <code>SecurityManager</code>
169 * exists and does not allow a connection to the specified host/port or
170 * binding to the specified local host/port.
171 * @exception IOException If a connection error occurs.
172 */
173 public Socket (String host, int port,
174 InetAddress localAddr, int localPort) throws IOException
175 {
176 this(InetAddress.getByName(host), port, localAddr, localPort, true);
177 }
178
179 /**
180 * Initializes a new instance of <code>Socket</code> and connects to the
181 * address and port number specified as arguments, plus binds to the
182 * specified local address and port.
183 *
184 * @param address The remote address to connect to
185 * @param port The remote port to connect to
186 * @param localAddr The local address to connect to
187 * @param localPort The local port to connect to
188 *
189 * @exception IOException If an error occurs
190 * @exception SecurityException If a security manager exists and its
191 * checkConnect method doesn't allow the operation
192 */
193 public Socket (InetAddress address, int port,
194 InetAddress localAddr, int localPort) throws IOException
195 {
196 this(address, port, localAddr, localPort, true);
197 }
198
199 /**
200 * Initializes a new instance of <code>Socket</code> and connects to the
201 * hostname and port specified as arguments. If the stream argument is set
202 * to <code>true</code>, then a stream socket is created. If it is
203 * <code>false</code>, a datagram socket is created.
204 *
205 * @param host The name of the host to connect to
206 * @param port The port to connect to
207 * @param stream <code>true</code> for a stream socket, <code>false</code>
208 * for a datagram socket
209 *
210 * @exception IOException If an error occurs
211 * @exception SecurityException If a security manager exists and its
212 * checkConnect method doesn't allow the operation
213 *
214 * @deprecated Use the <code>DatagramSocket</code> class to create
215 * datagram oriented sockets.
216 */
217 public Socket (String host, int port, boolean stream) throws IOException
218 {
219 this(InetAddress.getByName(host), port, null, 0, stream);
220 }
221
222 /**
223 * Initializes a new instance of <code>Socket</code> and connects to the
224 * address and port number specified as arguments. If the stream param is
225 * <code>true</code>, a stream socket will be created, otherwise a datagram
226 * socket is created.
227 *
228 * @param host The address to connect to
229 * @param port The port number to connect to
230 * @param stream <code>true</code> to create a stream socket,
231 * <code>false</code> to create a datagram socket.
232 *
233 * @exception IOException If an error occurs
234 * @exception SecurityException If a security manager exists and its
235 * checkConnect method doesn't allow the operation
236 *
237 * @deprecated Use the <code>DatagramSocket</code> class to create
238 * datagram oriented sockets.
239 */
240 public Socket (InetAddress host, int port, boolean stream) throws IOException
241 {
242 this(host, port, null, 0, stream);
243 }
244
245 /**
246 * This constructor is where the real work takes place. Connect to the
247 * specified address and port. Use default local values if not specified,
248 * otherwise use the local host and port passed in. Create as stream or
249 * datagram based on "stream" argument.
250 * <p>
251 *
252 * @param raddr The remote address to connect to
253 * @param rport The remote port to connect to
254 * @param laddr The local address to connect to
255 * @param lport The local port to connect to
256 * @param stream true for a stream socket, false for a datagram socket
257 *
258 * @exception IOException If an error occurs
259 * @exception SecurityException If a security manager exists and its
260 * checkConnect method doesn't allow the operation
261 */
262 private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport,
263 boolean stream) throws IOException
264 {
265 this();
266 if (impl == null)
267 throw new IOException("Cannot initialize Socket implementation");
268
269 SecurityManager sm = System.getSecurityManager();
270 if (sm != null)
271 sm.checkConnect(raddr.getHostName(), rport);
272
273 impl.create(stream);
274
275 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
276 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
277 // that default. JDK 1.2 doc infers not to do a bind.
278 if (laddr != null)
279 impl.bind(laddr, lport);
280
281 if (raddr != null)
282 impl.connect(raddr, rport);
283 }
284
285 /**
286 * Binds the socket to the givent local address/port
287 *
288 * @param bindpoint The address/port to bind to
289 *
290 * @exception IOException If an error occurs
291 * @exception SecurityException If a security manager exists and its
292 * checkConnect method doesn't allow the operation
293 * @exception IllegalArgumentException If the address type is not supported
294 *
295 * @since 1.4
296 */
297 public void bind (SocketAddress bindpoint) throws IOException
298 {
299 if ( !(bindpoint instanceof InetSocketAddress))
300 throw new IllegalArgumentException ();
301
302 InetSocketAddress tmp = (InetSocketAddress) bindpoint;
303 impl.bind (tmp.getAddress(), tmp.getPort());
304 }
305
306 /**
307 * Connects the socket with a remote address.
308 *
309 * @param endpoint The address to connect to
310 *
311 * @exception IOException If an error occurs
312 * @exception IllegalArgumentException If the addess type is not supported
313 * @exception IllegalBlockingModeException FIXME
314 *
315 * @since 1.4
316 */
317 public void connect (SocketAddress endpoint)
318 throws IOException
319 {
320 if (! (endpoint instanceof InetSocketAddress))
321 throw new IllegalArgumentException ("Address type not supported");
322
323 impl.connect (endpoint, 0);
324 }
325
326 /**
327 * Connects the socket with a remote address. A timeout of zero is
328 * interpreted as an infinite timeout. The connection will then block
329 * until established or an error occurs.
330 *
331 * @param endpoint The address to connect to
332 *
333 * @exception IOException If an error occurs
334 * @exception IllegalArgumentException If the address type is not supported
335 * @exception IllegalBlockingModeException FIXME
336 * @exception SocketTimeoutException If the timeout is reached
337 *
338 * @since 1.4
339 */
340 public void connect (SocketAddress endpoint, int timeout)
341 throws IOException
342 {
343 if (! (endpoint instanceof InetSocketAddress))
344 throw new IllegalArgumentException ("Address type not supported");
345
346 impl.connect (endpoint, timeout);
347 }
348
349 /**
350 * Returns the address of the remote end of the socket. If this socket
351 * is not connected, then <code>null</code> is returned.
352 *
353 * @return The remote address this socket is connected to
354 */
355 public InetAddress getInetAddress ()
356 {
357 if (impl != null)
358 return impl.getInetAddress();
359
360 return null;
361 }
362
363 /**
364 * Returns the local address to which this socket is bound. If this socket
365 * is not connected, then <code>null</code> is returned.
366 *
367 * @return The local address
368 */
369 public InetAddress getLocalAddress ()
370 {
371 if (impl == null)
372 return null;
373
374 InetAddress addr = null;
375 try
376 {
377 addr = (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
378 }
379 catch(SocketException e)
380 {
381 // (hopefully) shouldn't happen
382 // throw new java.lang.InternalError
383 // ("Error in PlainSocketImpl.getOption");
384 return null;
385 }
386
387 // FIXME: According to libgcj, checkConnect() is supposed to be called
388 // before performing this operation. Problems: 1) We don't have the
389 // addr until after we do it, so we do a post check. 2). The docs I
390 // see don't require this in the Socket case, only DatagramSocket, but
391 // we'll assume they mean both.
392 SecurityManager sm = System.getSecurityManager();
393 if (sm != null)
394 sm.checkConnect(addr.getHostName(), getLocalPort());
395
396 return addr;
397 }
398
399 /**
400 * Returns the port number of the remote end of the socket connection. If
401 * this socket is not connected, then -1 is returned.
402 *
403 * @return The remote port this socket is connected to
404 */
405 public int getPort ()
406 {
407 if (impl != null)
408 return impl.getPort();
409
410 return -1;
411 }
412
413 /**
414 * Returns the local port number to which this socket is bound. If this
415 * socket is not connected, then -1 is returned.
416 *
417 * @return The local port
418 */
419 public int getLocalPort ()
420 {
421 if (impl != null)
422 return impl.getLocalPort();
423
424 return -1;
425 }
426
427 /**
428 * If the socket is already bound this returns the local SocketAddress,
429 * otherwise null
430 *
431 * @since 1.4
432 */
433 public SocketAddress getLocalSocketAddress()
434 {
435 InetAddress addr;
436
437 try
438 {
439 addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
440 }
441 catch (SocketException e)
442 {
443 return null;
444 }
445
446 return new InetSocketAddress (addr, impl.getLocalPort());
447 }
448
449 /**
450 * If the socket is already connected this returns the remote SocketAddress,
451 * otherwise null
452 *
453 * @since 1.4
454 */
455 public SocketAddress getRemoteSocketAddress()
456 {
457 // FIXME: Implement this
458 return null;
459 }
460
461 /**
462 * Returns an InputStream for reading from this socket.
463 *
464 * @return The InputStream object
465 *
466 * @exception IOException If an error occurs or Socket is not connected
467 */
468 public InputStream getInputStream () throws IOException
469 {
470 if (impl != null)
471 return(impl.getInputStream());
472
473 throw new IOException("Not connected");
474 }
475
476 /**
477 * Returns an OutputStream for writing to this socket.
478 *
479 * @return The OutputStream object
480 *
481 * @exception IOException If an error occurs or Socket is not connected
482 */
483 public OutputStream getOutputStream () throws IOException
484 {
485 if (impl != null)
486 return impl.getOutputStream();
487
488 throw new IOException("Not connected");
489 }
490
491 /**
492 * Sets the TCP_NODELAY option on the socket.
493 *
494 * @param on true to enable, false to disable
495 *
496 * @exception SocketException If an error occurs or Socket is not connected
497 */
498 public void setTcpNoDelay (boolean on) throws SocketException
499 {
500 if (impl == null)
501 throw new SocketException("Not connected");
502
503 impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
504 }
505
506 /**
507 * Tests whether or not the TCP_NODELAY option is set on the socket.
508 * Returns true if enabled, false if disabled. When on it disables the
509 * Nagle algorithm which means that packets are always send immediatly and
510 * never merged together to reduce network trafic.
511 *
512 * @return Whether or not TCP_NODELAY is set
513 *
514 * @exception SocketException If an error occurs or Socket not connected
515 */
516 public boolean getTcpNoDelay() throws SocketException
517 {
518 if (impl == null)
519 throw new SocketException("Not connected");
520
521 Object on = impl.getOption(SocketOptions.TCP_NODELAY);
522
523 if (on instanceof Boolean)
524 return(((Boolean)on).booleanValue());
525 else
526 throw new SocketException("Internal Error");
527 }
528
529 /**
530 * Sets the value of the SO_LINGER option on the socket. If the
531 * SO_LINGER option is set on a socket and there is still data waiting to
532 * be sent when the socket is closed, then the close operation will block
533 * until either that data is delivered or until the timeout period
534 * expires. The linger interval is specified in hundreths of a second
535 * (platform specific?)
536 *
537 * @param on true to enable SO_LINGER, false to disable
538 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
539 * SO_LINGER not set.
540 *
541 * @exception SocketException If an error occurs or Socket not connected
542 * @exception IllegalArgumentException If linger is negative
543 */
544 public void setSoLinger(boolean on, int linger) throws SocketException
545 {
546 if (impl == null)
547 throw new SocketException("No socket created");
548
549 if (on == true)
550 {
551 if (linger < 0)
552 throw new IllegalArgumentException("SO_LINGER must be >= 0");
553
554 if (linger > 65535)
555 linger = 65535;
556
557 impl.setOption(SocketOptions.SO_LINGER, new Integer(linger));
558 }
559 else
560 {
561 impl.setOption(SocketOptions.SO_LINGER, new Boolean(false));
562 }
563 }
564
565 /**
566 * Returns the value of the SO_LINGER option on the socket. If the
567 * SO_LINGER option is set on a socket and there is still data waiting to
568 * be sent when the socket is closed, then the close operation will block
569 * until either that data is delivered or until the timeout period
570 * expires. This method either returns the timeouts (in hundredths of
571 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
572 * SO_LINGER is not set.
573 *
574 * @return The SO_LINGER timeout in hundreths of a second or -1
575 * if SO_LINGER not set
576 *
577 * @exception SocketException If an error occurs or Socket is not connected
578 */
579 public int getSoLinger() throws SocketException
580 {
581 if (impl == null)
582 throw new SocketException("Not connected");
583
584 Object linger = impl.getOption(SocketOptions.SO_LINGER);
585 if (linger instanceof Integer)
586 return(((Integer)linger).intValue());
587 else
588 return -1;
589 }
590
591 /**
592 * Sends urgent data through the socket
593 *
594 * @param data The data to send.
595 * Only the lowest eight bits of data are sent
596 *
597 * @exception IOException If an error occurs
598 *
599 * @since 1.4
600 */
601 public void sendUrgentData (int data) throws IOException
602 {
603 impl.sendUrgentData (data);
604 }
605
606 /**
607 * Enables/disables the SO_OOBINLINE option
608 *
609 * @param on True if SO_OOBLINE should be enabled
610 *
611 * @exception SocketException If an error occurs
612 *
613 * @since 1.4
614 */
615 public void setOOBInline (boolean on) throws SocketException
616 {
617 if (impl == null)
618 throw new SocketException("Not connected");
619
620 impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
621 }
622
623 /**
624 * Returns the current setting of the SO_OOBINLINE option for this socket
625 *
626 * @exception SocketException If an error occurs
627 *
628 * @since 1.4
629 */
630 public boolean getOOBInline () throws SocketException
631 {
632 if (impl == null)
633 throw new SocketException("Not connected");
634
635 Object buf = impl.getOption(SocketOptions.SO_OOBINLINE);
636
637 if (buf instanceof Boolean)
638 return(((Boolean)buf).booleanValue());
639 else
640 throw new SocketException("Internal Error: Unexpected type");
641 }
642
643 /**
644 * Sets the value of the SO_TIMEOUT option on the socket. If this value
645 * is set, and an read/write is performed that does not complete within
646 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
647 * would be sent in Unix if no data had been read). A value of 0 for
648 * this option implies that there is no timeout (ie, operations will
649 * block forever). On systems that have separate read and write timeout
650 * values, this method returns the read timeout. This
651 * value is in thousandths of a second (****????*****)
652 *
653 * @param timeout The length of the timeout in thousandth's of a second or
654 * 0 if not set
655 *
656 * @exception SocketException If an error occurs or Socket not connected
657 */
658 public synchronized void setSoTimeout (int timeout) throws SocketException
659 {
660 if (impl == null)
661 throw new SocketException("Not connected");
662
663 if (timeout < 0)
664 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
665
666 impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
667 }
668
669 /**
670 * Returns the value of the SO_TIMEOUT option on the socket. If this value
671 * is set, and an read/write is performed that does not complete within
672 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
673 * would be sent in Unix if no data had been read). A value of 0 for
674 * this option implies that there is no timeout (ie, operations will
675 * block forever). On systems that have separate read and write timeout
676 * values, this method returns the read timeout. This
677 * value is in thousandths of a second (implementation specific?).
678 *
679 * @return The length of the timeout in thousandth's of a second or 0
680 * if not set
681 *
682 * @exception SocketException If an error occurs or Socket not connected
683 */
684 public synchronized int getSoTimeout () throws SocketException
685 {
686 if (impl == null)
687 throw new SocketException("Not connected");
688
689 Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
690 if (timeout instanceof Integer)
691 return(((Integer)timeout).intValue());
692 else
693 return 0;
694 }
695
696 /**
697 * This method sets the value for the system level socket option
698 * SO_SNDBUF to the specified value. Note that valid values for this
699 * option are specific to a given operating system.
700 *
701 * @param size The new send buffer size.
702 *
703 * @exception SocketException If an error occurs or Socket not connected
704 * @exception IllegalArgumentException FIXME
705 *
706 * @since 1.2
707 */
708 public void setSendBufferSize (int size) throws SocketException
709 {
710 if (impl == null)
711 throw new SocketException("Not connected");
712
713 if (size <= 0)
714 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
715
716 impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
717 }
718
719 /**
720 * This method returns the value of the system level socket option
721 * SO_SNDBUF, which is used by the operating system to tune buffer
722 * sizes for data transfers.
723 *
724 * @return The send buffer size.
725 *
726 * @exception SocketException If an error occurs or socket not connected
727 *
728 * @since 1.2
729 */
730 public int getSendBufferSize () throws SocketException
731 {
732 if (impl == null)
733 throw new SocketException("Not connected");
734
735 Object buf = impl.getOption(SocketOptions.SO_SNDBUF);
736
737 if (buf instanceof Integer)
738 return(((Integer)buf).intValue());
739 else
740 throw new SocketException("Internal Error: Unexpected type");
741 }
742
743 /**
744 * This method sets the value for the system level socket option
745 * SO_RCVBUF to the specified value. Note that valid values for this
746 * option are specific to a given operating system.
747 *
748 * @param size The new receive buffer size.
749 *
750 * @exception SocketException If an error occurs or Socket is not connected
751 * @exception IllegalArgumentException If size is 0 or negative
752 *
753 * @since 1.2
754 */
755 public void setReceiveBufferSize (int size) throws SocketException
756 {
757 if (impl == null)
758 throw new SocketException("Not connected");
759
760 if (size <= 0)
761 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
762
763 impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
764 }
765
766 /**
767 * This method returns the value of the system level socket option
768 * SO_RCVBUF, which is used by the operating system to tune buffer
769 * sizes for data transfers.
770 *
771 * @return The receive buffer size.
772 *
773 * @exception SocketException If an error occurs or Socket is not connected
774 *
775 * @since 1.2
776 */
777 public int getReceiveBufferSize () throws SocketException
778 {
779 if (impl == null)
780 throw new SocketException("Not connected");
781
782 Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
783
784 if (buf instanceof Integer)
785 return(((Integer)buf).intValue());
786 else
787 throw new SocketException("Internal Error: Unexpected type");
788 }
789
790 /**
791 * This method sets the value for the socket level socket option
792 * SO_KEEPALIVE.
793 *
794 * @param on True if SO_KEEPALIVE should be enabled
795 *
796 * @exception SocketException If an error occurs or Socket is not connected
797 *
798 * @since Java 1.3
799 */
800 public void setKeepAlive (boolean on) throws SocketException
801 {
802 if (impl == null)
803 throw new SocketException("Not connected");
804
805 impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
806 }
807
808 /**
809 * This method returns the value of the socket level socket option
810 * SO_KEEPALIVE.
811 *
812 * @return The setting
813 *
814 * @exception SocketException If an error occurs or Socket is not connected
815 *
816 * @since Java 1.3
817 */
818 public boolean getKeepAlive () throws SocketException
819 {
820 if (impl == null)
821 throw new SocketException("Not connected");
822
823 Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);
824
825 if (buf instanceof Boolean)
826 return(((Boolean)buf).booleanValue());
827 else
828 throw new SocketException("Internal Error: Unexpected type");
829 }
830
831 /**
832 * Closes the socket.
833 *
834 * @exception IOException If an error occurs
835 */
836 public synchronized void close () throws IOException
837 {
838 if (impl != null)
839 impl.close();
840 }
841
842 /**
843 * Converts this <code>Socket</code> to a <code>String</code>.
844 *
845 * @return The <code>String</code> representation of this <code>Socket</code>
846 */
847 public String toString ()
848 {
849 return("Socket " + impl);
850 }
851
852 // Class Methods
853
854 /**
855 * Sets the <code>SocketImplFactory</code>. This may be done only once per
856 * virtual machine. Subsequent attempts will generate a
857 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
858 * check is made prior to setting the factory. If
859 * insufficient privileges exist to set the factory, then an
860 * <code>IOException</code> will be thrown.
861 *
862 * @exception SecurityException If the <code>SecurityManager</code> does
863 * not allow this operation.
864 * @exception SocketException If the SocketImplFactory is already defined
865 * @exception IOException If any other error occurs
866 */
867 public static synchronized void setSocketImplFactory (SocketImplFactory fac)
868 throws IOException
869 {
870 // See if already set
871 if (factory != null)
872 throw new SocketException("SocketImplFactory already defined");
873
874 // Check permissions
875 SecurityManager sm = System.getSecurityManager();
876 if (sm != null)
877 sm.checkSetFactory();
878
879 factory = fac;
880 }
881
882 /**
883 * Closes the input side of the socket stream.
884 *
885 * @exception IOException If an error occurs.
886 */
887 public void shutdownInput() throws IOException
888 {
889 if (impl != null)
890 impl.shutdownInput();
891 }
892
893 /**
894 * Closes the output side of the socket stream.
895 *
896 * @exception IOException If an error occurs.
897 */
898 public void shutdownOutput() throws IOException
899 {
900 if (impl != null)
901 impl.shutdownOutput();
902 }
903
904 /**
905 * Returns the socket channel associated with this socket.
906 *
907 * It returns null if no associated socket exists.
908 */
909 public SocketChannel getChannel()
910 {
911 return ch;
912 }
913
914 /**
915 * Checks if the SO_REUSEADDR option is enabled
916 *
917 * @exception SocketException If an error occurs
918 *
919 * @since 1.4
920 */
921 public boolean getReuseAddress () throws SocketException
922 {
923 if (impl == null)
924 throw new SocketException ("Cannot initialize Socket implementation");
925
926 Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
927
928 if (!(reuseaddr instanceof Boolean))
929 throw new SocketException ("Internal Error");
930
931 return ((Boolean) reuseaddr).booleanValue ();
932 }
933
934 /**
935 * Enables/Disables the SO_REUSEADDR option
936 *
937 * @exception SocketException If an error occurs
938 *
939 * @since 1.4
940 */
941 public void setReuseAddress (boolean on) throws SocketException
942 {
943 if (impl == null)
944 throw new SocketException ("Cannot initialize Socket implementation");
945
946 impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
947 }
948
949 /**
950 * Returns the current traffic class
951 *
952 * @exception SocketException If an error occurs
953 *
954 * @see Socket:setTrafficClass
955 *
956 * @since 1.4
957 */
958 public int getTrafficClass () throws SocketException
959 {
960 if (impl == null)
961 throw new SocketException ("Cannot initialize Socket implementation");
962
963 Object obj = impl.getOption(SocketOptions.IP_TOS);
964
965 if (obj instanceof Integer)
966 return ((Integer) obj).intValue ();
967 else
968 throw new SocketException ("Unexpected type");
969 }
970
971 /**
972 * Sets the traffic class value
973 *
974 * @param tc The traffic class
975 *
976 * @exception SocketException If an error occurs
977 * @exception IllegalArgumentException If tc value is illegal
978 *
979 * @see Socket:getTrafficClass
980 *
981 * @since 1.4
982 */
983 public void setTrafficClass (int tc) throws SocketException
984 {
985 if (impl == null)
986 throw new SocketException ("Cannot initialize Socket implementation");
987
988 if (tc < 0 || tc > 255)
989 throw new IllegalArgumentException();
990
991 impl.setOption (SocketOptions.IP_TOS, new Integer (tc));
992 }
993 }