Advertisement
Guest User

ClientStarer

a guest
Mar 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.*;
  5.  
  6.  
  7. public class ClientStarer {
  8.  
  9.     private int ID;
  10.     private Socket socket;
  11.     private DataOutputStream dos;
  12.     private DataInputStream dis;
  13.     //todo: init other required variables here
  14.  
  15.     ClientStarer(int id, String host, int port) throws IOException {
  16.         this.ID = id;
  17.         socket=new Socket(InetAddress.getByName(host),port);
  18.         dis= new DataInputStream(socket.getInputStream());
  19.         dos=new DataOutputStream(socket.getOutputStream());
  20.         dos.writeInt(id);
  21.         dos.flush();
  22.         // todo: Connect to server and send client ID
  23.         this.listen();
  24.         // todo: Listen for incoming messages
  25.     }
  26.  
  27.     // todo: Implement the sending message mechanism
  28.     void sendMessage(int idReceiver, String message) throws IOException {
  29.         dos.writeUTF(message+":"+idReceiver);
  30.         dos.flush();
  31.     }
  32.  
  33.     // todo: end communication - send END to server
  34.     private void endCommunication() throws IOException {
  35.         dos.writeUTF("END");
  36.         dos.flush();
  37.     }
  38.  
  39.     // todo: listen for icoming messages from the server.
  40.     // It should start a separate thread to handle listening
  41.     // and not block the execution
  42.     // Should start a new ClientStarterWorkerThread
  43.     private void listen() {
  44.         new ClientStarterWorkerThread(this.ID,dis).start();
  45.     }
  46.  
  47.     public static void main(String[] args) throws IOException, InterruptedException {
  48.         //todo: Initialize and start 3 clients
  49.  
  50.         ClientStarer client1=new ClientStarer(1,"localhost",9876);
  51.         ClientStarer client2=new ClientStarer(2,"localhost",9876);
  52.         ClientStarer client3=new ClientStarer(3,"localhost",9876);
  53.         // Simulate chat
  54.         client1.sendMessage(2, "Hello from client 1");
  55.         Thread.sleep(1000);
  56.         client2.sendMessage(3, "Hello from client 2");
  57.         Thread.sleep(1000);
  58.         client1.sendMessage(3, "Hello from client 1");
  59.         Thread.sleep(1000);
  60.         client3.sendMessage(1, "Hello from client 3");
  61.         Thread.sleep(1000);
  62.         client3.sendMessage(2, "Hello from client 3");
  63.  
  64.         // Exit the chatroom
  65.         client1.endCommunication();
  66.         client2.endCommunication();
  67.         client3.endCommunication();
  68.     }
  69. }
  70. class ClientStarterWorkerThread extends Thread {
  71.  
  72.     private int ID;
  73.     private DataInputStream inputStream;
  74.  
  75.     public ClientStarterWorkerThread(int clientID, DataInputStream inputStream) {
  76.         this.ID = clientID;
  77.         this.inputStream = inputStream;
  78.     }
  79.  
  80.     @Override
  81.     public void run() {
  82.         String line;
  83.         while (true)
  84.         {
  85.             try {
  86.                 line=inputStream.readUTF();
  87.                 if(line.equals("END"))
  88.                     break;
  89.                 else
  90.                 System.out.println(line);
  91.             } catch (IOException e) {
  92.                 e.printStackTrace();
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement