chatprogramm was sonst
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

1 year ago
  1. package client.net;
  2. import client.Main;
  3. import client.util.CeasarChipher;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.net.Socket;
  8. public class Client {
  9. private Socket s;
  10. private DataInputStream inputStream;
  11. private DataOutputStream outputStream;
  12. private Thread t;
  13. public Client(String host, int port) {
  14. try {
  15. s = new Socket(host, port);
  16. inputStream = new DataInputStream(s.getInputStream());
  17. outputStream = new DataOutputStream(s.getOutputStream());
  18. t = new Thread(() -> {
  19. try {
  20. while (true) {
  21. int len = inputStream.readInt();
  22. byte[] buf = new byte[len];
  23. inputStream.readFully(buf, 0, len);
  24. //TEST
  25. System.out.println(new String(CeasarChipher.decrypt(buf)));
  26. }
  27. } catch (IOException e) {
  28. Main.console.error("An error occurred while reading");
  29. e.printStackTrace();
  30. }
  31. });
  32. t.start();
  33. } catch (Throwable e) {
  34. Main.console.error("An error occurred while creating client!");
  35. e.printStackTrace();
  36. }
  37. }
  38. public void write(byte[] buf) {
  39. try {
  40. outputStream.writeInt(buf.length);
  41. outputStream.write(CeasarChipher.encrypt(buf));
  42. } catch (Throwable e) {
  43. server.Main.console.error("An error occurred while writing data!");
  44. e.printStackTrace();
  45. }
  46. }
  47. }