Guest User

Client Class

a guest
Oct 14th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. package practice;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. public class Client {
  7.  
  8. private String host;
  9. private int port;
  10.  
  11. public Client(String host, int port) {
  12.  
  13. this.setHost(host);
  14. this.setPort(port);
  15.  
  16. Thread client = new Thread(() -> {
  17. try {
  18. Socket socket = new Socket(host, port);
  19.  
  20. try (DataOutputStream output = new DataOutputStream(socket.getOutputStream());
  21. DataInputStream input = new DataInputStream(socket.getInputStream());
  22. BufferedReader console = new BufferedReader(new InputStreamReader(System.in));) {
  23.  
  24. String message = "";
  25.  
  26. while (true) {
  27.  
  28. System.out.println("what do you want to send to the Server?");
  29.  
  30. message = console.readLine();
  31.  
  32. output.writeUTF(message);
  33. output.flush();
  34. System.out.println(input.readUTF());
  35. }
  36. }
  37.  
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. });
  42. client.start();
  43. }
  44.  
  45. public String getHost() {
  46. return host;
  47. }
  48.  
  49. private void setHost(String host) {
  50. this.host = host;
  51. }
  52.  
  53. public int getPort() {
  54. return port;
  55. }
  56.  
  57. private void setPort(int port) {
  58. this.port = port;
  59. }
  60.  
  61. }
  62.  
Add Comment
Please, Sign In to add comment