IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Serveur requête et réponse


Sujet :

avec Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2009
    Messages
    155
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 155
    Par défaut Serveur requête et réponse
    Bonjour,

    Je signe un fichier XML en XAdES à l'aide d'une clé cryptographique.
    Le code pour faire cela fonctionne bien lorsque la clé est branchée sur l'ordinateur qui l’exécute.
    Le problème c'est que je voudrais signer ces XML à distance c'est à dire brancher ma clé sur une machine accessible via internet.
    La machine 1 qui a le document à signer n'a pas de clé et la machine 2 possède la clé.
    Je pense faire envoyer par une requête le fichier XML à signer et attendre en retour le fichier XML signé.
    Est ce faisable ?
    Avez vous une idée sur la manière de procéder ?
    Par ou dois je commencer (pistes, codes) ?

    Par avance merci
    cdt

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2009
    Messages
    155
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 155
    Par défaut
    Bonjour,

    J'ai un peu avancé au sujet de mon traitement.
    En faisant de petites recherches je suis partie sur une architecture client serveur avec des socket.
    J'ai trouvé un petit code qui permet de faire l'envoi d'un fichier vers le serveur.
    Client
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.client;
     
    import java.net.*;
    import java.io.*;
     
    public class Client_Sign {
        static Socket s;
        public static void main(String...args) { 
            Socket sock = null;
    		try {
    			sock = new Socket(InetAddress.getLocalHost(),9001);
     
    			Transfert.transfert(
                    new FileInputStream("D:\\test.jpg"),
                    sock.getOutputStream(),
                    true);
     
    		} catch (UnknownHostException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				sock.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
        }
    }
    Serveur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.serveur;
     
    import java.net.*;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    public class Serveur_Sign {
     
        public static void main(String...args) { 
            Socket sock = null;
    		try {
    			sock = new ServerSocket(9001).accept();
     
    			Transfert.transfert(
                    sock.getInputStream(),
                    new FileOutputStream("D:\\test2.jpg"),
                    true);
     
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				sock.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
        } 
    }
    Transfert
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.client;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    public class Transfert {
    	public static void transfert (InputStream in, OutputStream out, boolean closeOnExit) {
    		try {
    	        	byte buf[] = new byte[1024];
    	        	int n;
    	        	while((n=in.read(buf))!=-1) 
    	        	out.write(buf,0,n);
     
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} finally {
    				try { 
    					if (closeOnExit) {
    						in.close();
    						in = null;
    					}
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    	            try {
    	            	if (closeOnExit) {
    	            		out.close();
    	            		out = null;
    	            	}
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    	  }
    }
    Là j'arrive à déposer mon fichier par contre je ne voit pas trop comment faire le retour au client.
    Faut-il faire traiter le client comme un serveur ?

    Merci

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2009
    Messages
    155
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 155
    Par défaut
    Bonjour,

    Je me parle à moi même puisque personne ne répond mais j'ai un peu avancer.
    J'ai modifié mes classes serveur et client.
    Le client envoie le fichier à l'aide d'une socket et le serveur va traiter la réceptions des sockets dans un thread.
    Voilà ce que ça donne :

    Client :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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();
    			}
    		}
    	}
    }
    Le serveur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.serveur;
     
    import java.io.IOException;
    import java.net.DatagramSocket;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    class Serveur {
    	public static void main(String[] arg) {
    		int portEcoute = 12346;
    		ServerSocket standardiste;
    		Socket socket;
     
    		try {
    			standardiste = new ServerSocket(portEcoute);
    			while(true) {
    				System.out.println("attente d'un client...");
    				socket = standardiste.accept();
    				System.out.println("Client connecté : " + socket.toString());
    	 			new Service(socket);
    			}
    		}
    		catch(IOException exc) {
    	 		System.out.println("probleme de connexion");
    	 		exc.printStackTrace();
    		}
    	}
    }
    et le Thread service :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.serveur;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.OutputStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.Socket;
     
    class Service extends Thread {
    	DatagramSocket dSocket;
    	Socket socket;
    	InputStream entree = null;
    	OutputStream sortie = null;
    	FileOutputStream fos = null;
    	FileInputStream in = null;
    	String p_xml_to_sign = null;
    	private static int portEcoute = 12346;
     
    	//
    	private static int TAILLE_MAXIMALE = 1024;
     
    	Service(Socket socket) {
    		this.socket = socket;
    		p_xml_to_sign = "fichier_transmis";
    		try {
    			entree = new ObjectInputStream(socket.getInputStream());
    //			sortie = socket.getOutputStream();
    			this.start();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void run() {
    		byte buf[] = new byte[TAILLE_MAXIMALE];
    		int n;
    		try {
    			fos = new FileOutputStream(new File(p_xml_to_sign));
            	while((n = entree.read(buf)) != -1) {
            		System.out.println("La taille : " + n);
            		fos.write(buf,0,n);
            	}
            	fos.flush();
            	fos.close();
    		} catch(IOException e) {
    			e.printStackTrace();
    			System.err.println("erreur entree sortie...");
    		} finally {
    			try {
    				//sortie.close();
    				entree.close();
    				fos.close();
    				socket.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    Le transfert se fait bien au complet maintenant je voudrais savoir comment répondre au client en lui envoyant le fichier traité.
    Est ce que je dois refaire une socket et la traiter coté client comme un serveur ?
    Si quelqu'un à une idée...

    Merci

  4. #4
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 713
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 713
    Par défaut
    oui
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2009
    Messages
    155
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 155
    Par défaut
    Bonjour,

    merci pour ta réponse.
    J'ai donc fait la même chose coté client pour envoyer le fichier une fois traité.
    Cela fonctionne.
    Le code peut être épuré bien sur.

    Voilà le code final :
    Client
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    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.ServerSocket;
    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_envoyer";
     
     
    	public static void main(String[] arg) {
     
    		//
    		ObjectOutputStream sortie = null;
    		FileOutputStream fos = null;
    		InputStream entree = null;
    		FileInputStream in = null;
    		byte buf[] = new byte[TAILLE_MAXIMALE];
    		int n;
    		Socket socket = null;
    		ServerSocket servSock = null;
    		Socket sockrec = null;
     
     
    		//
    		try {
    			socket = new Socket("127.0.0.1", portEcouteServeur);
     
    			//Envoie du fichier vers serveur
    			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();
               	sortie.close();
               	System.out.println("Envoi OK depuis le client...");
     
            	//Reception du fichier du serveur
               	servSock = new ServerSocket(portEcouteServeur + 1);
               	sockrec = servSock.accept();
            	entree = new ObjectInputStream(sockrec.getInputStream());
            	fos = new FileOutputStream(new File("fichier_traite_recu"));
            	while((n=entree.read(buf))!=-1) {
            		System.out.println("la taille : " + n);
            		fos.write(buf,0,n);
            	}
            	fos.flush();
            	fos.close();
            	System.out.println("Reception OK depuis le client...");
    		} 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();
    	        	servSock.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    Serveur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.serveur;
     
    import java.io.IOException;
    import java.net.DatagramSocket;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    class Serveur {
    	public static void main(String[] arg) {
    		int portEcoute = 12346;
    		ServerSocket standardiste;
    		Socket socket;
     
    		try {
    			standardiste = new ServerSocket(portEcoute);
    			while(true) {
    				System.out.println("attente d'un client...");
    				socket = standardiste.accept();
    	 			new Service(socket);
    			}
    		}
    		catch(IOException exc) {
    	 		System.out.println("probleme de connexion");
    	 		exc.printStackTrace();
    		}
    	}
    }
    et le service Thread
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package net.serveur;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.Socket;
     
    class Service extends Thread {
    	Socket socket;
    	InputStream entree = null;
    	OutputStream sortie = null;
    	FileOutputStream fos = null;
    	FileInputStream in = null;
    	String p_xml_to_sign = null;
    	private static int portEcoute = 12346;
    	Socket sockreponse = null;
     
    	//
    	private static int TAILLE_MAXIMALE = 1024;
     
    	Service(Socket socket) {
    		this.socket = socket;
    		p_xml_to_sign = "fichier_a_traiter";
    		System.out.println("Client connecté : " + socket.toString());
    		try {
    			entree = new ObjectInputStream(socket.getInputStream());
    			sockreponse = new Socket(socket.getInetAddress(), portEcoute + 1);
    			sortie = new ObjectOutputStream(sockreponse.getOutputStream());
    			this.start();
    		}
    		catch(IOException exc) {
    			exc.printStackTrace();
    		}
    	}
     
    	public void run() {
    		byte buf[] = new byte[TAILLE_MAXIMALE];
    		int n;
    		try {
    			// Reception du fichier
    			fos = new FileOutputStream(new File(p_xml_to_sign));
            	while((n = entree.read(buf)) != -1) {
            		System.out.println("La taille : " + n);
            		fos.write(buf,0,n);
            	}
            	fos.flush();
            	fos.close();
            	buf = null;
            	System.out.println("Reception OK depuis le serveur...");
            	// Envoi du fichier
            	buf = new byte[TAILLE_MAXIMALE];
            	in = new FileInputStream(new File("fichier_traiter_a_envoyer_au_client"));
            	while((n = in.read(buf)) != -1) {
            		System.out.println("La taille : " + n);
            		sortie.write(buf,0,n);
            	}
            	sortie.flush();
            	sortie.close();
            	System.out.println("Envoi OK depuis le serveur...");
    		} catch(IOException e) {
    			e.printStackTrace();
    			System.err.println("erreur entree sortie...");
    		} finally {
    			try {
    				entree.close();
    				socket.close();
    				sockreponse.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    Voilà en espérant que cela aide quelqu'un....
    Merci

  6. #6
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 713
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 713
    Par défaut
    Citation Envoyé par titi42 Voir le message

    Voilà en espérant que cela aide quelqu'un....
    Merci
    Probablement, c'est un classique ...
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. requête sans réponses
    Par zarzar4 dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 05/10/2008, 22h51
  2. Réponses: 3
    Dernier message: 01/04/2007, 16h30
  3. Serveur HTTP, second réponse non renvoyée
    Par kobe1980 dans le forum Entrée/Sortie
    Réponses: 1
    Dernier message: 25/02/2007, 18h15
  4. Réponses: 21
    Dernier message: 06/02/2007, 14h05
  5. requête sans réponse
    Par Pau dans le forum Access
    Réponses: 5
    Dernier message: 02/06/2006, 15h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo