Hi folks,
Ik probeer van een multithreaded webserver een singlethreaded webserver te maken maar t wil nog niet echt lukken. Ik denk dat de truc in dit stukje zit:
public void run() {
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
Kan iemand mij vertellen hoe ik van de volgende code een singlethreaded webserver maak?
Thanks alvast!!!
Ik probeer van een multithreaded webserver een singlethreaded webserver te maken maar t wil nog niet echt lukken. Ik denk dat de truc in dit stukje zit:
public void run() {
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
Kan iemand mij vertellen hoe ik van de volgende code een singlethreaded webserver maak?
code:
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
| package mypackage1;
import java.util.*;
import java.io.*;
import java.net.*;
class ClassStringTokenizer{
public static void main(String args[]) throws IOException {
//geef een poort waarnaar de ServerSocket luistert
ServerSocket portlistener = new ServerSocket(12345);
while (true) {
try{
HttpRequest request= new HttpRequest (portlistener.accept());
Thread thread = new Thread (request);
thread.start();
}
catch(Exception e){}
}
}
}
final class HttpRequest implements Runnable {
final static String CRLF ="\r\n";
Socket socket;
public HttpRequest(Socket socket) throws Exception {
this.socket = socket;
}
public void run() {
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
private void processRequest() throws Exception {
InputStreamReader socketstream = new InputStreamReader(this.socket.getInputStream());
BufferedReader socketbuffer = new BufferedReader(socketstream);
String requestLine=socketbuffer.readLine();
DataOutputStream socketoutput = new DataOutputStream(this.socket.getOutputStream());
System.out.println(requestLine);
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken();
String fileName= tokens.nextToken();
if(fileName.startsWith("/"))
fileName = fileName.substring(1,fileName.length());
//Open the requested file.
FileInputStream fis = null;
boolean fileExists = true;
try {
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
fileExists = false;
}
// Construct the response message.
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
if (fileExists) {
statusLine = "HTTP/1.0 200 OK" + CRLF;
contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
} else {
statusLine = "HTTP/1.0 404 Not Found" + CRLF;
contentTypeLine = "NONE";
entityBody = "\n\n Not Found";
}
// Send the status line.
socketoutput.writeBytes(statusLine);
// Send the content type line.
socketoutput.writeBytes(contentTypeLine);
// Send a blank line to indicate the end of the header lines.
socketoutput.writeBytes(CRLF);
// Send the entity body.
if (fileExists) {
sendBytes(fis, socketoutput);
fis.close();
} else {
socketoutput.writeBytes(entityBody);
}
//Close the streams
socketoutput.close();
socketbuffer.close();
socket.close();
}
private String contentType(String fileName) {
if(fileName.endsWith(".htm") || fileName.endsWith(".html"))
return "text/html";
else if(fileName.endsWith(".gif"))
return "image/gif";
else if(fileName.endsWith(".txt"))
return "text/plain";
else
return "application/octet-stream";
}
private static void sendBytes(FileInputStream fis, OutputStream socketoutput) throws Exception {
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] buffer = new byte[1024];
int bytes = 0;
// Copy requested file into the socket's output stream.
while((bytes = fis.read(buffer)) != -1 )
socketoutput.write(buffer, 0, bytes);
}
} |
Thanks alvast!!!