// Source Code for ClientSocket.class //
// Client Code for Desktop Calendar    //

// Code based on using Server #18     //
// Written by Khalid Mufti - Group 31 //

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

// This class handles all socket communication, creation and termination. //
// Requires the ip address as a string and a port number as parameters.   //
public class ClientSocket 
{
   private Socket client;
   private boolean status;
   private BufferedReader in;
   private PrintWriter out;   
   private boolean done;
  

   // This is the constructor.  It sets up the input and output buffers and tries //
   // to setup a connection with remote server. 				  //
   // Requires the ip address as a string and a port number as parameters.        //
   public ClientSocket(String s, int p)
   {
      try
      {
         client = new Socket (s, p);
         in = new BufferedReader(new InputStreamReader (client.getInputStream()));
         out = new PrintWriter (client.getOutputStream(), true); 
         status = true; // Socket is okay //
      }
      
     catch (IOException e)
     {
        status = false;  // Something is wrong with the socket //
     }

     catch (SecurityException e)
     {
        status = false; // Something is wrong with the socket //
     }

   }

   // This function returns the status of the socket //
   public boolean isOkay()
   {
      return status;
   }

   // This function reads buffered data from the socket.  On an error, an IOException //
   // is thrown. 								      //
   public String Read()
      throws IOException
   {
      return in.readLine();
   }

   // This function writes data to the socket.  On an error, an IOException  //
   // is thrown.  The parameter text is outputted to the socket.             //
   public void Write(String text)
      throws IOException
   {
      out.println(text);  
      out.flush();     
   }

   // This function closes the socket.  On an error, an IOException is thrown //   
   public void Close ()
      throws IOException
   {
      client.close();
   } 
}
