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
|
public class serveur extends Thread {
private BufferedReader in;
private PrintWriter out;
private Socket X;
private ServerSocket S;
public void run() {
try {
S = new ServerSocket(1004);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s = "jhvj";
while (true) {
try {
X = S.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connect();
s = read();
String[] t = s.split(" HTTP", 2);
String[] u = t[0].split("/", 2);
File fich;
if (u[1].equals("")) {
fich = new File("C:/ggg/index.html");
System.out.println("1" + u[1]);
} else {
fich = new File("C:/ggg/" + u[1]);
System.out.println("2" + u[1]);
}
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fich));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = "";
try {
line = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (line != null) {
write(line);
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
close();
}
}
private void connect() {
try {
in = new BufferedReader(new InputStreamReader(X.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(X
.getOutputStream())), true);
} catch (IOException e) {
try {
X.close();
} catch (IOException e2) {
}
}
}
public void write(String str) {
out.println(str);
}
public String read() {
String str = "";
try {
str = in.readLine();
} catch (IOException e) {
}
return str;
}
public void close() {
try {
X.close();
} catch (IOException e) {
}
}
public static void main(String[] args) throws IOException {
new serveur().start();
}
} |
Partager