Problème sur mon serveur Java
bonjour à tous,
je crée actuellement une application avec un client/serveur. mon client envoie des infos (nom+id) au serveur qui les écris dans un fichier txt.
voici mon code pour le serveur:
Code:
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
|
package serveur;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Serveur{
private ServerSocket srv;
private final int port;
public Serveur(int port) throws IOException{
this.port=port;
srv = new ServerSocket(port);
}
public void close() throws IOException{
srv.close();
}
public boolean estConnecte(){
return true;
}
public void acceptSrv() throws IOException{
this.srv.close();
System.out.println("srv fermer");
this.srv = new ServerSocket(this.port);
}
public void service() throws IOException, ClassNotFoundException{
//connexion entre client/serveur
System.out.println("srv accept en attente");
Socket clt=this.srv.accept();
System.out.println("serveur ligne 34");
ObjectOutputStream output = new ObjectOutputStream(clt.getOutputStream());
ObjectInputStream input= new ObjectInputStream(clt.getInputStream());
//recupere le choix de l'action réalisé
String datas = input.readObject().toString();
String infos [] = datas.split ("_") ;
int mode =Integer.parseInt(infos[0]);
switch (mode)
{
case 1 :
String nom = infos[1];
String id= infos[2];
envoiTuple(nom,id,output);
break;
default :
System.out.println("erreur");
}
input.close();
clt.close();
output.close();
System.out.println("input,clt, output fermer");
}
public boolean envoiTuple(String nom, String id, ObjectOutputStream output) throws IOException{
//on va chercher le chemin et le nom du fichier et on me tout ca dans un String
String p=String.valueOf(this.port);
String adressedufichier = "C:\\Users\\jerome\\Downloads\\workspace\\serveur\\serveur"+p+".txt";
try{
BufferedWriter outputFichier = new BufferedWriter(new FileWriter(adressedufichier, true));
outputFichier.write(nom+"_"+id);
outputFichier.flush();
outputFichier.close();
envoiConfirmation(output,true," enregistrement réussi");
}
catch(IOException ioe){
envoiConfirmation(output,false," fail erreur");
System.out.print("Erreur : ");
ioe.printStackTrace();
}
return false;
}
public boolean testConnexion(ObjectInputStream input, ObjectOutputStream output) throws IOException{
envoiConfirmation(output,false,"Mode non implémenté.");
return false;
}
public void envoiConfirmation(ObjectOutputStream output, boolean conf, String msg) throws IOException{
System.out.println("envoi de conf: "+conf);
output.writeBoolean(conf);
System.out.println("envoi de msg: "+msg);
output.writeUTF(msg);
output.flush();
}
} |
et voici mon code pour mon client:
Code:
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
|
package client;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client{
private final Socket clt;
ObjectInputStream input;
ObjectOutputStream output;
Frame vue;
public Client(InetAddress address, int port, Frame vue) throws IOException{
//System.out.println("creation socket");
clt = new Socket(address,port);
//System.out.println("socket creer");
this.input = new ObjectInputStream(clt.getInputStream());
this.output = new ObjectOutputStream(clt.getOutputStream());
this.vue = vue;
}
public void close() throws IOException{
this.input.close();
this.output.close();
clt.close();
}
public boolean testConnexion() throws IOException{
this.output.writeObject("4_");
this.output.flush();
return receptionConfirmation();
}
public boolean envoiTuple(String nom, String id) throws IOException{
this.output.writeObject("1_"+nom+"_"+id);
this.output.flush();
return receptionConfirmation();
}
public boolean receptionConfirmation() throws IOException{
boolean conf = this.input.readBoolean();
String msg = this.input.readUTF();
if (conf){
vue.MaJLog("OK : "+msg);
}
else{
vue.MaJLog("KO : "+msg);
}
return conf;
}
} |
voici mon probleme:
lors de l'envoi du premier tuple d'information, tout fonctionne correctement. cependant, lors de l'envoie du second tuple d'information, j'ai une erreur:
java.net.SocketException: Software caused connection abort: recv failed
Sauriez-vous d'où cela peut venir?