KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > util > TimedSocket


1 package net.matuschek.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InterruptedIOException JavaDoc;
5 import java.net.InetAddress JavaDoc;
6 import java.net.Socket JavaDoc;
7
8 /**
9  * This class offers a timeout feature on socket connections.
10  * A maximum length of time allowed for a connection can be
11  * specified, along with a host and port.
12  *
13  * @author David Reilly (written for JavaWorld)
14  * @author Daniel Matuschek
15  * @version $Id: TimedSocket.java,v 1.2 2002/05/31 14:45:56 matuschd Exp $
16  *
17  * imported to the net.matuschek.util source tree by Daniel Matuschek
18  */

19 public class TimedSocket
20 {
21
22   /**
23    * Attempts to connect to a service at the specified address
24    * and port, for a specified maximum amount of time.
25    *
26    * @param addr Address of host
27    * @param port Port of service
28    * @param delay Delay in milliseconds
29    */

30   public static Socket JavaDoc getSocket ( InetAddress JavaDoc addr, int port, int delay)
31     throws InterruptedIOException JavaDoc, IOException JavaDoc
32   {
33     // Create a new socket thread, and start it running
34
SocketThread st = new SocketThread( addr, port );
35     st.start();
36     
37     int timer = 0;
38     Socket JavaDoc sock = null;
39     
40     for (;;) {
41       // Check to see if a connection is established
42

43       if (st.isConnected()) {
44     // Yes ... assign to sock variable, and break out of loop
45
sock = st.getSocket();
46     break;
47       } else {
48     // Check to see if an error occurred
49
if (st.isError()) {
50       // No connection could be established
51
throw (st.getException());
52     }
53     
54     try {
55       // Sleep for a short period of time
56
Thread.sleep ( POLL_DELAY );
57     } catch (InterruptedException JavaDoc ie) {}
58     
59     // Increment timer
60
timer += POLL_DELAY;
61     
62     // Check to see if time limit exceeded
63
if (timer > delay) {
64       // Can't connect to server
65
throw new InterruptedIOException JavaDoc("Could not connect for " +
66                        delay + " milliseconds");
67     }
68       }
69     }
70     
71     return sock;
72   }
73   
74   /**
75    * Attempts to connect to a service at the specified address
76    * and port, for a specified maximum amount of time.
77    *
78    * @param host Hostname of machine
79    * @param port Port of service
80    * @param delay Delay in milliseconds
81    */

82   public static Socket JavaDoc getSocket ( String JavaDoc host, int port, int delay)
83     throws InterruptedIOException JavaDoc, IOException JavaDoc
84   {
85     // Convert host into an InetAddress, and call getSocket method
86
InetAddress JavaDoc inetAddr = InetAddress.getByName (host);
87     
88     return getSocket ( inetAddr, port, delay );
89   }
90   
91   
92   /**
93    * Inner class for establishing a socket thread
94    * within another thread, to prevent blocking.
95    */

96   static class SocketThread extends Thread JavaDoc
97   {
98     // Socket connection to remote host
99
volatile private Socket JavaDoc m_connection = null;
100     // Hostname to connect to
101
private String JavaDoc m_host = null;
102     // Internet Address to connect to
103
private InetAddress JavaDoc m_inet = null;
104     // Port number to connect to
105
private int m_port = 0;
106     // Exception in the event a connection error occurs
107
private IOException JavaDoc m_exception = null;
108     
109     // Connect to the specified host and port number
110
public SocketThread ( String JavaDoc host, int port) {
111       // Assign to member variables
112
m_host = host;
113       m_port = port;
114     }
115     
116     // Connect to the specified host IP and port number
117
public SocketThread ( InetAddress JavaDoc inetAddr, int port ) {
118       // Assign to member variables
119
m_inet = inetAddr;
120       m_port = port;
121     }
122     
123     public void run() {
124       // Socket used for establishing a connection
125
Socket JavaDoc sock = null;
126       
127       try {
128     // Was a string or an inet specified
129
if (m_host != null) {
130       // Connect to a remote host - BLOCKING I/O
131
sock = new Socket JavaDoc (m_host, m_port);
132     } else {
133       // Connect to a remote host - BLOCKING I/O
134
sock = new Socket JavaDoc (m_inet, m_port);
135     }
136       }
137       catch (IOException JavaDoc ioe) {
138     // Assign to our exception member variable
139
m_exception = ioe;
140     return;
141       }
142
143       // If socket constructor returned without error,
144
// then connection finished
145
m_connection = sock;
146     }
147
148     // Are we connected?
149
public boolean isConnected() {
150       if (m_connection == null)
151     return false;
152       else
153     return true;
154     }
155
156     // Did an error occur?
157
public boolean isError() {
158       if (m_exception == null)
159     return false;
160       else
161     return true;
162     }
163
164     // Get socket
165
public Socket JavaDoc getSocket() {
166       return m_connection;
167     }
168     
169     // Get exception
170
public IOException JavaDoc getException() {
171       return m_exception;
172     }
173   }
174   
175   /** Polling delay for socket checks (in milliseconds) */
176   private static final int POLL_DELAY = 100;
177 }
178
Popular Tags