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
| //import javax.swing.*;
import java.net.*;
//import java.awt.*;
import java.io.*;
import java.text.DateFormat;
import java.util.*;
public class Server implements Runnable {
final static int PORT = 4321;
Socket service;
public Server () {
init();
}
private void init() {
try {
/*Dimension dimScrean = Toolkit.getDefaultToolkit().getScreenSize();
JFrame server = new JFrame("server");
server.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Rectangle bounds = new Rectangle();
bounds.x = dimScrean.width / 2 - server.getWidth() / 2;
bounds.y = dimScrean.height = server.getHeight();
server.setBounds(bounds);
server.setVisible(true);*/
// préparation du serveur
// réseau
ServerSocket ecoute = new ServerSocket(PORT);
while (true) {
service = ecoute.accept();
Thread t = new Thread(this);
t.start();
t.setName("Write connect Messages");;
}
}
catch (BindException be) {
System.err.println("Le serveur a quitter car un autre et déjà en utilisation");
System.exit(-1);
}
catch (IOException ioe) {
System.err.println(("Problème au niveau du serveur : "+ ioe.getMessage()) );
ioe.printStackTrace();
}
}
public void run () {
try {
// acceper la requête
BufferedReader rd;
String message;
rd = new BufferedReader(new InputStreamReader(service.
getInputStream()));
System.out.println("Nouvelle connection à : "+formateDate());
while (true) {
if ( (message = rd.readLine()) != null) {
Thread tWriteFile = new Thread(new writeInfile(message));
tWriteFile.start();
tWriteFile.setName("Write in file");
}
}
}
catch (IOException ioe) {
System.out.println("Le client a quitté : "+formateDate());
if (! ioe.getMessage().contains("reset"))
ioe.printStackTrace();
}
}
class writeInfile implements Runnable {
String messgage;
private File fileErrorOutput = new File (formateDate());
private FileOutputStream fileError = null;
private PrintStream fosErrorSender = null;
public writeInfile (String message) {
this.messgage = message;
}
public void run() {
if (messgage != null) {
try {
// TODO Auto-generated method stub
fileError = new FileOutputStream(fileErrorOutput);
fosErrorSender = new PrintStream(new BufferedOutputStream(
fileError));
System.out.println("Nouveau message d'erreur +" +
fileErrorOutput.getName());
fosErrorSender.println(messgage);
System.out.println(messgage);
fosErrorSender.flush();
fosErrorSender.close();
fileError.close();
if (fileErrorOutput.exists() && fileErrorOutput.length() == 0) { // aucune erreur n'est survenue
fileErrorOutput.delete();
System.out.println("Le fichier a été supprimé");
}
}
catch (FileNotFoundException fnfe) {
System.err.println("Erreur lors de la suppression du fichier : "+fileErrorOutput.getName());
fnfe.printStackTrace();
}
catch (IOException ioe) {
System.out.println("Le client a quitté : "+formateDate());
if (! ioe.getMessage().contains("reset"))
ioe.printStackTrace();
}
}
}
}
/**
* Cette méthode créer et formate la date du jour
@return String
*/
private String formateDate() {
TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
Calendar calendar = Calendar.getInstance(tz);
Date date = new Date();
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
calendar.setTime(date);
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH);
return ("ErrorReport_"+df.format(date)+" "+calendar.get(Calendar.HOUR_OF_DAY)+'_'+calendar.get(Calendar.MINUTE)+'_'+ calendar.get(Calendar.SECOND)+".log").replace('/', '_');
}
public static void main (String [] args) {
new Server();
}
} |
Partager