Ajouter un commentaire

abousaadprogrammer
plontage de le barre de tache lors de l'arret du serveur

j'ai un probleme avec mon serveur multithread quand je l'arrete pour la 2 eme fois à partir de la barre du tache system
celle-ci est plonté .mais le serveur continuer à servir ses client
j'ai mis un fichier journal coté client et serveur qui entregiste toutes les connexions effectuées
quelq'un peut me donner une solution
voici mon programme serveur
/*************classe serveurA *************/
import java.net.*;
import java.util.Date;
import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class serveurA extends Thread {
final static int port = 9632;
static Socket socket;
Date dt=new Date();
static String fmessage ;
static Socket socketClient;
static FileWriter fichserver;
static ServerSocket socketServeur;
static InetAddress adrLocale;
static MenuItem arret,aide,quit,demar;
static serveurA t ;
static Runnable runner;
static boolean actif =true;
static TrayIcon trayIcon;
static ThreadGroup tgp = new ThreadGroup("groupe");
static ThreadGroup[] GroupesThreads ;
String jours[] = {"Dimanche","Lundi","Mardi","Mercredi","Jeudi","Samedi","Dimache"};
static int i=0;
static String nomterad="";
static Console console;
public static void main(String[] args) {

if (SystemTray.isSupported()) {

SystemTray tray = SystemTray.getSystemTray();

Image image = Toolkit.getDefaultToolkit().getImage("icon.jpg");

PopupMenu popup = new PopupMenu();
quit = new MenuItem("quitter");
arret = new MenuItem("Arreter");
demar = new MenuItem("Demarrer");


popup.add(arret);
popup.addSeparator();
popup.add(demar);
popup.addSeparator();
popup.add(quit);

//image, message dans une bulle d'aide, et menu pop-up

trayIcon = new TrayIcon(image, "Serveur de donnée", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("serveur de fichier Bienvenue","Le System Tray en action !",TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}

arret.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){
//System.exit(0);

arret();


}});
demar.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){
//System.exit(0);

demarrage();


}});
quit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){
System.exit(0);

}});

} else System.err.println("Fontion non supportée");

demarrage();

}


static void arret(){
System.out.println("arret du serveur");

try {
//socketClient.close();
//socket.close();
actif=false;
//if(socket!=null) socket.close();
if(socketClient!=null)socketClient.close();
// t.stop();
socketServeur.close();


//System.out.println("Nombre total de threads dans le groupe apres : " + Thread.currentThread().getThreadGroup().activeCount());

} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}

static void demarrage(){
actif=true;
try {
adrLocale=InetAddress.getLocalHost();
socketServeur = new ServerSocket(port);
System.out.println("Lancement du serveur");
while (true) {
socketClient = socketServeur.accept();
t = new serveurA(socketClient);
t.start();

}

} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void controle(String msg) {
if (msg.equals("demar")) demarrage();
else if (msg.equals("arret")) arret();
else if (msg.equals("exit")) System.exit(0);
}
public String getdate() {
int year=dt.getYear()+1900;
return jours[dt.getDay()]+" "+dt.getDate()+"/"+dt.getMonth()+"/"+year
+" à "+dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds();
}

public serveurA(Socket socketA) {
this.socket = socketA;
}
public void run() {

try {

fichserver = new FileWriter ("journalserveur.txt",true);
String message = "";
System.out.println("Connexion avec le client : " + socket.getInetAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());
message = getdate()+" "+in.readLine()+"utilsant le port : "+socket.getPort()+"\r\n";;
//System.out.println("serveur : "+message);
fmessage+=message;
out.println("bonjour c'est le serveur avec adresse : "+adrLocale.getHostAddress()+" et Nom : "+adrLocale.getHostName());
} catch (Exception e) {
System.out.println(e.getMessage());
}

try {
fichserver.write(fmessage);
fmessage="";
fichserver.close();
socketClient.close();
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}

}
/************fin classe*********/
/************classe client*********/
import java.net.*;
import java.util.Date;
import java.io.*;
public class clientA {
final static int port = 9632;
static Date dt=new Date();
public static void main(String[] args) {


Socket socket;
DataInputStream userInput;
PrintStream theOutputStream;
try {
FileWriter fichclient = new FileWriter("journalclientB.txt",true);
InetAddress serveur = InetAddress.getByName("127.0.0.1");
socket = new Socket("localhost", port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());
out.println("connexion du client "+clientA.class.getName()+" ");
//System.out.println("client : "+in.readLine());
fichclient.write(dt.toLocaleString()+" "+in.readLine());
fichclient.write("\r\n");
fichclient.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/***************classe clientA****************/

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 PPPP   U   U  ZZZZZ  EEEE  U   U 
P P U U Z E U U
PPPP U U Z EEE U U
P U U Z E U U
P UUU ZZZZZ EEEE UUU