Advertisement
Gaudenz

EmployeeClient

May 30th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. public class EmployeeClient {
  6.     private static final String HOST = "localhost";
  7.     private static final int PORT = 8080;
  8.  
  9.     public static void main(String[] args) {
  10.         try (Socket socket = new Socket(HOST, PORT)) {
  11.             InputStream input = socket.getInputStream();
  12.             BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  13.  
  14.             OutputStream output = socket.getOutputStream();
  15.             PrintWriter writer = new PrintWriter(output, true);
  16.  
  17.             BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
  18.             String text;
  19.  
  20.             System.out.println("Enter employee ID (or 'exit' to quit):");
  21.             while ((text = consoleReader.readLine()) != null) {
  22.                 writer.println(text);
  23.  
  24.                 if (text.equals("exit")) {
  25.                     break;
  26.                 }
  27.  
  28.                 String response = reader.readLine();
  29.                 System.out.println("Server response: " + response);
  30.             }
  31.  
  32.             socket.close();
  33.         } catch (UnknownHostException ex) {
  34.             System.out.println("Server not found: " + ex.getMessage());
  35.         } catch (IOException ex) {
  36.             System.out.println("I/O error: " + ex.getMessage());
  37.         }
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement