Skip to content
Snippets Groups Projects
Commit 7f3152e0 authored by Chao Zhan's avatar Chao Zhan
Browse files

init tutorial 10

parent 377a8b5d
Branches
No related tags found
No related merge requests found
# ALP4 Tutorial-9 # ALP4 Tutorial-10
This branch contains all materials for the 9th tutorial session. This branch contains all materials for the 9th tutorial session.
## Agenda ## Agenda
- Assignment's solution presentation - Assignment's solution presentation
- Recap & Discussion: Java - Recap & Discussion: IPC, RPC, HTTP(S)
- Q&A - Q&A
package Client;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
public class TCPClient {
public static void main(String args[]) throws IOException {
int PORT = 12345;
String HOST = "127.0.0.1";
Socket socket = new Socket(HOST, PORT);
PrintStream out = new PrintStream(socket.getOutputStream());
// BufferedReader reads Keyboard Input
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
while (true) { // ^D will close dialogue
System.out.print("> ");
String message = keyboard.readLine();
if (message == null)
break;
out.println(message);
String answer = in.readLine();
System.out.println("echo: " + answer);
}
in.close();
keyboard.close();
out.close();
socket.close();
}
}
package Server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String args[]) throws IOException {
int PORT = 12345;
ServerSocket listen = new ServerSocket(PORT); // set port
System.out.printf("Listening on the port: %d\n", PORT);
while (true) { // non-terminating server
Socket socket = listen.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());
while (true) { // end of input stream will close dialogue
String message = in.readLine();
if (message == null) {
break;
}
String answer = message.replace('i', 'o');
out.println(answer);
}
in.close();
out.close();
socket.close(); // close connection
System.out.println("Socket closed.");
}
}
}
...@@ -16,7 +16,7 @@ transition: fade-out ...@@ -16,7 +16,7 @@ transition: fade-out
css: unocss css: unocss
--- ---
# ALP4 Tutorial 9 # ALP4 Tutorial 10
## Chao Zhan ## Chao Zhan
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment