SHARE
TWEET

Untitled

a guest Sep 16th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package example1.client;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.Socket;
  7.  
  8. public class Client {
  9.     public static void main(String[] args) throws Exception {
  10.         //tworze polaczenie na serwer
  11.         Socket connection = new Socket("127.0.0.1", 3344);
  12.  
  13.         //dwa strumienie ktore: pozwalaja wysylac komunikaty na serwer i pozwalaja wczytac komunikaty z serwera
  14.         PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
  15.         BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  16.         /*
  17.         za pomoca scannera wczytaj od uzywtnoika komendy ktore zostana wyslane na serwer
  18.         komendy powinny byc tak dlugo wczytywane az uzytkownik wpisze slowo END
  19.         przykladowe komendy
  20.         -> ADD#DJUPA#11222333
  21.         -> GET#DUPA
  22.         -> DEL#DUPA
  23.         -> END
  24.         -> ALA MA KOTA/ ADD#:)
  25.        
  26.        
  27.          */
  28.  
  29.         //-----------PROTOKOL-------------
  30.         out.println("ADD#MATI#600700800");
  31.         String response = in.readLine();
  32.         System.out.println("from server: "+response);
  33.  
  34.         out.println("ADD#Waldek#601700800");
  35.         response = in.readLine();
  36.         System.out.println("from server: "+response);
  37.  
  38.         out.println("ADD#Czarek#601700801");
  39.         response = in.readLine();
  40.         System.out.println("from server: "+response);
  41.  
  42.         out.println("DEL#Waldek");
  43.         response = in.readLine();
  44.         System.out.println("from server: "+response);
  45.  
  46.         out.println("GET#Waldek");
  47.         response = in.readLine();
  48.         System.out.println("from server: "+response);
  49.  
  50.         out.println("GET#MATI");
  51.         response = in.readLine();
  52.         System.out.println("from server: "+response);
  53.  
  54.         out.println("END");
  55.         //-----------PROTOKOL-------------
  56.  
  57.         in.close();
  58.         out.close();
  59.  
  60.         connection.close();
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. package example1.server;
  67.  
  68. import java.io.BufferedReader;
  69. import java.io.IOException;
  70. import java.io.InputStreamReader;
  71. import java.io.PrintWriter;
  72. import java.net.ServerSocket;
  73. import java.net.Socket;
  74. import java.util.ArrayList;
  75. import java.util.List;
  76.  
  77. public class Server {
  78.     public static void main(String[] args) throws Exception {
  79.         ServerSocket ss = new ServerSocket(3344);
  80.         System.out.println("Serwer start");
  81.  
  82.         List<PhoneNumberRow> numberRows = new ArrayList<>();
  83.  
  84.         while (true) {
  85.             Socket clientConnection = ss.accept();
  86.             processClientConnection(numberRows, clientConnection);
  87.  
  88.         }
  89.  
  90.     }
  91.  
  92.     private static void processClientConnection(List<PhoneNumberRow> numberRows, Socket clientConnection) throws IOException {
  93.         System.out.println("Polaczyl sie klient z: " + clientConnection.getInetAddress().getHostAddress());
  94.  
  95.         PrintWriter out = new PrintWriter(clientConnection.getOutputStream(), true);
  96.         BufferedReader in = new BufferedReader(new InputStreamReader(clientConnection.getInputStream()));
  97.  
  98.         //-------------PROTOKOL---------------
  99.         boolean clientConnected = true;
  100.         while (clientConnected) {
  101.             String command = in.readLine();
  102.             String[] commandArgs = command.split("#");
  103.             //["add","mati","nrtel"]
  104.             if ("ADD".equals(commandArgs[0])) {
  105.                 if (numberRows.stream().anyMatch(row -> row.getName().equals(commandArgs[1]))) {
  106.                     out.println("WPIS DLA OSOBY: " + commandArgs[1] + " juz istnieje");
  107.                 } else {
  108.                     numberRows.add(new PhoneNumberRow(commandArgs[1], commandArgs[2]));
  109.                     out.println("OK DODANO");
  110.                 }
  111.             } else if ("GET".equals(commandArgs[0])) {
  112.                 if (numberRows.stream().anyMatch(row -> row.getName().equals(commandArgs[1]))) {
  113.                     out.println("Number do: " + commandArgs[1] + " to: " + numberRows.stream().filter(row -> row.getName().equals(commandArgs[1])).findFirst().get().getNumber());
  114.                 }else{
  115.                     out.println("BRAK NUMBERU DLA: "+commandArgs[1]);
  116.                 }
  117.             } else if ("DEL".equals(commandArgs[0])) {
  118.                 if (numberRows.stream().anyMatch(row -> row.getName().equals(commandArgs[1]))) {
  119.                     numberRows.removeIf(row -> row.getName().equals(commandArgs[1]));
  120.                     out.println("USUNIETO");
  121.                 }else{
  122.                     out.println("BRAK NUMBERU DLA: "+commandArgs[1]);
  123.                 }
  124.  
  125.             } else if ("END".equals(commandArgs[0])) {
  126.                 clientConnected = false;
  127.             } else {
  128.                 out.println("nieznana komenda!");
  129.             }
  130.         }
  131.  
  132.         System.out.println(numberRows);
  133.         out.close();
  134.         in.close();
  135.         clientConnection.close();
  136.         //------------------------------------
  137.     }
  138. }
  139.  
  140.  
  141.  
  142. package example1.server;
  143.  
  144. public class PhoneNumberRow {
  145.     private String name;
  146.     private String number;
  147.  
  148.     public PhoneNumberRow(String name, String number) {
  149.         this.name = name;
  150.         this.number = number;
  151.     }
  152.  
  153.     public String getName() {
  154.         return name;
  155.     }
  156.  
  157.     public void setName(String name) {
  158.         this.name = name;
  159.     }
  160.  
  161.     public String getNumber() {
  162.         return number;
  163.     }
  164.  
  165.     public void setNumber(String number) {
  166.         this.number = number;
  167.     }
  168.  
  169.     @Override
  170.     public String toString() {
  171.         return "PhoneNumberRow{" +
  172.                 "name='" + name + '\'' +
  173.                 ", number='" + number + '\'' +
  174.                 '}';
  175.     }
  176. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top