This commit is contained in:
2026-02-04 10:14:53 +01:00
parent 22a11a0fa6
commit 77df0f8298
3 changed files with 132 additions and 0 deletions

45
src/Ex5/Client.java Normal file
View File

@@ -0,0 +1,45 @@
package Ex5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
private final Socket socket;
/**
* Create a new client
*
* @param address server adress (needed
* @throws IOException
*/
public Client(InetSocketAddress address) throws IOException {
this.socket = new Socket(address.getAddress(), address.getPort());
}
public void sendMessage(String message) {
PrintWriter writer;
try {
writer = new PrintWriter(this.socket.getOutputStream(), true);
writer.println(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public String readMessage() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}