ex5
This commit is contained in:
45
src/Ex5/Client.java
Normal file
45
src/Ex5/Client.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user