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
|
import java.net.*;
import java.io.*;
import java.security.*;
import java.nio.channels.*;
class TCPIP{
static ServerSocketChannel serverSocketChannel = null;
static ServerSocket serverSocket = null;
static Socket clientSocket = null;
static boolean fin = false;
public static void main(String arg[]){
try{
System.out.println("Lancement du serveur");
SocketAddress address = new InetSocketAddress(666);
serverSocketChannel = serverSocketChannel.open();
serverSocket = serverSocketChannel.socket();
serverSocket.bind(address);
while(!fin){
try{
clientSocket = serverSocket.accept();
traitements();
} catch(ClosedByInterruptException IException){
fin = true;
}
}
} catch(IOException IException){
IException.printStackTrace();
} finally {
try{
if(serverSocket != null) serverSocket.close();
} catch(Exception IException){
}
try{
if(serverSocketChannel!=null) serverSocketChannel.close();
} catch(Exception IException){
}
}
}
public static void traitements() {
try{
System.out.println("Connection établie");
String messageRecu = "",messageEnvoye,
ip= ""+clientSocket.getInetAddress();
//if(ip.equals("/127.0.0.1")) fin=true;
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintStream out = new PrintStream(clientSocket.getOutputStream());
messageRecu = in.readLine();
String pseudo = messageRecu.substring(0,messageRecu.indexOf(";")),
passe = messageRecu.substring(messageRecu.indexOf(";")+1,messageRecu.indexOf(";",1+messageRecu.indexOf(";"))),
requete = messageRecu.substring(passe.length()+pseudo.length()+2);
messageEnvoye = "";
String idOK;
idOK=Access.verifieID(pseudo,Codeur.MD5(passe),"SELECT * FROM autorisation");
if(!idOK.equals("false")){
int numero = Integer.valueOf(idOK);
idOK=Access.verifieID(pseudo,Codeur.MD5(passe),"SELECT * FROM autorisation");
messageEnvoye=toAString(Access2.newEnleveNull(Access2.newEleveAvecLeTout(Access2.newIdEleves(""+numeroProf))));//,numeroProf
}else messageEnvoye += "ERREUR";
out.println(messageEnvoye);
clientSocket.close();
System.out.println("Connection fermée");
}catch (Exception e) {
e.printStackTrace();
}
}
public static String toAString(String donnee[][]){
String retour="";
for(int i=0;i<donnee.length;i++){
retour+=((char)18);
for(int j=0;j<donnee[i].length;j++)
retour+=((char)8)+donnee[i][j];
}
return retour;
}
} |