1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
package net.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
class Client {
//
private static int TAILLE_MAXIMALE = 1024;
//
private static int portEcouteServeur = 12346;
//
private static String p_xml_to_sign = "fichier_a_transmettre";
public static void main(String[] arg) {
//
ObjectOutputStream sortie = null;
FileOutputStream fos = null;
InputStream entree = null;
InputStream in = null;
byte buf[] = new byte[TAILLE_MAXIMALE];
int n;
Socket socket = null;
DatagramSocket dSocket;
int success;
File file = new File(p_xml_to_sign);
// try {
// InetAddress adresseIP = InetAddress.getByName("127.0.0.1");
// dSocket = new DatagramSocket();
// FileInputStream fis = new FileInputStream(file);
// byte[] contenu = new byte[(int) file.length()];
// //
// fis.read(contenu);
// DatagramPacket packet = new DatagramPacket(contenu, contenu.length, adresseIP, portEcouteServeur);
// dSocket.send(packet);
// //
// byte[] buffer = new byte[TAILLE_MAXIMALE];
// DatagramPacket reponse = new DatagramPacket(buffer, buffer.length, adresseIP, portEcouteServeur);
//
// //on met un timeout
// //dSocket.setSoTimeout(2000);
// dSocket.receive(reponse);
// fos = new FileOutputStream(file.getParent() + "signed.xml");
// fos.write(reponse.getData(), reponse.getOffset(), reponse.getLength());
//
// dSocket.close();
// success = 0;
// } catch (IOException ioe) {
// success = -1;
// ioe.printStackTrace();
// }
//
try {
socket = new Socket("127.0.0.1",portEcouteServeur);
sortie = new ObjectOutputStream(socket.getOutputStream());
in = new FileInputStream(new File(p_xml_to_sign));
while((n=in.read(buf))!=-1) {
System.out.println("la taille : " + n);
sortie.write(buf,0,n);
}
sortie.flush();
//
// fos = new FileOutputStream(new File("C:\\JAVA\\facture_fa11000044_signed.xml"));
// entree = new ObjectInputStream(socket.getInputStream());
// while((n=in.read(buf))!=-1) {
// System.out.println("la taille : " + n);
// fos.write(buf,0,n);
// }
// fos.flush();
} catch(FileNotFoundException exc) {
System.out.println("Fichier introuvable");
} catch(UnknownHostException exc) {
System.out.println("destinataire inconnu");
} catch(IOException exc) {
System.out.println("probleme d'entree-sortie");
exc.printStackTrace();
} finally {
try {
in.close();
socket.close();
sortie.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} |
Partager