package trasferimentoFile; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static final int MAXBUFFER = 512; /** * @param args nomeHostPozzo */ public static void main(String[] args) { String hostname = args[0]; String filename = args[1]; InetAddress hostIp = null; try { hostIp = InetAddress.getByName(hostname); } catch (UnknownHostException e) { System.out.println("Hostname "+hostname+" sconosciuto"); return; } Socket s = null; try { s = new Socket(hostIp,Server.PORTA); } catch (IOException e) { System.out.println("Connessione a "+hostname+" porta "+Server.PORTA+" non riuscita"); e.getCause(); return; } FileInputStream fis = null; try { fis = new FileInputStream(filename); } catch (FileNotFoundException e) { System.out.println("Errore nell'apertura del file "+filename); return; } OutputStream os = null; try { os = s.getOutputStream(); } catch (IOException e) { System.out.println("Errore: getOutputStream"); return; } try { os.write(new String(filename+"\n").getBytes()); } catch (IOException e) { System.out.println("Errore nella scrittura del nome file su socket"); return; } byte [] buffer = new byte[MAXBUFFER]; int letti = 0; try { letti = fis.read(buffer, 0, MAXBUFFER); } catch (IOException e) { System.out.println("Errore nella lettura dal file "+filename); return; } while(letti > 0) { try { os.write(buffer,0,letti); letti = fis.read(buffer, 0, MAXBUFFER); } catch (IOException e) { System.out.println("Errore nella scrittura su socket"); return; } } try { os.close(); fis.close(); s.close(); } catch (IOException e) { System.out.println("Errore nella chiusura del socket"); return; } } }