-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.java
More file actions
33 lines (29 loc) · 1.13 KB
/
EchoServer.java
File metadata and controls
33 lines (29 loc) · 1.13 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
/* Assignment 2 - Question 1 - Server
Authors: John Hebb & Matthew Souter
Date: Monday, October 8th, 2018
Description: Echo server program.
*/
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) throws IOException {
// Set port number
int portNum = 9999;
// Open sockets, buffers and streams
try (
ServerSocket serverSocket = new ServerSocket(portNum);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
) {
// Read from and write to streams
String s;
while ((s = in.readLine()) != null) {
out.println(s);
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port " + portNum + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}