no save
Assistance
Achat
News

Forum | programmation
[java] probleme quand je fais des methodes
karine, le lun. 02 mai 2005 à 14:26:16
ok, avant j'avais ça :

class Clients extends Thread {
private Socket client;
private Statement stmt;
private int numero;
private InputStream sin;
private OutputStream sout;
public static int port = 80;
private static String query;

public Clients(Socket client, Statement stmt, int numero){
try{
this.client = client;
this.stmt = stmt;
this.numero = numero;
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();

Socket socket = new Socket();

String tmp;
boolean fini = false;
int position;
int cle = 0;
String url = "";
String requete = "";
String user_agent = "";
String host_client = "";
String referer = "";


/*********************************************/
/* PARTIE TRAITEMENT DE LA REQUETE DU CLIENT */
/*********************************************/

// 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));
}


// On récupère la dernière valeur ID ajoutée dans la base
int cle_avant = 0;

query = "SELECT MAX(ID) FROM traces_clients";
ResultSet result = stmt.executeQuery(query);
while ( result.next() )
{
cle_avant = result.getInt(1);
}

cle = cle_avant + 1;

query = "INSERT INTO traces_clients(ID,chaine_entiere,methode,URL,norme_HTTP) VALUES ('" + cle + "', '" + 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);
query = "UPDATE traces_clients SET User_Agent = '" + m.group(1) + "' WHERE ID = " + cle;
stmt.executeUpdate(query);
}

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 */
/***********************************/

// Creation de la socket entre le proxy et le 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 != "") {
System.out.println(requete + user_agent + "\r\n" + "Host: " + host_client + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");
sortant.println(requete + user_agent+ "\r\n" + "Host: " + host_client + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");

} else {
System.out.println(requete + "\r\n" + user_agent + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");
sortant.println(requete + user_agent + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");

}



/**************************************/
// Lecture de la réponse du serveur Web
/**************************************/

PrintStream sout = new PrintStream(client.getOutputStream());
System.out.println("sout");
InputStream entrant = socket.getInputStream();
System.out.println("entrant");
System.out.println("Lecture de la réponse du serveur ...");

byte [] buffer = new byte[4096];
int lus;
String ligne = "";


// on lit byte par byte la réponse du serveur Web et on l'envoie au client
while ((lus = entrant.read(buffer)) != -1) {

sout.write(buffer,0,lus);

}


sout.close();
socket.close();
System.out.println("socket.close();");
client.close();

}
catch (Exception e){System.err.println(e);}
} // ! run() method

} // ! Clients class



Après j'ai transformé comme ça :

class Clients extends Thread {
private Socket client;
private static Statement stmt;
private int numero;
private InputStream sin;
private OutputStream sout;
public static int port = 80;
private static String query;
private static int cle;
private static String url = "";
private static String requete = "";
private static String user_agent = "";
private static String host_client = "";
private static String referer = "";

public Clients(Socket client, Statement stmt, int numero){
try{
this.client = client;
this.stmt = stmt;
this.numero = numero;
start();
}
catch (Exception e){System.err.println(e);}
} // !constructeur

public void run(){
try{

// sout = client.getOutputStream();

Socket socket = new Socket();

String tmp;


/*********************************************/
/* PARTIE TRAITEMENT DE LA REQUETE DU CLIENT */
/*********************************************/
System.out.println("appel lire_requete_client(client)" + client);
lire_requete_client(client);
System.out.println("retour lire_requete_client(client)" + client);


/***********************************/
/* PARTIE CONNEXION AU SERVEUR WEB */
/***********************************/
System.out.println("connexion serveur web");
// Creation de la socket entre le proxy et le 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 {
System.out.println("avant");
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 != "") {
System.out.println(requete + user_agent + "\r\n" + "Host: " + host_client + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");
sortant.println(requete +"\r\n"+ user_agent+ "\r\n" + "Host: " + host_client + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");

} else {
System.out.println(requete + "\r\n" + user_agent + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");
sortant.println(requete + "\r\n"+user_agent + "\r\n" + "Pragma: no-cache" + "\r\n" + "Cache-control: no-cache" + "\r\n");

}



/**************************************/
// Lecture de la réponse du serveur Web
/**************************************/

PrintStream sout = new PrintStream(client.getOutputStream());
System.out.println("sout");
InputStream entrant = socket.getInputStream();
System.out.println("entrant");
System.out.println("Lecture de la réponse du serveur ...");

byte [] buffer = new byte[4096];
int lus;
String ligne = "";


// on lit byte par byte la réponse du serveur Web et on l'envoie au client
//BufferedReader bin = new BufferedReader(new InputStreamReader(entrant));
while ((lus = entrant.read(buffer)) != -1) {

sout.write(buffer,0,lus);

}


sout.close();
socket.close();
System.out.println("socket.close();");

client.close();
System.out.println("client.close");
}
catch (Exception e){System.err.println(e);}
} // ! run() method

private static void lire_requete_client(Socket client){
String tmp;
boolean fini = false;
int position;


try {
InputStream sin = client.getInputStream();
BufferedReader from_client = new BufferedReader(new InputStreamReader(sin));

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));
}


// On récupère la dernière valeur ID ajoutée dans la base
int cle_avant = 0;

query = "SELECT MAX(ID) FROM traces_clients";
ResultSet result = stmt.executeQuery(query);
while ( result.next() )
{
cle_avant = result.getInt(1);
}

cle = cle_avant + 1;

query = "INSERT INTO traces_clients(ID,chaine_entiere,methode,URL,norme_HTTP) VALUES ('" + cle + "', '" + 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);
query = "UPDATE traces_clients SET User_Agent = '" + m.group(1) + "' WHERE ID = " + cle;
stmt.executeUpdate(query);
}

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");


} catch (Exception e){System.err.println(e);}

} // ! lire_requete_client()

} // ! Clients class

PrécédentEdoc
mai 05
Edoc
mai 05
Suivant
REPONSES
Edoc
mai 05
karine
mai 05
Edoc
mai 05
Edoc
mai 05
karine
mai 05
Version Web
Réalisé par RedShift
no save