Subversion Repositories HomeAutomation

Rev

Rev 15 | Blame | Last modification | View Log | SVN | RSS feed

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