CLASSE CLIENT
codice:
package udp_pacchetti;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JFileChooser;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ste
*/
public class UDP_Client_pacchetti {
DatagramSocket dataSocket;
DatagramPacket dataPacket;
String nome;
int dimensione;
String dimension;
private InetAddress indirizzoServer;
private int porta;
public void Start() throws IOException {
porta = 9876;
indirizzoServer = InetAddress.getByName("127.0.0.1");
// QUI SELEZIONO IL FILE
JFileChooser fileChooser = new JFileChooser();
int n = fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
String path_file = file.getAbsolutePath();
// E NE ACQUISISCO I DATI
nome = file.getName();
String nome_stringa = nome;
System.out.println("PROGRAMMA DI TRASFERIMENTO IN PACCHETTI");
System.out.println("il nome da inviare è: " +nome_stringa);
dimensione = (int) file.length();
dimension = String.valueOf(dimensione);
String dimensione_stringa = dimension;
//System.out.println("Il nome del fileselezionato è: " +nome);
System.out.println("La sua dimensione : " +dimensione_stringa+ " byte");
System.out.println("Percorso: " +path_file);
invia_Nome(nome_stringa);
invia_Dimensione(dimensione_stringa);
invia_File(file, nome, path_file, dimensione );
}
private void invia_File(File file, String nome, String path_file, int dimensione) throws IOException {
System.out.println("*** Inizio invio file");
dataSocket = new DatagramSocket();
try {
byte[] buf = new byte[1024];
int read,tot=0;
FileInputStream inputStream = new FileInputStream(file);
// INVIO I PACCHETTI
while ((read = inputStream.read(buf)) != -1 ){
tot += read;
System.out.println(" - "+tot+ " byte");
dataPacket = new DatagramPacket(buf, buf.length, indirizzoServer, 9876);
dataSocket.send(dataPacket);
}
System.out.println("*** FILE INVIATO CON SUCCESSO ***");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} // FINE INVIA_FILE
private void invia_Nome(String nome) throws IOException {
byte [] name = {0};
dataSocket = new DatagramSocket();
dataPacket = new DatagramPacket (name, name.length, indirizzoServer, porta);
String nome_file = nome;
System.out.println("Il nome del file è: " +nome_file);
name = nome_file.getBytes();
dataPacket.setData(name);
dataPacket.setLength(name.length);
// INVIO DEL PACCEHTTO SUL SOCKET
dataSocket.send(dataPacket);
}// FINE INVIA NOME
private void invia_Dimensione (String dimensione_stringa) throws IOException {
byte [] dimension = {0};
String dimensione_file = dimensione_stringa;
dataSocket = new DatagramSocket();
dataPacket = new DatagramPacket (dimensione_file.getBytes(), dimensione_file.length(), indirizzoServer, porta);
System.out.println("La dimensione del file è: " +dimensione_file);
// INVIO DEL PACCEHTTO SUL SOCKET
dataSocket.send(dataPacket);
dataSocket.close();
}// FINE INVIA DIMENSIONE
public static void main(String[] args) throws IOException
{
UDP_Client_pacchetti udpClientPac = new UDP_Client_pacchetti();
udpClientPac.Start();
}
}