KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > util > JRawConnection


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.util;
17
18 import net.sf.jftp.*;
19 import net.sf.jftp.config.*;
20 import net.sf.jftp.gui.framework.*;
21 import net.sf.jftp.net.*;
22 import net.sf.jftp.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import java.io.*;
28
29 import java.net.*;
30
31 import java.util.*;
32
33 import javax.swing.*;
34 import javax.swing.event.*;
35
36
37 /*
38 alternative connection class, used for raw tcp/ip connection
39 */

40 public class JRawConnection implements Runnable JavaDoc
41 {
42     private int timeout = Settings.connectionTimeout;
43     private String JavaDoc host;
44     private int port;
45     private PrintStream out;
46     private DataInputStream in;
47     private Socket s;
48     private JReciever jrcv;
49     private boolean isOk = false;
50     private boolean established = false;
51     private boolean reciever = false;
52     private Thread JavaDoc runner;
53
54     public JRawConnection(String JavaDoc host, int port)
55     {
56         this(host, port, false);
57     }
58
59     public JRawConnection(String JavaDoc host, int port, boolean reciever)
60     {
61         this.host = host;
62         this.port = port;
63         this.reciever = reciever;
64
65         runner = new Thread JavaDoc(this);
66         runner.start();
67     }
68
69     public void run()
70     {
71         try
72         {
73             s = new Socket(host, port);
74
75             // s.setSoTimeout(Resource.socketTimeout);
76
out = new PrintStream(s.getOutputStream());
77             in = new DataInputStream(s.getInputStream());
78
79             if(reciever)
80             {
81                 JReciever jrcv = new JReciever(in);
82             }
83
84             isOk = true;
85         }
86         catch(Exception JavaDoc ex)
87         {
88             isOk = false;
89         }
90
91         established = true;
92     }
93
94     public boolean isThere()
95     {
96         int cnt = 0;
97
98         while(!established && (cnt < timeout))
99         {
100             pause(100);
101             cnt = cnt + 100;
102         }
103
104         return isOk;
105     }
106
107     public void send(String JavaDoc data)
108     {
109         try
110         {
111             out.println(data);
112         }
113         catch(Exception JavaDoc ex)
114         {
115             System.out.println(ex + "@JConnection.send()");
116         }
117     }
118
119     public PrintStream getInetOutputStream()
120     {
121         return out;
122     }
123
124     public DataInputStream getInetInputStream()
125     {
126         return in;
127     }
128
129     private void pause(int time)
130     {
131         try
132         {
133             Thread.sleep(time);
134         }
135         catch(Exception JavaDoc ex)
136         {
137             System.out.println(ex);
138         }
139     }
140 }
141
Popular Tags