Advertisement
Guest User

Untitled

a guest
Dec 21st, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.01 KB | None | 0 0
  1. //SERVER
  2.  
  3. import java.io.IOException;
  4. import java.net.BindException;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.LinkedList;
  8.  
  9. public class Server extends Thread{
  10.  
  11.     public static LinkedList <Korisnik> korisnici = new LinkedList <Korisnik>();
  12.     public static LinkedList <ServerNit> niti = new LinkedList <ServerNit>();
  13.     public static void main(String[] args) {
  14.        
  15.         int port = 10000;
  16.         ServerSocket serverSoket = null;
  17.         Socket soketZaKomunikaciju = null;
  18.         try {
  19.             serverSoket = new ServerSocket(port);
  20.            
  21.             while(true){
  22.                 System.out.println("Cekam na konekciju...");
  23.                 soketZaKomunikaciju = serverSoket.accept();
  24.                 System.out.println("Konekcija je uspostavljena");
  25.                 ServerNit klijent = new ServerNit(soketZaKomunikaciju);
  26.                 niti.add(klijent);
  27.                 klijent.start();
  28.             }
  29.         }catch (BindException e){
  30.             System.out.println("Port je zauzet.");
  31.         }
  32.         catch (IOException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.     }
  37.  
  38. }
  39.  
  40. //KLIJENT
  41.  
  42. import java.io.BufferedReader;
  43. import java.io.DataOutput;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.io.InputStreamReader;
  47. import java.io.PrintStream;
  48. import java.io.RandomAccessFile;
  49. import java.net.Socket;
  50. import java.net.UnknownHostException;
  51.  
  52. import org.omg.CORBA.portable.InputStream;
  53.  
  54. public class Klijent implements Runnable{
  55.  
  56.     static Socket soketZaKomunikaciju= null;  
  57.     static BufferedReader inputSaKonzole = null;
  58.     static BufferedReader input = null;
  59.     static PrintStream output = null;
  60.     static   InputStream inbyte= null;
  61.    
  62.     public static void main(String[] args) {
  63.        
  64.         try {
  65.             soketZaKomunikaciju=new Socket("localhost",10000);
  66.             inputSaKonzole = new BufferedReader(new InputStreamReader(System.in));
  67.             input = new BufferedReader(new InputStreamReader(soketZaKomunikaciju.getInputStream()));
  68.             output = new PrintStream(soketZaKomunikaciju.getOutputStream());
  69.            
  70.             new Thread(new Klijent()).start();
  71.             String str;
  72.             while(true){
  73.                 str = input.readLine();
  74.                 System.out.println(str);
  75.                
  76.                 if(str.startsWith("Dovidjenja"))  
  77.                     break;
  78.             }
  79.            
  80.             soketZaKomunikaciju.close();
  81.         } catch (UnknownHostException e) {
  82.             // TODO Auto-generated catch block
  83.             e.printStackTrace();
  84.         } catch (IOException e) {
  85.             System.out.println("SERVER PAO!");
  86.         }
  87.  
  88.     }
  89.  
  90.     @Override
  91.     public void run() {
  92.        
  93.         try {
  94.             String poruka;
  95.            
  96.             while(true){
  97.                 poruka = inputSaKonzole.readLine();
  98.                 output.println(poruka);
  99.                 if(poruka.equals("quit")){
  100.                     break;
  101.                 }
  102.             }
  103.            
  104.         } catch (IOException e) {
  105.             // TODO Auto-generated catch block
  106.             e.printStackTrace();
  107.         }
  108.        
  109.     }
  110.  
  111. }
  112.  
  113. //SERVERNIT
  114.  
  115. import java.io.BufferedReader;
  116. import java.io.BufferedWriter;
  117. import java.io.DataOutput;
  118. import java.io.File;
  119. import java.io.FileOutputStream;
  120. import java.io.FileReader;
  121. import java.io.FileWriter;
  122. import java.io.IOException;
  123. import java.io.InputStreamReader;
  124. import java.io.OutputStream;
  125. import java.io.PrintStream;
  126. import java.io.PrintWriter;
  127. import java.io.RandomAccessFile;
  128. import java.net.Socket;
  129. import java.util.Random;
  130.  
  131. import org.omg.CORBA.portable.InputStream;
  132.  
  133. public class ServerNit extends Thread{
  134.  
  135.     Socket soketNit = null;
  136.     String username = null;
  137.     String password = null;
  138.     static int procitaniBajtovi=0;
  139.     public static byte[] niz = new byte[1024];
  140.    
  141.     public ServerNit(Socket soket) {
  142.         soketNit=soket;
  143.     }
  144.  
  145.     @Override
  146.     public void run() {
  147.         try {
  148.             while (true) {
  149.                 PrintStream out = new PrintStream(soketNit.getOutputStream());
  150.                 BufferedReader in = new BufferedReader(new InputStreamReader(soketNit.getInputStream()));
  151.                 OutputStream outbyte = soketNit.getOutputStream();
  152.  
  153.                 out.println("Izaberite opciju: \n1. Upload fajlova\n2. Download fajlova\nZa prekid komunikacije ukucajte quit");
  154.                 out.flush();
  155.                 String opcija=in.readLine();
  156.                 if (opcija.equals("1")) {
  157.                     boolean z = false;
  158.                     boolean pom = false;
  159.                     boolean x = false;
  160.                     String karakteri = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  161.                     while (x == false) {
  162.                         out.println("Da li zelite da se registrujete? Da/Ne");
  163.                         out.flush();
  164.                         String izbor=in.readLine();
  165.                         if (izbor.equals("Da")) {
  166.                             out.println("Unesite korisnicko ime: ");
  167.                             out.flush();
  168.                             username = in.readLine();
  169.                             out.println("Unesite lozinku: ");
  170.                             out.flush();
  171.                             password = in.readLine();
  172.                             for (int i = 0; i < Server.korisnici.size(); i++) {
  173.                                 if (Server.korisnici.get(i).username.equals(username)) {
  174.                                     out.println("Vec ste registrovani!");
  175.                                     out.flush();
  176.                                     pom = true;
  177.                                     break;
  178.                                 }
  179.                             }
  180.                             if (pom == false) {
  181.                                 Korisnik korisnik = new Korisnik(username, password);
  182.                                 Server.korisnici.add(korisnik);
  183.                                 x = true;
  184.                             }
  185.  
  186.                         } else if (izbor.equals("Ne")) {
  187.                             out.println("Ako zelite da nastavite sa upload-om, molimo Vas da se prijavite");
  188.                             out.flush();
  189.                             out.println("Unesite korisnicko ime: ");
  190.                             out.flush();
  191.                             username = in.readLine();
  192.                             out.println("Unesite lozinku: ");
  193.                             out.flush();
  194.                             password = in.readLine();
  195.                             for (int i = 0; i < Server.korisnici.size(); i++) {
  196.                                 if (Server.korisnici.get(i).username.equals(username)
  197.                                         && Server.korisnici.get(i).password.equals(password)) {
  198.                                     out.println("Uspesno ste se prijavili!");
  199.                                     out.flush();
  200.                                     pom = true;
  201.                                     x = true;
  202.                                     break;
  203.                                 }
  204.                             }
  205.                             if (pom == false) {
  206.                                 out.println("Neuspesno prijavljivanje!");
  207.                             }
  208.  
  209.                         } else {
  210.                             out.println("Greska!! Unesite Da ili Ne!");
  211.                         }
  212.                     }
  213.  
  214.                     while (z == false) {
  215.                         out.println("Mozete uneti najvise 500 karaktera");
  216.                         out.flush();
  217.                         String tekst=in.readLine();
  218.                         if (tekst.length() > 500) {
  219.                             out.println("Greska! Uneli ste vise od 500 karaktera!");
  220.                         } else {
  221.                             out.println("Unesite naziv fajla: ");
  222.                             out.flush();
  223.                             String naziv = in.readLine();
  224.                             String id = "";
  225.                             Random r = new Random();
  226.                             for (int i = 0; i < 10; i++) {
  227.                                 id = id + karakteri.charAt(r.nextInt(karakteri.length()));
  228.                             }
  229.                             out.println("Kod za upload-ovani fajl je " + id);
  230.                             out.flush();
  231.                             for (int i = 0; i < Server.korisnici.size(); i++) {
  232.                                 if (Server.korisnici.get(i).username.equals(username)) {
  233.                                     Fajl f = new Fajl(id);
  234.                                     f.fajl = new File(naziv);
  235.                                     RandomAccessFile raf = new RandomAccessFile(f.fajl, "rw");
  236.                                     raf.writeUTF(tekst);
  237.                                     raf.close();
  238.                                     Server.korisnici.get(i).fajlovi.add(f);
  239.                                 }
  240.                             }
  241.                             out.println("Da li zelite da vidite koje ste fajlove upload-ovali? Da/Ne");
  242.                             out.flush();
  243.                             String izbor2=in.readLine();
  244.                             if (izbor2.equals("Da")) {
  245.                                 for (int i = 0; i < Server.korisnici.size(); i++) {
  246.                                     if (Server.korisnici.get(i).username.equals(username)) {
  247.                                         for (int j = 0; j < Server.korisnici.get(i).fajlovi.size(); j++) {
  248.                                             out.println("Kod za fajl: " + Server.korisnici.get(i).fajlovi.get(j).id + " Naziv fajla: "
  249.                                                     + Server.korisnici.get(i).fajlovi.get(j).fajl.getName()+"\n");
  250.                                         }
  251.                                     }
  252.                                 }
  253.                             }
  254.                             z = true;
  255.                         }
  256.                     }
  257.                 } else if (opcija.equals("2")) {
  258.                     out.println("Unesite kod za fajl koji zelite da download-ujete");
  259.                     out.flush();
  260.                     String kod=in.readLine();
  261.                     boolean y = false;
  262.                     for (int i = 0; i < Server.korisnici.size(); i++) {
  263.                         for (int j = 0; j < Server.korisnici.get(i).fajlovi.size(); j++) {
  264.                             if (Server.korisnici.get(i).fajlovi.get(j).id.equals(kod)) {
  265.                                 RandomAccessFile citanje = new RandomAccessFile(Server.korisnici.get(i).fajlovi.get(j).fajl, "rw");
  266.                                 RandomAccessFile download= new RandomAccessFile(Server.korisnici.get(i).fajlovi.get(j).fajl.getName(), "rw");
  267.                                 while(true){
  268.                                     procitaniBajtovi = citanje.read(niz);
  269.                                     if(procitaniBajtovi == -1){
  270.                                         break;
  271.                                     }
  272.                                     out.println("Sadrzaj fajla: ");
  273.                                     out.flush();
  274.                                     outbyte.write(niz,0,procitaniBajtovi);
  275.                                     download.write(niz,0,procitaniBajtovi);
  276.                                     out.println("\n");
  277.                                 }
  278.                                 citanje.close();
  279.                                 download.close();
  280.                                 y = true;
  281.                                 break;
  282.                             }
  283.                         }
  284.                     }
  285.                     if (y == false) {
  286.                         out.println("Ne postoji fajl sa unetim kodom");
  287.                     }
  288.                 } else {
  289.                     out.println("Morate izabrati 1 ili 2!");
  290.                 }
  291.             }
  292.        
  293.            
  294.         } catch (IOException e) {
  295.             // TODO Auto-generated catch block
  296.             e.printStackTrace();
  297.         }
  298.     }  
  299.  
  300. }
  301.  
  302. //KORISNIK
  303.  
  304. import java.io.File;
  305. import java.util.LinkedList;
  306.  
  307. public class Korisnik {
  308.  
  309.     public String username;
  310.     public String password;
  311.     public LinkedList <Fajl> fajlovi = new LinkedList <Fajl>();
  312.    
  313.     public Korisnik(String ime, String lozinka){
  314.         username = ime;
  315.         password = lozinka;
  316.        
  317.     }
  318. }
  319.  
  320. //FAJL
  321.  
  322. import java.io.File;
  323.  
  324. public class Fajl {
  325.  
  326.     public String id;
  327.     public File fajl;
  328.    
  329.     public Fajl(String id){
  330.         this.id=id;
  331.     }
  332.    
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement