-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEchoClient.java
More file actions
39 lines (34 loc) · 1.49 KB
/
EchoClient.java
File metadata and controls
39 lines (34 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// This program implements an echo client that sends messages to the server and receives the echoed messages.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class EchoClient {
public static void main(String[] args) {
String hostname = "localhost"; // Server hostname
int port = 12345; // Server port number
try (Socket socket = new Socket(hostname, port)) {
System.out.println("Connected to echo server");
// Create input and output streams for communication
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
String userInput;
// Read user input and send it to the server
while (true) {
System.out.print("Enter message: ");
userInput = scanner.nextLine();
if (userInput.equalsIgnoreCase("exit")) {
break;
}
out.println(userInput);
// Read and print the echoed message from the server
System.out.println("Echoed from server: " + in.readLine());
}
} catch (IOException e) {
System.err.println("Error connecting to server: " + e.getMessage());
}
}
}