diff --git a/Client.java b/Client.java new file mode 100644 index 0000000..0a2d8ff --- /dev/null +++ b/Client.java @@ -0,0 +1,47 @@ + +import java.io.*; +import java.net.*; +import java.util.*; + +// Client class +class Client { + + // driver code + public static void main(String[] args) { + // establish a connection by providing host and port + // number + try (Socket socket = new Socket("localhost", 1234)) { + + // writing to server + PrintWriter out = new PrintWriter( + socket.getOutputStream(), true); + + // reading from server + BufferedReader in = new BufferedReader(new InputStreamReader( + socket.getInputStream())); + + // object of scanner class + Scanner sc = new Scanner(System.in); + String line = null; + + while (!"exit".equalsIgnoreCase(line)) { + + // reading from user + line = sc.nextLine(); + + // sending the user input to server + out.println(line); + out.flush(); + + // displaying server reply + System.out.println("Server replied " + + in.readLine()); + } + + // closing the scanner object + sc.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +}