-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
42 lines (34 loc) · 1.31 KB
/
Main.java
File metadata and controls
42 lines (34 loc) · 1.31 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
40
41
42
package com.dj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 5000)) {
socket.setSoTimeout(5000);
BufferedReader echoes = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter stringToEcho = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
String echoString;
String response;
do {
System.out.println("Enter string to be echoed");
echoString = scanner.nextLine();
stringToEcho.println(echoString);
if (!echoString.equals("exit")) {
response = echoes.readLine();
System.out.println(response);
}
} while (!echoString.equals("exit"));
} catch (SocketTimeoutException e) {
System.out.println("Socket time out");
} catch (IOException e) {
System.out.println("Client Error: " + e.getMessage());
}
}
}