Mohammad_Daoud

Peer Client Side

Aug 10th, 2021 (edited)
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package homeworks.peerApp;
  2. /**
  3.  * @TEAM_MEMBERS :
  4.  * MOHAMMAD ABDALLAH RAJA DAOUD - 0173632
  5.  * HAMZA MONTHER MOSTAFA AMIRAH - 0189136
  6.  *
  7.  * @instructor :
  8.  * Dr. Wesam Al Mobaideen
  9.  */
  10. import java.io.*;
  11. import java.net.InetAddress;
  12.  
  13. import java.net.Socket;
  14. // this is the client side of peer application
  15. public class ClientPeer extends Thread {
  16.     /**
  17.      * In this program we've create a simple client-server application but the difference is that
  18.      *          the client can send multiple messages and the server will receive it.(send and receive in parallel)
  19.      *
  20.      * To do that we create a values for bufferReader and an Address.
  21.      * After that we create 2 Thread objects -> 1st : the reader thread that will handle the data that have been sending
  22.      *                                                from the server .
  23.      *                                       -> 2nd : the writer thread which will carry the data that we need to sent
  24.      *                                                and sending it to server
  25.      * Now after we created these two threads all we have to do is RUN THE PROJECT .
  26.      *
  27.      */
  28.     public static void main(String[] args) {
  29.         try {
  30.             // TODO: CREATE THE PRE-CONNECTION SET-UP
  31.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// create the reader
  32.             InetAddress myAddress = InetAddress.getLocalHost(); // to show the active workin'On machine
  33.  
  34.             Socket client = new Socket("127.0.0.1", 5555);// declare a socket for client
  35.  
  36.             System.out.println("You are on : " + myAddress);// to show my address
  37.             // the next statements for UI
  38.             System.out.println("CONNECTING . . . ");
  39.             System.out.println("\nConnected with " + client.getRemoteSocketAddress());
  40.             System.out.println("\n**************************************************\n" +
  41.                     "be first who send or wait for receiving messages .. \n" +
  42.                     "**************************************************");
  43.  
  44.             Thread readerThread =new ReadThread(client);
  45.             Thread writerThread =new WriteThread(client);
  46.  
  47.             //TODO : MAKE ALL THINGS WORK
  48.             readerThread.start();// start the reading thread
  49.             readerThread.join(1000);
  50.             writerThread.start();// start the writing thread
  51.             writerThread.join(1000);
  52.         } catch (Exception e) {
  53.             //TODO HANDLE ANY EXCEPTION
  54.             e.printStackTrace();
  55.             System.err.println("❌ ERROR ❌ :" + e); // for catching the error (EXCEPTION HANDLING )
  56.             System.err.println("termination in process !!");
  57.             System.exit(-1);
  58.         }
  59.     }
  60. }
  61.  
  62.  
Add Comment
Please, Sign In to add comment