1
15-214
School of Computer Science
distributed systems Christian Kstner Bogdan Vasilescu School of - - PowerPoint PPT Presentation
Principles of Software Construction: Objects, Design, and Concurrency Introduction to networking and distributed systems Christian Kstner Bogdan Vasilescu School of Computer Science 15-214 1 Administrivia HW 6 out tomorrow morning -
1
15-214
School of Computer Science
2
15-214
3
15-214
4
15-214
5
15-214
6
15-214
7
15-214
8
15-214
9
15-214
10
15-214
public static void main(String[] args) throws IOException { Socket socket; if (args.length == 2) { // We're the client InetAddress host = InetAddress.getByName(args[0]); int port = Integer.parseInt(args[1]); socket = new Socket(host, port); } else { // We're the server int port = Integer.parseInt(args[0]); ServerSocket serverSocket = new ServerSocket(port); socket = serverSocket.accept(); } InputStream socketIn = socket.getInputStream(); new Thread(() -> copyLines(socketIn, System.out)).start(); copyLines(System.in, socket.getOutputStream()); }
11
15-214
private static void copyLines(InputStream in, OutputStream out) { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); PrintWriter writer = new PrintWriter(out, true); // Read a line at a time from reader and copy to writer try { String line; while ((line = reader.readLine()) != null) { writer.println(line); } } catch (IOException e) { System.out.println("IO error: " + e); } }
12
15-214
13
15-214
14
15-214
15
15-214
16
15-214
17
15-214
18
15-214
19
15-214
20
15-214
21
15-214
22
15-214
23
15-214
25
15-214