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

Entrée/Sortie Java Discussion :

synchronisation client serveur


Sujet :

Entrée/Sortie Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2005
    Messages : 85
    Points : 55
    Points
    55
    Par défaut synchronisation client serveur
    bonjour,

    J'ai implémenté un client-serveur,
    Le client envoie un fichier au serveur qui le lit, le stocke puis le serveur lance un exécutable qui est un solveur. Ce solveur à la fin du calcul envoie le fichier résultat sur le client. Mais le client lui n'attends pas que le serveur ait fini son calcul donc il bloque sur la lecture du résultat je crois...

    Qui a une idée pour que mon client puisse attendre la fin du calcul par le serveur avant de lire le résultat.

    Xavier

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Tu peux utiliser un calback

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2005
    Messages : 85
    Points : 55
    Points
    55
    Par défaut
    Désolé mais je suis nouveau en java,

    Peux m'expliquer comment faire pour placer un callback dans une architecture client server avce communication par socket?


    Merci,

    XAvier

  4. #4
    Membre habitué
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2004
    Messages
    138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juillet 2004
    Messages : 138
    Points : 125
    Points
    125
    Par défaut
    Salut,
    si je comprends bien tu veux que ton client puisse encore faire quelque chose pendant que le client traite son fichier et lui renvoie ?

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2005
    Messages : 85
    Points : 55
    Points
    55
    Par défaut
    En fait , je veux que mon client attende la fin du calcul par le serveur..
    Le serveur est en charge de lancer un calcul, a la fin de ce calcul, j'ai un fichier résultat que je dois transférer au client.

    Quand le fichier est écrit, le server l'écrit sur sa sortie (outputstream) et le client doit attendre que ce fichier soit écrit pour le lire sur son entrée InputStream).
    Actuellement, le client lit sur son entrée alors que le server est tojours en train de calculer ... ce qui créé un blocage du programme car le client attends de lire des infos.

    Xavier

  6. #6
    Membre habitué
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2004
    Messages
    138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juillet 2004
    Messages : 138
    Points : 125
    Points
    125
    Par défaut
    Normalement le client attend jusqu'à qu'il recoive quelque chose.
    Le serveur quand à lui lorsque la demande est faite doit traiter son calcul et le renvoyé.
    Alors je ne vois pas vraiment ou est-ce que ca te bloque...

    Essai de précisé la planté STP. Le client recoit quand meme le fichier ?

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2005
    Messages : 85
    Points : 55
    Points
    55
    Par défaut
    ben en fait quand je met en commentaire:
    *la partie serveur où je met le fichier à transférer sur le flux de sortie,
    *la partie client où je lis sur le flux d'entrée,

    mon programme est ok:

    1 le client envoie un fichier sur le serveur
    2 le serveur lance le calcul et écrit son fichier résultat


    quand je décommente le code cité précédemment:
    1 le client envoie un fichier sur le serveur,
    2 le serveur attends et ne lance pas le calcul....
    par contre si je kill le client à ce moment la, le serveur commence le calcul et écrit son fichier résultat

    C pour ca que j'ai l'impression que c'est le client qui bloque .
    Ce que je peux faire c t'envoyer mon code demain car je l'ai pas ici, tu pourras jeter un coup d'oeil,
    Xavier

  8. #8
    Membre habitué
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2004
    Messages
    138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juillet 2004
    Messages : 138
    Points : 125
    Points
    125
    Par défaut
    Oui pourquoi pas... Je prendrais quelque minutes pour essayer de résoudre ton problème...

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2005
    Messages : 85
    Points : 55
    Points
    55
    Par défaut
    le code 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
    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
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
     
    import java.io.*;
    import java.net.*;
     
    public class SamcefServer {
    // Choose a port outside of the range 1-1024:
    	private static final int PORT = 8080;
    	private static SamcefConnection connection = null;
     
    	public SamcefServer () {}
     
    	public int getPORT() {
    		return PORT;
    	}
     
    	public static void runSamcef() {
    		System.out.println("Samcef starts running");
    		String [] Cmd={"/home/Xavier/pro" };
    		try {
    			Runtime r = Runtime.getRuntime();
    			r.traceInstructions(true);
    			r.traceInstructions(true);
    			r.traceMethodCalls(true);
    			final Process proc = r.exec(Cmd);
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream errStream =
    						new BufferedInputStream(proc.getErrorStream());
    						byte[] buffer = new byte[128];
    						int read;
     
    						while ((read = errStream.read(buffer, 0, buffer.length)) != -1) {
    							System.err.write(buffer, 0, read);
    						}
    						errStream.close();
    					}
    					catch (IOException ioe) {
    						ioe.printStackTrace();
    					}
    				}
    			}.start();
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream outStream =
    						new BufferedInputStream(proc.getInputStream());
    						byte[] buffer = new byte[128];
    						int read;
     
    						while ((read = outStream.read(buffer, 0, buffer.length)) != -1) {
    							System.out.write(buffer, 0, read);
    						}
    						outStream.close();
    					}
    					catch (IOException ioe) {
    						ioe.printStackTrace();
    					}
    				}
    			}.start();
     
     
    			Thread waitFor = new Thread() {
    				public void run() {
    					try {
    						proc.waitFor();
    					}
    					catch(InterruptedException ie) {
    						ie.printStackTrace();
    					}
    				}
    			};
    			waitFor.start();
     
    			int status = -1;
    			try {
    				waitFor.join();
    				status = proc.exitValue();
    			}
    			catch (InterruptedException ie) {
    				ie.printStackTrace();
    			}
     
    			System.out.println(status);
    		}
    		catch (IOException e) {
    			System.out.println("erreur d'execution " + Cmd + e.toString());
    		}
    		catch (IllegalThreadStateException e) {
    			System.out.println("interruption 2" + Cmd + e.toString());
    			e.printStackTrace();
    		}	
    	}
     
    	public static void runPostPro() {
    		String [] Cmd2={"/home/Xavier/postpro" };
    		try {
    			Runtime r2 = Runtime.getRuntime();
    			r2.traceInstructions(true);
    			r2.traceInstructions(true);
    			r2.traceMethodCalls(true);
    			final Process proc2 = r2.exec(Cmd2);
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream errStream2 =
    						new BufferedInputStream(proc2.getErrorStream());
    						byte[] buffer2 = new byte[128];
    						int read2;
     
    						while ((read2 = errStream2.read(buffer2, 0, buffer2.length)) != -1) {
    							System.err.write(buffer2, 0, read2);
    						}
    						errStream2.close();
    					}
    					catch (IOException ioe2) {
    						ioe2.printStackTrace();
    					}
    				}
    			}.start();
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream outStream2 =
    						new BufferedInputStream(proc2.getInputStream());
    						byte[] buffer2 = new byte[128];
    						int read2;
     
    						while ((read2 = outStream2.read(buffer2, 0, buffer2.length)) != -1) {
    							System.out.write(buffer2, 0, read2);
    						}
    						outStream2.close();
    					}
    					catch (IOException ioe2) {
    						ioe2.printStackTrace();
    					}
    				}
    			}.start();
     
     
    			Thread waitFor2 = new Thread() {
    				public void run() {
    					try {
    						proc2.waitFor();
    					}
    					catch(InterruptedException ie2) {
    						ie2.printStackTrace();
    					}
    				}
    			};
    			waitFor2.start();
     
    			int status2 = -1;
    			try {
    				waitFor2.join();
    				status2 = proc2.exitValue();
    			}
    			catch (InterruptedException ie2) {
    				ie2.printStackTrace();
    			}
     
    			System.out.println(status2);
    		}
    		catch (IOException e2) {
    			System.out.println("erreur d'execution " + Cmd2 + e2.toString());
    			e2.printStackTrace();
    		}
    		catch (IllegalThreadStateException e2) {
    			System.out.println("interruption 2" + Cmd2 + e2.toString());
    			e2.printStackTrace();
    		}					
    		System.out.println("Computation is finish");
     
    	}
     
    	public static void main(String[] args) {
    		System.out.println("wait for client");
    		ServerSocket server;
    		try { 
    			server = new ServerSocket(PORT);
    			while (true) {
    				Socket s = server.accept();
    				connection = new SamcefConnection(s);
    				while (connection.connection_closed != true) {
    					;
    				}
    				System.out.println("Samcef start launching computation");
    				runSamcef();
    				runPostPro();
     
    				//new FatigueAnalysisConnection(s);
    			}
    		}
    		catch (IOException e) {
    			System.out.println("Erreur à la création d'un objet Socket : "+ e.getMessage());
    			System.exit(1); 
    			e.printStackTrace();
    		}
     
     
    	}
    }

    Cette classe est juste charger de lancer le calcul par un runSamcef().

    code Connection:

    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
    105
    import java.net.*;
    import java.io.*;
     
     
    public class SamcefConnection implements Runnable {
    	Socket Client;
    	DataInputStream fromClient;
    	DataOutputStream toClient;
    	boolean connection_closed = true;
     
    	public SamcefConnection(Socket client) {
    		this.Client = client;
    		try {
        			fromClient = new DataInputStream(Client.getInputStream());
          			toClient = new DataOutputStream(Client.getOutputStream());
    			connection_closed = false; 
    		}
    		catch (IOException e) {
    			try {
    				Client.close();
    				connection_closed = true;
    			}
    			catch (IOException ee) {}
    		}
    		new Thread(this).start();
    	}
     
    	public void sendFile(File file){
    		String pdu;
    		try{
    			FileInputStream fis = new FileInputStream(file);
    			//DataOutputStream toServer = new DataOutputStream(skt.getOutputStream());
     
    			pdu = Long.toString(file.length()) + "\n";
    			toClient.write(pdu.getBytes());
     
    			pdu = file.getName() + "\n";
    			toClient.write(pdu.getBytes());
     
    			while(fis.available() > 0){
    				toClient.write(fis.read());
    			}
    			fis.close();
    		}catch(Exception e){
    			System.err.println("WOOPS!" + e);
    			e.printStackTrace();
    		}
    	}
     
     
    	public void receiveFile(/*Socket skt*/){
    		try{
    			while(Client.isConnected() && !Client.isClosed()){
    				long fileSize = Long.parseLong(fromClient.readLine());
    				String fileName = fromClient.readLine();
    				FileOutputStream file = new FileOutputStream(new File(fileName));
     
    				for(int i = 0; i < fileSize; i++){
    					file.write(fromClient.read());
    				}
    			}
    		}catch(Exception e){
    			System.err.println("WOOPS!" + e);
    			e.printStackTrace();
    		}
    	}
     
    	public void run () {
    		try {
    // 			//load_File();
     			/*String read; 
    			String write;
    			FileWriter importFile = new FileWriter("test.dat");
     			while((write = fromClient.readLine()) != null) {
     				importFile.write(write);
    				importFile.write("\n");
    				//fromClient.reset();
     			}*/
    			receiveFile();
    			File file = new File("/home/Xavier/disp.txt");
    			sendFile(file);
     
    			//stop();
     
    		}
    		catch (Exception e) {
    			System.out.println("Exception: " + e.getMessage());
    			e.printStackTrace();
    		}
    	}
     
    	public void stop () {
    		try {
    	//		toClient.write("Closing connection to SamcefServer");
    			Client.close();
    			connection_closed = true; 
    		}
    		catch (Exception e) {
    			System.out.println("Exception at the closing of the connection");
    			e.printStackTrace();
    		}
    	}
     
     
    }

    Ici, le run() pêrmet de lire le fichier envoyé par le client par un receiveFile();

    code 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
    105
    106
    107
    108
    109
    110
    111
    112
    import java.net.*;
    import java.io.*;
     
     
     
    public class Samcef implements Runnable {
    	int portServer;
    	String adressServer;
    	Thread processus = null;
    	Socket socket;
    	DataInputStream fromServer;
    	DataOutputStream toServer;
    	String pro;
    	String postpro;
     
     
    	public Samcef(String adress, int port) {
    		portServer = port;
    		adressServer = adress;
    		processus = new Thread(this);
    		processus.start();
    	}
     
    	public void connect() {
    		try {
    			socket = new Socket(adressServer, portServer);
          			fromServer = new DataInputStream(socket.getInputStream());
          			toServer = new DataOutputStream(socket.getOutputStream());
    		}
    		catch (Exception e) {
    			System.out.println("unable to connect to the server");
    			e.printStackTrace();
    		}
    	}
     
    	public void disconnect() {
    		try {
    			socket.close();
    		}
    		catch (Exception e) {
    			System.out.println("failed disconnection");
    			e.printStackTrace();
    		}
    	}
     
    	public void sendFile(File file/*, Socket skt*/){
    		String pdu;
    		try{
    			FileInputStream fis = new FileInputStream(file);
    			//DataOutputStream toServer = new DataOutputStream(skt.getOutputStream());
     
    			pdu = Long.toString(file.length()) + "\n";
    			toServer.write(pdu.getBytes());
     
    			pdu = file.getName() + "\n";
    			toServer.write(pdu.getBytes());
     
    			while(fis.available() > 0){
    				toServer.write(fis.read());
    			}
    			fis.close();
    		}catch(Exception e){
    			System.err.println("WOOPS!" + e);
    			e.printStackTrace();
    		}
    	}
     
    	public void receivedFile(){
    		try{
    			while(socket.isConnected() && !socket.isClosed()){
    				long fileSize = Long.parseLong(fromServer.readLine());
    				String fileName = fromServer.readLine();
    				FileOutputStream file = new FileOutputStream(new File(fileName));
     
    				for(int i = 0; i < fileSize; i++){
    					file.write(fromServer.read());
    				}
    			}
    		}catch(Exception e){
    			System.err.println("WOOPS!" + e);
    			e.printStackTrace();
    		}
    	}
     
     
    	public void run() {
    		try {
    			connect();
    			File file = new File("/disk3/home/mencaglia/develop/fatigue/test/testserveur/test2.dat");
    			sendFile(file);
    			File mainFile = new File("/disk3/home/mencaglia/develop/fatigue/test/testserveur/test_main2.dat");
    			sendFile(mainFile);
    			//model transfer to the server
    			receivedFile();
     
    		}
    		catch (Exception e) {
    			System.out.println("unable to set connection");
    			e.printStackTrace();
    		}
    		finally {
    			processus = null;
    		}
    	}
     
    	public static void main(String args[]) {
    		System.out.println("... wait for connection...");
    		String str = "t100";
    		int p = 8080;
    		Samcef samcef = new Samcef(str, p);
    	}
    }
    Ici, tous ce passe dans le run(), envoie un fichier au serveur et aprés attente de réception d'un fichier par le serveur.

    et tu verras que si tu commente le receiveFile() dans le run() de la classe client et le sendFile() dans le run de la classe SamcefConnection, le programme se passe...
    Xavier

  10. #10
    Membre habitué
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2004
    Messages
    138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Service public

    Informations forums :
    Inscription : Juillet 2004
    Messages : 138
    Points : 125
    Points
    125
    Par défaut
    Pourquoi tu n'enverrais pas un objet serializable ?

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2005
    Messages : 85
    Points : 55
    Points
    55
    Par défaut
    ben pour ce que j'ai à faire je trouve que c'est pas la peine, j'y avais pensé..


    Parc contre j'ai modifié le code et de la manière suivante
    code 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
    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
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    import java.io.*;
    import java.net.*;
     
    public class SamcefServer {
    // Choose a port outside of the range 1-1024:
    	private static final int PORT = 8080;
    	private static SamcefConnection connection = null;
     
    	public SamcefServer () {}
     
    	public int getPORT() {
    		return PORT;
    	}
     
    	public static void runSamcef() {
    		System.out.println("Samcef starts running");
    		String [] Cmd={"/home/Xavier/pro" };
    		try {
    			Runtime r = Runtime.getRuntime();
    			r.traceInstructions(true);
    			r.traceInstructions(true);
    			r.traceMethodCalls(true);
    			final Process proc = r.exec(Cmd);
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream errStream =
    						new BufferedInputStream(proc.getErrorStream());
    						byte[] buffer = new byte[128];
    						int read;
     
    						while ((read = errStream.read(buffer, 0, buffer.length)) != -1) {
    							System.err.write(buffer, 0, read);
    						}
    						errStream.close();
    					}
    					catch (IOException ioe) {
    						ioe.printStackTrace();
    					}
    				}
    			}.start();
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream outStream =
    						new BufferedInputStream(proc.getInputStream());
    						byte[] buffer = new byte[128];
    						int read;
     
    						while ((read = outStream.read(buffer, 0, buffer.length)) != -1) {
    							System.out.write(buffer, 0, read);
    						}
    						outStream.close();
    					}
    					catch (IOException ioe) {
    						ioe.printStackTrace();
    					}
    				}
    			}.start();
     
     
    			Thread waitFor = new Thread() {
    				public void run() {
    					try {
    						proc.waitFor();
    					}
    					catch(InterruptedException ie) {
    						ie.printStackTrace();
    					}
    				}
    			};
    			waitFor.start();
     
    			int status = -1;
    			try {
    				waitFor.join();
    				status = proc.exitValue();
    			}
    			catch (InterruptedException ie) {
    				ie.printStackTrace();
    			}
     
    			System.out.println(status);
    		}
    		catch (IOException e) {
    			System.out.println("erreur d'execution " + Cmd + e.toString());
    		}
    		catch (IllegalThreadStateException e) {
    			System.out.println("interruption 2" + Cmd + e.toString());
    			e.printStackTrace();
    		}	
    	}
     
    	public static void runPostPro() {
    		String [] Cmd2={"/home/Xavier/postpro" };
    		try {
    			Runtime r2 = Runtime.getRuntime();
    			r2.traceInstructions(true);
    			r2.traceInstructions(true);
    			r2.traceMethodCalls(true);
    			final Process proc2 = r2.exec(Cmd2);
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream errStream2 =
    						new BufferedInputStream(proc2.getErrorStream());
    						byte[] buffer2 = new byte[128];
    						int read2;
     
    						while ((read2 = errStream2.read(buffer2, 0, buffer2.length)) != -1) {
    							System.err.write(buffer2, 0, read2);
    						}
    						errStream2.close();
    					}
    					catch (IOException ioe2) {
    						ioe2.printStackTrace();
    					}
    				}
    			}.start();
     
    			new Thread() {
    				public void run() {
    					try {
    						BufferedInputStream outStream2 =
    						new BufferedInputStream(proc2.getInputStream());
    						byte[] buffer2 = new byte[128];
    						int read2;
     
    						while ((read2 = outStream2.read(buffer2, 0, buffer2.length)) != -1) {
    							System.out.write(buffer2, 0, read2);
    						}
    						outStream2.close();
    					}
    					catch (IOException ioe2) {
    						ioe2.printStackTrace();
    					}
    				}
    			}.start();
     
     
    			Thread waitFor2 = new Thread() {
    				public void run() {
    					try {
    						proc2.waitFor();
    					}
    					catch(InterruptedException ie2) {
    						ie2.printStackTrace();
    					}
    				}
    			};
    			waitFor2.start();
     
    			int status2 = -1;
    			try {
    				waitFor2.join();
    				status2 = proc2.exitValue();
    			}
    			catch (InterruptedException ie2) {
    				ie2.printStackTrace();
    			}
     
    			System.out.println(status2);
    		}
    		catch (IOException e2) {
    			System.out.println("erreur d'execution " + Cmd2 + e2.toString());
    			e2.printStackTrace();
    		}
    		catch (IllegalThreadStateException e2) {
    			System.out.println("interruption 2" + Cmd2 + e2.toString());
    			e2.printStackTrace();
    		}					
    		System.out.println("Computation is finish");
     
    	}
     
    	public static void main(String[] args) {
    		System.out.println("wait for client");
    		ServerSocket server;
    		try { 
    			server = new ServerSocket(PORT);
    			while (true) {
    				Socket s = server.accept();
    				connection = new SamcefConnection(s);
    //				while (connection.connection_closed != true) {
    //					;
    //				}
    				System.out.println("Samcef start launching computation");
    				runSamcef();
    				runPostPro();
    				File file = new File("/home/Xavier/disp.txt");
    				while(!(file.exists())) {
    					try {
    						Thread.sleep(10);
    					} catch (InterruptedException e) {
    						System.out.println("Client impatient ... couldn't wait");
    					}	
    				}
    				connection.sendFile(file);
    				//new FatigueAnalysisConnection(s);
    			}
    		}
    		catch (IOException e) {
    			System.out.println("Erreur à la création d'un objet Socket : "+ e.getMessage());
    			System.exit(1); 
    			e.printStackTrace();
    		}
     
     
    	}
    }


    code 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
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
     
    import java.net.*;
    import java.io.*;
     
     
     
    public class Samcef implements Runnable {
    	int portServer;
    	String adressServer;
    	Thread processus = null;
    	Socket socket;
    	DataInputStream fromServer;
    	DataOutputStream toServer;
    	String pro;
    	String postpro;
     
     
    	public Samcef(String adress, int port) {
    		portServer = port;
    		adressServer = adress;
    		processus = new Thread(this);
    		processus.start();
    	}
     
    	public void connect() {
    		try {
    			socket = new Socket(adressServer, portServer);
          			fromServer = new DataInputStream(socket.getInputStream());
          			toServer = new DataOutputStream(socket.getOutputStream());
    		}
    		catch (Exception e) {
    			System.out.println("unable to connect to the server");
    			e.printStackTrace();
    		}
    	}
     
    	public void disconnect() {
    		try {
    			socket.close();
    		}
    		catch (Exception e) {
    			System.out.println("failed disconnection");
    			e.printStackTrace();
    		}
    	}
     
    	public void sendFile(File file/*, Socket skt*/){
    		String pdu;
    		try{
    			FileInputStream fis = new FileInputStream(file);
    			//DataOutputStream toServer = new DataOutputStream(skt.getOutputStream());
     
    			pdu = Long.toString(file.length()) + "\n";
    			toServer.write(pdu.getBytes());
     
    			pdu = file.getName() + "\n";
    			toServer.write(pdu.getBytes());
     
    			while(fis.available() > 0){
    				toServer.write(fis.read());
    			}
    			fis.close();
    		}catch(Exception e){
    			System.err.println("WOOPS!" + e);
    			e.printStackTrace();
    		}
    	}
     
    	public void receivedFile(){
    		try{
    			while(socket.isConnected() && !socket.isClosed()){
    				long fileSize = Long.parseLong(fromServer.readLine());
    				String fileName = fromServer.readLine();
    				FileOutputStream file = new FileOutputStream(new File(fileName));
     
    				for(int i = 0; i < fileSize; i++){
    					file.write(fromServer.read());
    				}
    			}
    		}catch(Exception e){
    			System.err.println("WOOPS!" + e);
    			e.printStackTrace();
    		}
    	}
     
     
    	public void run() {
    		try {
    			connect();
    			File file = new File("/disk3/home/mencaglia/develop/fatigue/test/testserveur/test2.dat");
    			sendFile(file);
    			File mainFile = new File("/disk3/home/mencaglia/develop/fatigue/test/testserveur/test_main2.dat");
    			sendFile(mainFile);
    			//model transfer to the serverreceiveFile
    			int count = fromServer.available();
    			while(count > 0) {
    				try {
    					Thread.sleep(10);
    				} catch (InterruptedException e) {
    					System.out.println("Client impatient ... couldn't wait");
    				}
    				receivedFile();
    			}			
    		}
    		catch (Exception e) {
    			System.out.println("unable to set connection");
    			e.printStackTrace();
    		}
    		finally {
    			processus = null;
    		}
    	}
     
    	public static void main(String args[]) {
    		System.out.println("... wait for connection...");
    		String str = "t100";
    		int p = 8080;
    		Samcef samcef = new Samcef(str, p);
    	}
    }
    mais j'ai encore un problème:

    j'attends bien que le fichier disp.txt existe pour le transférer mais au moment où je le tranfère j'obtiens cette exception:

    java.net.SocketException: Broken pipe
    java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
    at java.io.DataOutputStream.write(DataOutputStream.java:71)
    at SamcefConnection.sendFile(SamcefConnection.java:45)
    at SamcefServer.main(SamcefServer.java:201)


    et je comprends pas pourquoi le connextion est fermée car j'appel jamais les méthodes close() où disconnect().

    Si tu peux m'aider, je sais que c pas évident pour qqn de rentrer dans un code mais si tu peux jeter un petit coup d'oeil.

    Xavier

Discussions similaires

  1. Synchronisation BDD Client/Serveur
    Par kevindeta dans le forum C#
    Réponses: 9
    Dernier message: 09/01/2012, 15h03
  2. client/serveur : synchronisation, tempo et ping de présence
    Par balibalo dans le forum Développement
    Réponses: 3
    Dernier message: 21/06/2004, 11h32
  3. Quel outil pour du développement Client/Serveur (Win XP) ?
    Par jey_bonnet dans le forum Débats sur le développement - Le Best Of
    Réponses: 5
    Dernier message: 02/11/2002, 14h57
  4. comment gerer plusieurs connexions client/serveur
    Par naili dans le forum C++Builder
    Réponses: 3
    Dernier message: 14/08/2002, 16h58
  5. Langage le mieux adapté pour application client serveur ?
    Par guenus dans le forum Débats sur le développement - Le Best Of
    Réponses: 4
    Dernier message: 17/06/2002, 15h46

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