KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > net > JConnection


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.net;
17
18 import java.io.BufferedOutputStream JavaDoc;
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.Socket JavaDoc;
25
26 import net.sf.jftp.config.Settings;
27 import net.sf.jftp.system.logging.Log;
28
29
30 /**
31  * Basic class for getting an TCP/IP-Connection
32  * timeout sets (as the name says) the maximum time the Thread
33  * waits for the target host...
34  */

35 public class JConnection implements Runnable JavaDoc
36 {
37     private int timeout = Settings.connectionTimeout;
38     private String JavaDoc host;
39     private int port;
40     private PrintStream JavaDoc out;
41     private BufferedReader JavaDoc in;
42     private Socket JavaDoc s;
43     private boolean isOk = false;
44     private boolean established = false;
45     private boolean reciever = false;
46     private Thread JavaDoc runner;
47     private int localPort = -1;
48     private int time = 0;
49
50     /*
51     private boolean useSocks4 = false;
52     private String socks4Host = "192.168.0.1";
53     private int socks4Port = 1080;
54     */

55     public JConnection(String JavaDoc host, int port)
56     {
57         this.host = host;
58         this.port = port;
59
60         runner = new Thread JavaDoc(this);
61         runner.start();
62     }
63
64     public JConnection(String JavaDoc host, int port, int time)
65     {
66         this.host = host;
67         this.port = port;
68         this.timeout = time;
69         this.time = time;
70
71         runner = new Thread JavaDoc(this);
72         runner.start();
73     }
74
75     /*
76     private int swabInt(int v)
77     {
78             return (v >>> 24) | (v << 24) |
79       ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
80     }
81     */

82     public void run()
83     {
84         try
85         {
86             /*
87                 if(useSocks4)
88                 {
89                         s = new Socket(socks4Host, socks4Port);
90
91                     Log.debug("\nsending socks4-request...");
92
93                     OutputStream o = s.getOutputStream();
94
95                     byte buf[] = new byte[9];
96
97                     buf[0] = (byte) 4;
98                     buf[1] = (byte) 0x1;
99                     buf[2] = (byte) 1;
100                     buf[3] = (byte) 1;
101                     buf[4] = (byte) 1;
102                     buf[5] = (byte) 1;
103                     buf[6] = (byte) 1;
104                     buf[7] = (byte) 1;
105                     buf[8] = (byte) 0;
106
107                     o.write(buf);
108                     o.flush();
109
110                         Log.debug("reading response...");
111                     byte ret[] = new byte[8];
112                     int bytes = s.getInputStream().read(ret);
113
114                     if(ret[1] == 90)
115                     {
116                             Log.debug("connection accepted");
117
118                                 localPort = s.getLocalPort();
119                                 out = new PrintStream(new BufferedOutputStream(s.getOutputStream(), Settings.bufferSize));
120                                 in = new BufferedReader(new InputStreamReader(s.getInputStream()), Settings.bufferSize);
121                             isOk = true;
122                     }
123                     else
124                     {
125                             Log.debug("socks-connection refused (returncode " + ret[1] + ")");
126                             isOk = false;
127                     }
128                 }
129                 else
130                 {
131                 */

132             s = new Socket JavaDoc(host, port);
133
134             localPort = s.getLocalPort();
135
136             //if(time > 0) s.setSoTimeout(time);
137
out = new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(s.getOutputStream(),
138                                                            Settings.bufferSize));
139             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(s.getInputStream()),
140                                     Settings.bufferSize);
141             isOk = true;
142
143             // }
144
}
145         catch(Exception JavaDoc ex)
146         {
147             ex.printStackTrace();
148             Log.out("WARNING: connection closed due to exception (" + host +
149                     ":" + port + ")");
150             isOk = false;
151
152             try
153             {
154                 if((s != null) && !s.isClosed())
155                 {
156                     s.close();
157                 }
158
159                 if(out != null)
160                 {
161                     out.close();
162                 }
163
164                 if(in != null)
165                 {
166                     in.close();
167                 }
168             }
169             catch(Exception JavaDoc ex2)
170             {
171                 ex2.printStackTrace();
172                 Log.out("WARNING: got more errors trying to close socket and streams");
173             }
174         }
175
176         established = true;
177     }
178
179     public boolean isThere()
180     {
181         int cnt = 0;
182
183         while(!established && (cnt < timeout))
184         {
185             pause(10);
186             cnt = cnt + 10;
187
188             //System.out.println(cnt + "/" + timeout);
189
}
190
191         return isOk;
192     }
193
194     public void send(String JavaDoc data)
195     {
196         try
197         {
198             //System.out.println(":"+data+":");
199
out.print(data);
200             out.print("\r\n");
201             out.flush();
202
203             if(data.startsWith("PASS"))
204             {
205                 Log.debug("> PASS ****");
206             }
207             else
208             {
209                 Log.debug("> " + data);
210             }
211         }
212         catch(Exception JavaDoc ex)
213         {
214             ex.printStackTrace();
215         }
216     }
217
218     public PrintStream JavaDoc getInetOutputStream()
219     {
220         return out;
221     }
222
223     public BufferedReader JavaDoc getReader()
224     {
225         return in;
226     }
227
228     public int getLocalPort()
229     {
230         return localPort;
231     }
232
233     public InetAddress JavaDoc getLocalAddress() throws IOException JavaDoc
234     {
235         return s.getLocalAddress();
236     }
237
238     private void pause(int time)
239     {
240         try
241         {
242             Thread.sleep(time);
243         }
244         catch(Exception JavaDoc ex)
245         {
246         }
247     }
248
249     public BufferedReader JavaDoc getIn() {
250         return in;
251     }
252
253     public void setIn(BufferedReader JavaDoc in) {
254         this.in = in;
255     }
256
257     public PrintStream JavaDoc getOut() {
258         return out;
259     }
260
261     public void setOut(PrintStream JavaDoc out) {
262         this.out = out;
263     }
264 }
265
Popular Tags