2024-11-12 08:56:25 +00:00
|
|
|
|
|
|
|
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
|
2024-11-12 09:13:53 +00:00
|
|
|
try (Socket socket = new Socket("localhost", 8080)) {
|
2024-11-12 08:56:25 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|