La ligne est pas marquée mais avec les traces je vois que ça bloque au niveau de la ligne stmt.executeUpdate(query);
J'utilise pas de tableau ou de vecteur, c'est ça le pire !
Je vous mets le code si ça peut aider.
public class PServer {
final static int localport = 8080;
public static int cle = 0;
public static Statement stmt;
// Variables pour accéder à la base de données :
public static String my_password;
public static String my_login;
public static String my_Base;
public static String my_Serveur;
public static void main(String[] args) throws IOException {
ServerSocket serveur = null;
try{
// Connection à la base de données pour sauvegarder les requetes des clients et les réponses du serveur Web
paramConnection();
newConnection();
serveur = new ServerSocket(localport);
System.out.println("Serveur proxy démarré sur le port " + localport + "\n");
// Ecoute infinie des requêtes des clients
while(true){
Socket client = serveur.accept();
cle = cle + 1;
Clients c = new Clients(client,cle);
System.out.println("Nouveau client\n");
}
}
catch (Exception e){System.err.println(e);}
finally{
try{
if (serveur != null) {
serveur.close();
System.out.println("serveur.close");
}
}
catch (Exception e){System.err.println(e);}
}
}
/**
* paramConnection()
* methode qui effectue le paramétrage des variable de connection
*/
public static void paramConnection() {
try {
String chemin = "../Connection.txt";
FileReader fr = new FileReader(chemin);
BufferedReader br = new BufferedReader(fr);
String texte;
for (int i=0; i<4; i++) {
texte = br.readLine();
if (i == 0) my_password = recupParam(texte);
//System.out.println(my_password);
if (i == 1) my_login = recupParam(texte);
//System.out.println(my_login);
if (i == 2) my_Base = recupParam(texte);
//System.out.println(my_Base);
if (i == 3) my_Serveur = recupParam(texte);
//System.out.println(my_Serveur);
} // Fin for.
br.close();
//readLine pour lire une ligne
//note: si il n y a rien, la fonction retournera la valeur null
} // Fin try.
catch(IOException ioe) {
System.out.println("erreur de lecture du fichier de connection reglage par defaut: " + ioe);
my_password = new String(""); //par defaut ""
my_login = new String("root"); //par defaut "root"
my_Base = new String("db1"); //nom de la base (par defaut "db1")
my_Serveur = new String("localhost"); //par defaut "localhost"
} // Fin catch.
} // Fin de paramConnection().
/**
* newConnection() :
* Méthode qui fait la connexion avec la base de données MySQL db1.
*/
public static void newConnection() {
try {
// Chargement du driver JDBC de MySQL.
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println(my_password);
System.out.println(my_Serveur);
System.out.println(my_Base);
System.out.println(my_login);
// Connection à la base de données db1.
java.sql.Connection connection = DriverManager.getConnection((String)("jdbc:mysql://"+my_Serveur+"/"+my_Base+"?user="+my_login+"&password="+my_password));
stmt = connection.createStatement();
} // Fin du Try.
catch (Exception e) {
System.out.println("error : in newConnection");
e.printStackTrace();
System.exit(-1);
} // Fin du Catch.
} // Fin de newConnection();
/**
* String recup_param(String texte)
* methode qui permet de recuperer les parametre entre "" dans le string
*/
private static String recupParam(String texte){
String formater;
char[] tab;
char[] result;
int i,j;
tab = texte.toCharArray();
i = 0;
while (tab[i] != '"')
i++;
i++;
formater = new String();
while (tab[i] != '"')
formater = formater + tab[i++];
return formater;
} // Fin de recupParam().
} // ! PServ class
class Clients extends Thread {
private Socket client;
private int cle;
private String url;
private InputStream sin;
private OutputStream sout;
public static int port = 80;
private String query;
public static Statement stmt;
public Clients(Socket client, int cle){
try{
this.client = client;
this.cle = cle;
start();
}
catch (Exception e){System.err.println(e);}
} // !constructeur
public void run(){
try{
sin = client.getInputStream();
BufferedReader from_client = new BufferedReader(new InputStreamReader(sin));
sout = client.getOutputStream();
/*byte [] buffer = new byte[4096];
int lus;*/
String tmp;
String requete = "";
String user_agent = "";
String host_client = "";
String referer = "";
Socket socket = new Socket();
int position;
boolean fini = false;
// Lecture de la requete du client
while (((tmp = from_client.readLine()) != null) && !fini){
System.out.println(tmp);
String regex0 = "^(GET|POST) +(http[^ ]+) +HTTP/(.\\..)$";
Pattern p0 = Pattern.compile(regex0);
Matcher m0 = p0.matcher(tmp);
while (m0.find()){
for (int i=0; i <= m0.groupCount(); i++){
System.out.println("Groupe " + i + " : " + m0.group(i));
}
query = "INSERT INTO traces_clients(chaine_entiere) VALUES ('GET')";
System.out.println("ça passe1");
stmt.executeUpdate(query);
System.out.println("ça passe2");
// insertion dans la base des données de l'utilisateur
//System.out.println("INSERT INTO traces_clients(chaine_entiere,methode,URL,norme_HTTP) VALUES ('" + m0.group(0) + "', '" + m0.group(1) + "', '" + m0.group(2) + "', '" + m0.group(3) + "')");
//query = "INSERT INTO traces_clients(chaine_entiere,methode,URL,norme_HTTP) VALUES ('" + m0.group(0) + "', '" + m0.group(1) + "', '" + m0.group(2) + "', '" + m0.group(3) + "')";
//stmt.executeUpdate(query);
}
if (tmp.trim().toUpperCase().startsWith("GET")) {
position = tmp.trim().toUpperCase().lastIndexOf("HTTP") > 0 ? tmp.toUpperCase().lastIndexOf("HTTP") : tmp.length();
url = tmp.trim().substring(11,position-2).trim();
requete = tmp;
} else if (tmp.trim().toUpperCase().startsWith("HEAD") || tmp.trim().toUpperCase().startsWith("POST")) {
position = tmp.trim().toUpperCase().lastIndexOf("HTTP") > 0 ? tmp.toUpperCase().lastIndexOf("HTTP") : tmp.length();
url = tmp.trim().substring(12,position-2).trim();
requete = tmp;
}
// il faut recuperer le User-Agent, le Host et le Referer
String regex = "^User-Agent: +(.*\\))$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(tmp);
while (m.find()){
user_agent = m.group(0);
}
String regex2 = "^Host: +(.*)";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(tmp);
while (m2.find()){
host_client = m2.group(1);
}
String regex3 = "^Referer: +(.*)";
Pattern p3 = Pattern.compile(regex3);
Matcher m3 = p3.matcher(tmp);
while (m3.find()){
referer = m3.group(1);
}
// On arrete de lire quand il n'y a plus rien à lire
String regex4 = "^[a-zA-Z]";
Pattern p4 = Pattern.compile(regex4);
Matcher m4 = p4.matcher(tmp);
if (!m4.find()) {
fini = true;
System.out.println("fini " + fini);
break;
}
}
System.out.println("Url demandée : " + url + "\n");
//PARTIE CONNEXION AU SERVEUR WEB
if (host_client != "") {
socket = new Socket(InetAddress.getByName(host_client),port);
System.out.println("socket créé");
}
else if (referer != ""){
socket = new Socket(InetAddress.getByName(referer),port);
System.out.println("socket créé");
} else {
socket = new Socket(InetAddress.getByName(url),port);
System.out.println("socket créé");
}
PrintStream sortant = new PrintStream(socket.getOutputStream());
// On envoie au serveur Web la requete du client
if (host_client != "") {
sortant.println(requete + " \r\n" + user_agent + "\r\n" + "Host: " + host_client + "\r\n" );
} else {
sortant.println(requete + " \r\n" + user_agent + "\r\n" );
}
// Lecture de la réponse du serveur Web
PrintStream sout = new PrintStream(client.getOutputStream());
//BufferedReader entrant = new BufferedReader(new InputStreamReader(socket.getInputStream()));
InputStream entrant = socket.getInputStream();
System.out.println("Lecture de la réponse du serveur ...");
FileWriter sortie = new FileWriter("fichier.txt");
boolean html = false;
boolean pas_encore = false;
byte [] buffer = new byte[4096];
int lus;
while ((lus = entrant.read(buffer)) != -1) {
sout.write(buffer,0,lus);
System.out.write(buffer,0,lus);
}
socket.close();
System.out.println("socket.close();");
client.close();
System.out.println("client.close();");
}
catch (Exception e){System.err.println(e);}
} // ! run() method
} // ! Clients class