clean up fhs
This commit is contained in:
53
praktikum-5/aufgabe-4/Client.java
Normal file
53
praktikum-5/aufgabe-4/Client.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package praktikum5.aufgabe4;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Scanner;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class Client {
|
||||
private int port;
|
||||
|
||||
public Client(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String send(String msg) throws IOException {
|
||||
var socket = new Socket("localhost", port);
|
||||
var out = socket.getOutputStream();
|
||||
var in = socket.getInputStream();
|
||||
var reader = new InputStreamReader(in);
|
||||
var writer = new OutputStreamWriter(out);
|
||||
|
||||
writer.write(msg);
|
||||
writer.flush();
|
||||
socket.shutdownOutput();
|
||||
|
||||
var response = "";
|
||||
var curr = reader.read();
|
||||
while (curr != -1) {
|
||||
response += (char)curr;
|
||||
curr = reader.read();
|
||||
}
|
||||
reader.close();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
int port = 4711;
|
||||
var client = new Client(port);
|
||||
var reader = new Scanner(System.in);
|
||||
while (true) {
|
||||
var msg = reader.nextLine();
|
||||
var response = client.send(msg);
|
||||
System.out.println("Response: " + response);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user