-
Notifications
You must be signed in to change notification settings - Fork 0
/
echoserver.java
34 lines (28 loc) · 1.06 KB
/
echoserver.java
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
import java.io.*;
import java.net.*;
public class echoserver
{
private final static int PORT = 8080;
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Listening on port " + PORT);
while (true) {
try (Socket socket = serverSocket.accept();
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
BufferedReader in = new BufferedReader(isr);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)
) {
System.out.println("Connection accepted");
String line;
while ((line = in.readLine()) != null) {
System.out.println("Server received: " + line + ". Sending to client");
out.println(line);
if (line.equals("Bye")) {
break;
}
}
}
}
}
}