Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. client
  2.  
  3. package LV8;
  4. import java.net.*;
  5. import java.io.*;
  6. //OBA FILE-A OVDJE SU COPYPASTE SA NEKE STRANICE NA NETU
  7. public class Client
  8. { private Socket socket = null;
  9. private DataInputStream console = null;
  10. private DataOutputStream streamOut = null;
  11.  
  12. public Client(String serverName, int serverPort)
  13. { System.out.println("Establishing connection. Please wait ...");
  14. try
  15. { socket = new Socket(serverName, serverPort);
  16. System.out.println("Connected: " + socket);
  17. start();
  18. }
  19. catch(UnknownHostException uhe)
  20. { System.out.println("Host unknown: " + uhe.getMessage());
  21. }
  22. catch(IOException ioe)
  23. { System.out.println("Unexpected exception: " + ioe.getMessage());
  24. }
  25. String line = "";
  26. while (!line.equals(".bye"))
  27. { try
  28. { line = console.readLine();
  29. streamOut.writeUTF(line);
  30. streamOut.flush();
  31. }
  32. catch(IOException ioe)
  33. { System.out.println("Sending error: " + ioe.getMessage());
  34. }
  35. }
  36. }
  37. public void start() throws IOException
  38. { console = new DataInputStream(System.in);
  39. streamOut = new DataOutputStream(socket.getOutputStream());
  40. }
  41. public void stop()
  42. { try
  43. { if (console != null) console.close();
  44. if (streamOut != null) streamOut.close();
  45. if (socket != null) socket.close();
  46. }
  47. catch(IOException ioe)
  48. { System.out.println("Error closing ...");
  49. }
  50. }
  51. public static void main(String args[])
  52. { Client client = new Client("0.0.0.0", 8081);
  53. if (args.length != 2)
  54. System.out.println("Usage: java ChatClient host port");
  55. else
  56. client = new Client(args[0], Integer.parseInt(args[1]));
  57. }
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61.  
  62.  
  63.  
  64. server
  65.  
  66. package LV8;
  67.  
  68. import java.io.*;
  69. import java.net.*;
  70. import java.util.*;
  71.  
  72. public class Server {
  73. private ServerSocket serverSocket;
  74. private Socket clientSocket;
  75. private DataInputStream streamInput;
  76. private List<String> BannedWords = Arrays.asList("psovka1", "psovka2", "psovka3", "uvreda1, uvreda2, uvreda3");
  77. public Server(int port) {
  78.  
  79. try {
  80. System.out.println("Creating server with port: " + port + "...");
  81. serverSocket = new ServerSocket(port);
  82. System.out.println("Server started: " + serverSocket);
  83. System.out.println("Waiting for a client...");
  84. clientSocket = serverSocket.accept();
  85. System.out.println("Client accepted:" + clientSocket);
  86. streamInput = new DataInputStream((new BufferedInputStream(clientSocket.getInputStream())));
  87.  
  88. boolean done = false;
  89. while(!done) {
  90. try {
  91. String line = streamInput.readUTF();
  92. for(String word: BannedWords) {
  93. if(line.toLowerCase().contains(word)) {
  94. line = line.replace(word, "***");
  95.  
  96. }
  97. }
  98. System.out.println(line);
  99. done = line.equals(".bye");
  100. } catch (IOException ex) {
  101. done = true;
  102. }
  103. }
  104. if(clientSocket != null)
  105. clientSocket.close();
  106. if(streamInput != null)
  107. streamInput.close();
  108. } catch (IOException ex) {
  109. System.out.println("Error:" + ex.getMessage());
  110. }
  111. }
  112. public static void main(String args[])
  113. {
  114. Server server = new Server(8081);
  115. if (args.length != 1)
  116. System.out.println("Usage: java ChatServer port");
  117. else
  118. server = new Server(Integer.parseInt(args[0]));
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement