Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SERVER
- import java.io.IOException;
- import java.net.BindException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.LinkedList;
- public class Server extends Thread{
- public static LinkedList <Korisnik> korisnici = new LinkedList <Korisnik>();
- public static LinkedList <ServerNit> niti = new LinkedList <ServerNit>();
- public static void main(String[] args) {
- int port = 10000;
- ServerSocket serverSoket = null;
- Socket soketZaKomunikaciju = null;
- try {
- serverSoket = new ServerSocket(port);
- while(true){
- System.out.println("Cekam na konekciju...");
- soketZaKomunikaciju = serverSoket.accept();
- System.out.println("Konekcija je uspostavljena");
- ServerNit klijent = new ServerNit(soketZaKomunikaciju);
- niti.add(klijent);
- klijent.start();
- }
- }catch (BindException e){
- System.out.println("Port je zauzet.");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //KLIJENT
- import java.io.BufferedReader;
- import java.io.DataOutput;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.io.RandomAccessFile;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import org.omg.CORBA.portable.InputStream;
- public class Klijent implements Runnable{
- static Socket soketZaKomunikaciju= null;
- static BufferedReader inputSaKonzole = null;
- static BufferedReader input = null;
- static PrintStream output = null;
- static InputStream inbyte= null;
- public static void main(String[] args) {
- try {
- soketZaKomunikaciju=new Socket("localhost",10000);
- inputSaKonzole = new BufferedReader(new InputStreamReader(System.in));
- input = new BufferedReader(new InputStreamReader(soketZaKomunikaciju.getInputStream()));
- output = new PrintStream(soketZaKomunikaciju.getOutputStream());
- new Thread(new Klijent()).start();
- String str;
- while(true){
- str = input.readLine();
- System.out.println(str);
- if(str.startsWith("Dovidjenja"))
- break;
- }
- soketZaKomunikaciju.close();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("SERVER PAO!");
- }
- }
- @Override
- public void run() {
- try {
- String poruka;
- while(true){
- poruka = inputSaKonzole.readLine();
- output.println(poruka);
- if(poruka.equals("quit")){
- break;
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //SERVERNIT
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.DataOutput;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.io.RandomAccessFile;
- import java.net.Socket;
- import java.util.Random;
- import org.omg.CORBA.portable.InputStream;
- public class ServerNit extends Thread{
- Socket soketNit = null;
- String username = null;
- String password = null;
- static int procitaniBajtovi=0;
- public static byte[] niz = new byte[1024];
- public ServerNit(Socket soket) {
- soketNit=soket;
- }
- @Override
- public void run() {
- try {
- while (true) {
- PrintStream out = new PrintStream(soketNit.getOutputStream());
- BufferedReader in = new BufferedReader(new InputStreamReader(soketNit.getInputStream()));
- OutputStream outbyte = soketNit.getOutputStream();
- out.println("Izaberite opciju: \n1. Upload fajlova\n2. Download fajlova\nZa prekid komunikacije ukucajte quit");
- out.flush();
- String opcija=in.readLine();
- if (opcija.equals("1")) {
- boolean z = false;
- boolean pom = false;
- boolean x = false;
- String karakteri = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- while (x == false) {
- out.println("Da li zelite da se registrujete? Da/Ne");
- out.flush();
- String izbor=in.readLine();
- if (izbor.equals("Da")) {
- out.println("Unesite korisnicko ime: ");
- out.flush();
- username = in.readLine();
- out.println("Unesite lozinku: ");
- out.flush();
- password = in.readLine();
- for (int i = 0; i < Server.korisnici.size(); i++) {
- if (Server.korisnici.get(i).username.equals(username)) {
- out.println("Vec ste registrovani!");
- out.flush();
- pom = true;
- break;
- }
- }
- if (pom == false) {
- Korisnik korisnik = new Korisnik(username, password);
- Server.korisnici.add(korisnik);
- x = true;
- }
- } else if (izbor.equals("Ne")) {
- out.println("Ako zelite da nastavite sa upload-om, molimo Vas da se prijavite");
- out.flush();
- out.println("Unesite korisnicko ime: ");
- out.flush();
- username = in.readLine();
- out.println("Unesite lozinku: ");
- out.flush();
- password = in.readLine();
- for (int i = 0; i < Server.korisnici.size(); i++) {
- if (Server.korisnici.get(i).username.equals(username)
- && Server.korisnici.get(i).password.equals(password)) {
- out.println("Uspesno ste se prijavili!");
- out.flush();
- pom = true;
- x = true;
- break;
- }
- }
- if (pom == false) {
- out.println("Neuspesno prijavljivanje!");
- }
- } else {
- out.println("Greska!! Unesite Da ili Ne!");
- }
- }
- while (z == false) {
- out.println("Mozete uneti najvise 500 karaktera");
- out.flush();
- String tekst=in.readLine();
- if (tekst.length() > 500) {
- out.println("Greska! Uneli ste vise od 500 karaktera!");
- } else {
- out.println("Unesite naziv fajla: ");
- out.flush();
- String naziv = in.readLine();
- String id = "";
- Random r = new Random();
- for (int i = 0; i < 10; i++) {
- id = id + karakteri.charAt(r.nextInt(karakteri.length()));
- }
- out.println("Kod za upload-ovani fajl je " + id);
- out.flush();
- for (int i = 0; i < Server.korisnici.size(); i++) {
- if (Server.korisnici.get(i).username.equals(username)) {
- Fajl f = new Fajl(id);
- f.fajl = new File(naziv);
- RandomAccessFile raf = new RandomAccessFile(f.fajl, "rw");
- raf.writeUTF(tekst);
- raf.close();
- Server.korisnici.get(i).fajlovi.add(f);
- }
- }
- out.println("Da li zelite da vidite koje ste fajlove upload-ovali? Da/Ne");
- out.flush();
- String izbor2=in.readLine();
- if (izbor2.equals("Da")) {
- for (int i = 0; i < Server.korisnici.size(); i++) {
- if (Server.korisnici.get(i).username.equals(username)) {
- for (int j = 0; j < Server.korisnici.get(i).fajlovi.size(); j++) {
- out.println("Kod za fajl: " + Server.korisnici.get(i).fajlovi.get(j).id + " Naziv fajla: "
- + Server.korisnici.get(i).fajlovi.get(j).fajl.getName()+"\n");
- }
- }
- }
- }
- z = true;
- }
- }
- } else if (opcija.equals("2")) {
- out.println("Unesite kod za fajl koji zelite da download-ujete");
- out.flush();
- String kod=in.readLine();
- boolean y = false;
- for (int i = 0; i < Server.korisnici.size(); i++) {
- for (int j = 0; j < Server.korisnici.get(i).fajlovi.size(); j++) {
- if (Server.korisnici.get(i).fajlovi.get(j).id.equals(kod)) {
- RandomAccessFile citanje = new RandomAccessFile(Server.korisnici.get(i).fajlovi.get(j).fajl, "rw");
- RandomAccessFile download= new RandomAccessFile(Server.korisnici.get(i).fajlovi.get(j).fajl.getName(), "rw");
- while(true){
- procitaniBajtovi = citanje.read(niz);
- if(procitaniBajtovi == -1){
- break;
- }
- out.println("Sadrzaj fajla: ");
- out.flush();
- outbyte.write(niz,0,procitaniBajtovi);
- download.write(niz,0,procitaniBajtovi);
- out.println("\n");
- }
- citanje.close();
- download.close();
- y = true;
- break;
- }
- }
- }
- if (y == false) {
- out.println("Ne postoji fajl sa unetim kodom");
- }
- } else {
- out.println("Morate izabrati 1 ili 2!");
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //KORISNIK
- import java.io.File;
- import java.util.LinkedList;
- public class Korisnik {
- public String username;
- public String password;
- public LinkedList <Fajl> fajlovi = new LinkedList <Fajl>();
- public Korisnik(String ime, String lozinka){
- username = ime;
- password = lozinka;
- }
- }
- //FAJL
- import java.io.File;
- public class Fajl {
- public String id;
- public File fajl;
- public Fajl(String id){
- this.id=id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement