Advertisement
DoGy70

Untitled

Dec 4th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintStream;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class Server
  10. {
  11. public static void main(String[] args)
  12. {
  13. new Server().start();
  14. }
  15.  
  16. private List<PrintStream> clients;
  17. private final Object clientsLock;
  18.  
  19. private class ServerRunnable implements Runnable
  20. {
  21. private final Socket client;
  22.  
  23. public ServerRunnable(Socket client)
  24. {
  25. this.client = client;
  26. }
  27.  
  28. @Override
  29. public void run()
  30. {
  31. try (Scanner in = new Scanner(client.getInputStream());
  32. PrintStream out = new PrintStream(client.getOutputStream()))
  33. {
  34. synchronized (clientsLock)
  35. {
  36. clients.add(out);
  37. }
  38.  
  39. while (true)
  40. {
  41. String line = in.nextLine();
  42. if (line.contains("quit"))
  43. {
  44. synchronized (clientsLock)
  45. {
  46. clients.remove(out);
  47. return;
  48. }
  49. }
  50. synchronized (clientsLock)
  51. {
  52. for (PrintStream c : clients)
  53. c.println(line);
  54. }
  55. }
  56. }
  57. catch (IOException e)
  58. {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. public Server()
  65. {
  66. clients = new ArrayList<>();
  67. clientsLock = new Object();
  68. }
  69.  
  70. public void start()
  71. {
  72. try
  73. {
  74. ServerSocket serverSocket = new ServerSocket(8080);
  75.  
  76. while (true)
  77. {
  78. Socket client = serverSocket.accept();
  79. Thread clientThread = new Thread(new ServerRunnable(client));
  80. clientThread.start();
  81. }
  82.  
  83. }
  84. catch (IOException e)
  85. {
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90.  
  91. CLIENTTTTTTT_---------------------------------------------
  92. import java.io.IOException;
  93. import java.io.PrintStream;
  94. import java.net.Socket;
  95. import java.util.Scanner;
  96.  
  97. public class Client {
  98. public static void main(String[] args) {
  99. new Client().start();
  100. }
  101.  
  102. private String myName;
  103. private volatile boolean shouldStop;
  104.  
  105. public Client() {
  106. myName = null;
  107. shouldStop = false;
  108. }
  109.  
  110. public void start() {
  111. try {
  112. Socket server = new Socket("localhost", 8080);
  113.  
  114. Scanner in = new Scanner(server.getInputStream());
  115. PrintStream out = new PrintStream(server.getOutputStream());
  116.  
  117. Thread reader = new Thread(() ->
  118. {
  119. while (!shouldStop) {
  120. if (in.hasNextLine()) {
  121. String line = in.nextLine();
  122. if (!line.startsWith(myName))
  123. System.out.println(line);
  124. }
  125. }
  126. });
  127.  
  128. Thread writer = new Thread(() ->
  129. {
  130. Scanner console = new Scanner(System.in);
  131.  
  132. System.out.println("Enter name:");
  133. myName = console.nextLine();
  134.  
  135. String line = null;
  136.  
  137. while (true) {
  138. line = console.nextLine();
  139. out.println(myName + ": " + line);
  140. if (line.contains("quit")) {
  141. shouldStop = true;
  142. try {
  143. server.close();
  144. in.close();
  145. out.close();
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149. return;
  150. }
  151. }
  152. });
  153.  
  154. reader.start();
  155. writer.start();
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement