Strumenti Utente

Strumenti Sito


lpr-b:clientupload
no way to compare when less than two revisions

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.


lpr-b:clientupload [12/10/2007 alle 13:26 (18 anni fa)] (versione attuale) – creata Marco Danelutto
Linea 1: Linea 1:
 +<code java>
 +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;
 + }
 +
 +
 + }
 +
 +}
 +</code>
lpr-b/clientupload.txt · Ultima modifica: 12/10/2007 alle 13:26 (18 anni fa) da Marco Danelutto

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki