Advertisement
Domy131097

LV8: Zad1

Jan 21st, 2018
121
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None
  1. package Zadatak1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.io.PrintWriter;
  8.  
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11.  
  12. public class Posluzitelj {
  13.  
  14. public static void main(String[] args) {
  15. ServerSocket server = null;
  16. String linija;
  17. BufferedReader is;
  18. PrintWriter os;
  19. Socket klijent = null;
  20. try {
  21. server = new ServerSocket(1000, 2);
  22. System.out.println("Postavljeni posluzitelj: "+ server.toString());
  23. }
  24. catch(IOException greska){
  25. System.err.println("Greska");
  26. System.exit(1);
  27. }
  28. try {
  29. klijent = server.accept();
  30. System.out.println("SERVER: Imamo klijenta" + klijent.getInetAddress());
  31. is = new BufferedReader(new InputStreamReader(klijent.getInputStream()));
  32. os = new PrintWriter(new OutputStreamWriter(klijent.getOutputStream()),true);
  33. boolean cenza = false;
  34. int ids = is.read();
  35. String cenzura[] = {"Psovka1", "Psovka2", "Psovka3"};
  36. while ((linija = is.readLine()) != null) {
  37. cenza = false;
  38. for(int i=0;i<cenzura.length; ++i) {
  39. if(linija.equals(cenzura[i])) {
  40. for(int j = 0; j < cenzura[i].length(); j++) System.out.print("*");
  41. System.out.print("\n");
  42. cenza = true;
  43. }
  44. }
  45. if(cenza == false) System.out.println("(ID:" + ids + ") " + linija);
  46. }
  47. }
  48. catch(IOException g) {
  49. System.err.println("Greska");
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. package Zadatak1;
  57.  
  58. import java.io.BufferedReader;
  59. import java.io.IOException;
  60. import java.io.InputStream;
  61. import java.io.InputStreamReader;
  62. import java.io.OutputStream;
  63. import java.io.PrintStream;
  64. import java.io.PrintWriter;
  65. import java.net.Socket;
  66. import java.util.Scanner;
  67.  
  68. public class Klijent {
  69. public int ID;
  70. public Klijent() {
  71. ID = (int )(Math.random() * 50 + 1);
  72. }
  73. public int getID() {
  74. return ID;
  75. }
  76. public void setID(int id){
  77. ID = id;
  78. }
  79. public static void main(String[] args){
  80. try {
  81. Klijent user = new Klijent();
  82. Socket klijent = new Socket("127.0.0.1", 1000);
  83. System.out.println("KLIJENT (ID:" + user.getID() + "): " + klijent);
  84. InputStream in = klijent.getInputStream();
  85. OutputStream ou = klijent.getOutputStream();
  86.  
  87. BufferedReader bis = new BufferedReader(new InputStreamReader(in));
  88. PrintWriter ps = new PrintWriter(new PrintStream(ou), true);
  89.  
  90. Scanner sc = new Scanner(System.in);
  91. ps.write(user.getID());
  92. String linija=null;
  93. while((linija=sc.next()) != null) {
  94. ps.println(linija);
  95. }
  96. }
  97. catch(IOException greska){
  98. System.err.println("Greska");
  99. }
  100. }
  101. }
Advertisement
RAW Paste Data Copied
Advertisement