Advertisement
Guest User

siuap

a guest
Dec 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.io.IOException;
  4.  
  5. class Odbior extends Thread {
  6. Socket sock;
  7. BufferedReader sockReader;
  8. PrintWriter sockWriter;
  9.  
  10. public Odbior(Socket sock) throws IOException {
  11. this.sock = sock;
  12. this.sockReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  13. }
  14.  
  15. public void run() {
  16. try {
  17. String str;
  18. while(true) {
  19. if (sockReader.ready()) {
  20. str = this.sockReader.readLine();
  21. if (str.equalsIgnoreCase("koniec")) {
  22. sock.shutdownOutput();
  23. break;
  24. }
  25. System.out.println(str);
  26. }
  27. else if (sock.isInputShutdown()) {
  28. break;
  29. }
  30. }
  31. }
  32. catch (Exception e) {
  33. e.printStackTrace();;
  34. }
  35. }
  36. }
  37.  
  38. class Nadaj extends Thread
  39. {
  40. Socket sock;
  41. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  42. PrintWriter sockWriter;
  43. String nick;
  44.  
  45. public Nadaj(Socket sock, String nick) throws IOException
  46. {
  47. this.sock = sock;
  48. this.nick = nick;
  49. this.sockWriter = new PrintWriter(sock.getOutputStream());
  50. }
  51.  
  52. public void run() {
  53. try {
  54. PrintWriter out = this.sockWriter;
  55. String str;
  56. while (true) {
  57. if(in.ready()) {
  58. str = in.readLine();
  59. if (str.equalsIgnoreCase("koniec")) {
  60. out.println(str);
  61. sock.shutdownInput();
  62. in.close();
  63. break;
  64. }
  65. out.println(this.nick + " " + str);
  66. } else if (sock.isOutputShutdown()) break;
  67. }
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73.  
  74. public class Klient
  75. {
  76. public static final int PORT=50007;
  77. public static final String HOST = "155.158.131.155";
  78.  
  79. public static void main(String[] args) throws IOException
  80. {
  81. //nawiazanie polaczenia z serwerem
  82. Socket sock;
  83. sock=new Socket(HOST, PORT);
  84. System.out.println("Nawiazalem polaczenie: "+sock);
  85.  
  86. //tworzenie watka odbierajacego
  87. Odbior odbior = new Odbior(sock);
  88. Nadaj nadaj = new Nadaj(sock, "Gaspar");
  89. odbior.start();
  90. nadaj.start();
  91. while(true) {
  92. if (!nadaj.isAlive() | !odbior.isAlive()) {
  93. break;
  94. }
  95. }
  96.  
  97. System.out.println("Koniec poloczenia");
  98.  
  99. //zamykanie polaczenia
  100. sock.close();
  101. }
  102. }
  103.  
  104.  
  105. *********************************************************************************************************************
  106.  
  107. import java.io.*;
  108. import java.net.*;
  109. import java.util.*;
  110.  
  111. public class SerwerMulti {
  112. public static final int PORT=5007;
  113. private static Set<String>names = new HashSet<>();
  114. private static Set<PrintWriter>senders = new HashSet<>();
  115.  
  116. public static void main(String args[]) throws IOException {
  117. ServerSocket server = new ServerSocket(PORT);
  118. Socket client;
  119.  
  120. System.out.println("Waiting for connection to: " + server);
  121. while(true) {
  122. client = server.accept();
  123. new ClientHandler(client).start();
  124. }
  125. }
  126.  
  127. private static class ClientHandler extends Thread {
  128. private String name;
  129. private Socket client;
  130.  
  131. private BufferedReader clientReceiver;
  132. private PrintWriter clientSender;
  133. public ClientHandler(Socket client) {
  134. this.client = client;
  135. }
  136.  
  137. public void run() {
  138. try {
  139. this.clientReceiver = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
  140. this.clientSender = new PrintWriter(this.client.getOutputStream());
  141.  
  142. this.clientSender.println("Your name?");
  143. this.clientSender.flush();
  144.  
  145. while(true) {
  146. if (this.clientReceiver.ready()) {
  147. this.name = this.clientReceiver.readLine();
  148. synchronized (names) {
  149. if (!this.name.trim().isEmpty() && !names.contains(name)) {
  150. names.add(this.name);
  151.  
  152. this.clientSender.println("Welcome "+this.name+"!");
  153. this.clientSender.flush();
  154. break;
  155. } else {
  156. this.clientSender.println("Name: "+this.name+" is already in use!\n"+"Your name?");
  157. this.clientSender.flush();
  158. }
  159. }
  160. }
  161. }
  162. for (PrintWriter sender: senders) {
  163. sender.println(this.name + "has joined");
  164. sender.flush();
  165. }
  166. senders.add(this.clientSender);
  167. String message = "";
  168. boolean exit = false;
  169.  
  170. while(!exit) {
  171. if (this.clientReceiver.ready()) {
  172. message = this.clientReceiver.readLine();
  173.  
  174. if(!message.equalsIgnoreCase("end")) {
  175. message = "<" + this.name + ">:" + message;
  176. } else {
  177. message = this.name + "has left";
  178. exit = true;
  179. }
  180. for (PrintWriter sender: senders) {
  181. if(sender != this.clientSender) {
  182. sender.println(message);
  183. sender.flush();
  184. }
  185. }
  186. }
  187. }
  188. senders.remove(this.clientSender);
  189. names.remove(this.name);
  190.  
  191. this.clientReceiver.close();
  192. this.clientSender.close();
  193. this.client.close();
  194. }
  195.  
  196. catch (IOException e) {
  197. System.out.println(e);
  198. }
  199. }
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement