Exemplo n.º 1
0
  /**
   * Method to create a socket to connect the client to the server
   *
   * @throws IOException
   */
  private void connectToServer() throws IOException {
    displayMessage("Attempting connection...\n");

    InetAddress ipAddress =
        InetAddress.getByName(clientAddress); // object that holds the ip address of the client
    clientSocket = new Socket(ipAddress, port); // create Socket to make connection to server

    displayMessage("Successful Connection\n"); // successful connection message
  }
Exemplo n.º 2
0
 /**
  * Method to close the connection between the client and the server. Input and output streams that
  * were opened by the client are closed, and the socket through which the client and server were
  * using to is closed as well.
  */
 private void closeConnection() {
   displayMessage("\nAttempting to close connection...");
   try {
     clientOutputStream.close(); // close client output stream
     clientInputStream.close(); // close client input stream
     clientSocket.close(); // close socket
     displayMessage("Connection successfully closed");
   } catch (IOException ioException) {
     System.out.println("IO Exception in closeConnection Method");
   }
 }
Exemplo n.º 3
0
 /**
  * Method that uses the socket to send data from the client to the server using the
  * ObjectOutputStream variable
  *
  * @param message
  */
 private void sendData(String message) {
   try {
     clientOutputStream.writeObject(
         "CLIENT: " + message); // writes message to stream to be sent to server
     clientOutputStream.flush(); // clears buffer
     displayMessage("\nCLIENT: " + message);
   } catch (IOException ioException) {
     System.out.println("IO Exception in sendData Method");
   }
 }
Exemplo n.º 4
0
 /**
  * Method that keeps the connection alive between client and server and stays open until
  * connection is terminated
  *
  * @throws IOException
  */
 private void handleConnection() throws IOException {
   boolean clientIsAlive = true;
   // continually keep the connection open and read the messages from the server until the
   // connection is closed
   do {
     try {
       // readObject is used to read an object from the stream and then casted as a string
       messageFromServer = (String) clientInputStream.readObject();
       displayMessage("\n" + messageFromServer); // display message
     }
     // no definition for the readObject class was found
     catch (ClassNotFoundException classNotFoundException) {
       System.out.println("Class Not found exception in handleConnection");
     }
   } while (clientIsAlive
       == true); // keep going until connection terminated (Infinite until exit/Server closes)
 }
Exemplo n.º 5
0
 public void run() {
   while (true) {
     switch (Thread.currentThread().getName()) {
       case "SEND":
         try {
           sendMessage();
         } catch (NumberFormatException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }
         break;
       case "RECV":
         try {
           displayMessage();
         } catch (ClassNotFoundException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }
         break;
     }
   }
 }