Scusate ho preparato questi due piccoli programmini per trasferire tramite rete un file, uno è un server:

codice:
/**
 * @(#)Server.java
 *
 *
 * @author 
 * @version 1.00 2007/6/7
 */
import java.io.*;
import java.net.*;

public class Server {
        
    /**
     * Creates a new instance of <code>Server</code>.
     */
    public Server() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       File f = new File("sfondo.gif");
       int dimension = (int)f.length();
       byte []data = new byte[dimension];
       try
          {
           while(true)
           {
            FileInputStream fis = new FileInputStream(f);
            ServerSocket s1 = new ServerSocket(10000);
            Socket s = s1.accept();
            DataOutputStream opt = (DataOutputStream)s.getOutputStream();
       	    fis.read(data, 0, dimension-1);
       	    opt.write(data, 0, dimension-1);
            opt.close();
            s1.close();
            fis.close();
           }  
          }
          catch(Exception e ){}
          
    }
}
e un client per riceverlo:

codice:
/**
 * @(#)Client.java
 *
 *
 * @author 
 * @version 1.00 2007/6/7
 */
import java.io.*;
import java.net.*;

public class Client {
        
    /**
     * Creates a new instance of <code>Client</code>.
     */
    public Client() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try{
      FileOutputStream fos = new FileOutputStream("FileRicevuto");
      Socket s = new Socket("127.0.0.1",10000);  
      DataInputStream dis = (DataInputStream)s.getInputStream();
      int b;
      while((b = dis.readInt())!=-1)
      {
      	System.out.println(b);
      	fos.write(b);
      }
      dis.close();
      fos.close();
      s.close();
      }
      catch(Exception e){
      }
    }
}

ma non funziona, il client sembra copiare byte a byte (al momento la scelta è voluta) peò nello streaming in uscita ovvero nel file non scrive nulla.

Qualcuno ha idea di cosa accada?