Rev 15 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 15 | Rev 73 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * TcpClient.java |
2 | * TcpClient.java |
| 3 | * |
3 | * |
| 4 | * Created on den 25 augusti 2003, 22:50 |
4 | * Created on den 25 augusti 2003, 22:50 |
| 5 | */ |
5 | */ |
| 6 | package Macbeth.Net; |
6 | package Macbeth.Net; |
| 7 | 7 | ||
| 8 | import java.net.*; |
8 | import java.net.*; |
| 9 | import java.io.*; |
9 | import java.io.*; |
| 10 | 10 | ||
| 11 | /** |
11 | /** |
| 12 | * A TCP client that sends data to a TCP Server. |
12 | * A TCP client that sends data to a TCP Server. |
| 13 | * @author Jimmy |
13 | * @author Jimmy |
| 14 | */ |
14 | */ |
| 15 | public class TcpClient { |
15 | public class TcpClient { |
| 16 | private int port; |
16 | private int port; |
| 17 | private String hostname; |
17 | private String hostname; |
| 18 | private Socket socket; |
18 | private Socket socket; |
| 19 | private BufferedWriter bw; |
19 | private BufferedWriter bw; |
| 20 | private BufferedReader br; |
20 | private BufferedReader br; |
| 21 | 21 | ||
| 22 | /** |
22 | /** |
| 23 | * Creates a new instance of TcpClient. |
23 | * Creates a new instance of TcpClient. |
| 24 | * @param port The port that the socket connection should be opened through use. |
24 | * @param port The port that the socket connection should be opened through use. |
| 25 | * @param hostname The hostname that the socket should connect to. |
25 | * @param hostname The hostname that the socket should connect to. |
| 26 | */ |
26 | */ |
| 27 | public TcpClient(int port, String hostname) { |
27 | public TcpClient(int port, String hostname) { |
| 28 | this.port = port; |
28 | this.port = port; |
| 29 | this.hostname = hostname; |
29 | this.hostname = hostname; |
| 30 | socket = null; |
30 | socket = null; |
| 31 | bw = null; |
31 | bw = null; |
| 32 | br = null; |
32 | br = null; |
| 33 | } |
33 | } |
| 34 | 34 | ||
| 35 | /** |
35 | /** |
| 36 | * Finds out whether or not the client has successfully |
36 | * Finds out whether or not the client has successfully |
| 37 | * connected to the server. |
37 | * connected to the server. |
| 38 | * @return true if connected, false otherwise. |
38 | * @return true if connected, false otherwise. |
| 39 | */ |
39 | */ |
| 40 | public boolean isConnected() { |
40 | public boolean isConnected() { |
| 41 | if (socket!=null && bw!=null) { |
41 | if (socket!=null && bw!=null) { |
| 42 | return socket.isConnected(); |
42 | return socket.isConnected(); |
| 43 | } else { |
43 | } else { |
| 44 | return false; |
44 | return false; |
| 45 | } |
45 | } |
| 46 | } |
46 | } |
| 47 | 47 | ||
| 48 | /** |
48 | /** |
| 49 | * Opens the connection to the server. |
49 | * Opens the connection to the server. |
| 50 | * @throws UnknownHostException if the hostname is invalid. |
50 | * @throws UnknownHostException if the hostname is invalid. |
| 51 | * @throws IOException if an I/O-error occurs while trying to connect. |
51 | * @throws IOException if an I/O-error occurs while trying to connect. |
| 52 | */ |
52 | */ |
| 53 | public void openConnection() throws UnknownHostException,IOException { |
53 | public void openConnection() throws UnknownHostException,IOException { |
| 54 | if (hostname==null || hostname.equals("")) return; |
54 | if (hostname==null || hostname.equals("")) return; |
| 55 | socket = new Socket(hostname, port); |
55 | socket = new Socket(hostname, port); |
| 56 | bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); |
56 | bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); |
| 57 | br = new BufferedReader(new InputStreamReader(socket.getInputStream()) ); |
57 | br = new BufferedReader(new InputStreamReader(socket.getInputStream()) ); |
| 58 | } |
58 | } |
| 59 | 59 | ||
| 60 | /** |
60 | /** |
| 61 | * Closes the connection to the server. |
61 | * Closes the connection to the server. |
| 62 | * @throws IOException if an I/O-error occurs while trying to close connection. |
62 | * @throws IOException if an I/O-error occurs while trying to close connection. |
| 63 | */ |
63 | */ |
| 64 | public void closeConnection() throws IOException { |
64 | public void closeConnection() throws IOException { |
| 65 | if (bw!=null) { |
65 | if (bw!=null) { |
| 66 | bw.close(); |
66 | bw.close(); |
| 67 | } |
67 | } |
| 68 | if (br!=null) { |
68 | if (br!=null) { |
| 69 | br.close(); |
69 | br.close(); |
| 70 | } |
70 | } |
| 71 | socket.close(); |
71 | socket.close(); |
| 72 | } |
72 | } |
| 73 | 73 | ||
| 74 | /** |
74 | /** |
| 75 | * Sends a string to the server through the socket. |
75 | * Sends a string to the server through the socket. |
| 76 | * @param s The string. It will be sent immediately (output stream is flushed). |
76 | * @param s The string. It will be sent immediately (output stream is flushed). |
| 77 | * @throws IOException If an I/O-error occurs while trying to send the string. |
77 | * @throws IOException If an I/O-error occurs while trying to send the string. |
| 78 | */ |
78 | */ |
| 79 | public void sendString(String s) throws IOException { |
79 | public void sendString(String s) throws IOException { |
| 80 | if (isConnected()) { |
80 | if (isConnected()) { |
| 81 | bw.write(s); |
81 | bw.write(s); |
| 82 | bw.newLine(); |
82 | bw.newLine(); |
| 83 | bw.flush(); |
83 | bw.flush(); |
| 84 | } else { |
84 | } else { |
| 85 | throw new IOException("Not connected to server!"); |
85 | throw new IOException("Not connected to server!"); |
| 86 | } |
86 | } |
| 87 | } |
87 | } |
| 88 | 88 | ||
| 89 | 89 | ||
| 90 | /** |
90 | /** |
| 91 | * Gets a row of data from the server through the socket. |
91 | * Gets a row of data from the server through the socket. |
| 92 | * @throws IOException If an I/O-error occurs while trying to receive a row. |
92 | * @throws IOException If an I/O-error occurs while trying to receive a row. |
| 93 | */ |
93 | */ |
| 94 | public String getRow() throws IOException { |
94 | public String getRow() throws IOException { |
| 95 | if (isConnected()) { |
95 | if (isConnected()) { |
| 96 | return br.readLine(); |
96 | return br.readLine(); |
| 97 | } else { |
97 | } else { |
| 98 | throw new IOException("Not connected to server!"); |
98 | throw new IOException("Not connected to server!"); |
| 99 | } |
99 | } |
| 100 | } |
100 | } |
| 101 | } |
101 | } |
| 102 | 102 | ||