Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package practice;
- import java.io.*;
- import java.net.*;
- public class Client {
- private String host;
- private int port;
- public Client(String host, int port) {
- this.setHost(host);
- this.setPort(port);
- Thread client = new Thread(() -> {
- try {
- Socket socket = new Socket(host, port);
- try (DataOutputStream output = new DataOutputStream(socket.getOutputStream());
- DataInputStream input = new DataInputStream(socket.getInputStream());
- BufferedReader console = new BufferedReader(new InputStreamReader(System.in));) {
- String message = "";
- while (true) {
- System.out.println("what do you want to send to the Server?");
- message = console.readLine();
- output.writeUTF(message);
- output.flush();
- System.out.println(input.readUTF());
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- client.start();
- }
- public String getHost() {
- return host;
- }
- private void setHost(String host) {
- this.host = host;
- }
- public int getPort() {
- return port;
- }
- private void setPort(int port) {
- this.port = port;
- }
- }
Add Comment
Please, Sign In to add comment