Lato server:
Classe Main:
Codice: Seleziona tutto
public class Main {
public static void main(String[] args) throws IOException {
int n_port=2000;
ServerSocket server=new ServerSocket(n_port); //Creiamo la serversocket sulla porta 2000;
System.out.println("Server "+server.getInetAddress().getHostAddress()+" in listening!");
//Ciclo infinito per permettere l'ascolto e la ricezione di più client;
while(true){
Socket client=server.accept(); //Immagine del client sul server;
Server thread=new Server(client);
Thread worker=new Thread(thread);
worker.start();
}
}
}
Codice: Seleziona tutto
public class Server extends Thread{
private Socket c;
private String ip_client;
private String mess_letto;
private int count;
private String matr_t;
public Server(Socket client) throws IOException{
this.matr_t=null;
this.count=0;
this.c=client;
this.ip_client=this.c.getInetAddress().getHostName();
System.out.println("Connessione in ingresso con il client "+this.ip_client);
}
@Override
public void run(){
//Dichiariamo i buffer;
BufferedReader reader=null;
Writer writer=null;
//Instanziamo i buffer;
reader=new BufferedReader(new InputStreamReader(this.c.getInputStream()));
writer=new OutputStreamWriter(this.c.getOutputStream());
String mess="\nInserisci il path del file da caricare: ";
writer.write(mess);
writer.flush();
try{
//Intercetto il file;
ObjectInputStream inps=new ObjectInputStream(this.c.getInputStream());
File infile=(File) inps.readObject();
File savefile=new File("/home/utente/Scrivania/cartella/"+this.matr_t+"/"+infile.getName());
this.save(infile,savefile);
}catch(ArrayIndexOutOfBoundsException e){
continue;
}
catch(Exception ex){
//Ignoro;
}
}
//Metodo per permettere di salvare i file ricevuti;
public void save(File in,File out) throws FileNotFoundException, IOException{
System.out.println("Intercettiamo il file"+in.getName());
System.out.println("Dimensione del file"+in.length());
FileInputStream fis=new FileInputStream(in);
FileOutputStream fos=new FileOutputStream(out);
byte []buff=new byte[1024];
int i=0;
while((i=fis.read(buff))!=-1){
fos.write(buff,0,i);
}
fis.close();
fos.close();
System.out.println("Ricezione completata");
}
}
Lato client:
Codice: Seleziona tutto
public class Main {
public static void main(String [] args) throws IOException, InterruptedException{
int k=0;
Socket s=null;
PrintWriter out=null;
BufferedReader in=null;
Scanner buff_in=new Scanner(System.in);
System.out.println("Stiamo provando a collegarci al server [0.0.0.0]");
try{
s=new Socket("192.168.0.110",2000);
out=new PrintWriter(s.getOutputStream(),true); //Instanziamo il buffer di output;
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(UnknownHostException e){
//Ignoro;
}
catch(IOException e){
//Ignoro;
}
String path=buff_in.next(); //Inseriamo il path;
Invia ogg=new Invia();
ogg.sendFile(new File(path),s); //Inviamo il file al server;
}
Codice: Seleziona tutto
public class Invia {
public void sendFile(File file,Socket s) throws IOException{
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
oos.reset();
oos.writeObject(file);
oos.flush();
oos.close();
}
}
