FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Experiments/HttpServer.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Experiments
/
HttpServer.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (63 loc) · 2.12 KB
Breadcrumbs
JavaExercises
/
Experiments
/
HttpServer.java
Copy path
File metadata and controls
70 lines (63 loc) · 2.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
net
.
ServerSocket
;
import
java
.
net
.
Socket
;
import
java
.
io
.
InputStream
;
import
java
.
io
.
OutputStream
;
import
java
.
io
.
InputStreamReader
;
import
java
.
io
.
BufferedReader
;
/**
* From https://habr.com/post/69136/
*/
public
class
HttpServer
{
public
static
void
main
(
String
[]
args
)
throws
Throwable
{
ServerSocket
ss
=
new
ServerSocket
(
8080
);
while
(
true
) {
Socket
s
=
ss
.
accept
();
System
.
err
.
println
(
"Client accepted"
);
new
Thread
(
new
SocketProcessor
(
s
)).
start
();
}
}
private
static
class
SocketProcessor
implements
Runnable
{
private
Socket
s
;
private
InputStream
is
;
private
OutputStream
os
;
private
SocketProcessor
(
Socket
s
)
throws
Throwable
{
this
.
s
=
s
;
this
.
is
=
s
.
getInputStream
();
this
.
os
=
s
.
getOutputStream
();
}
public
void
run
() {
try
{
readInputHeaders
();
writeResponse
(
"<html><body><h1>Hello from Habrahabr</h1></body></html>"
);
}
catch
(
Throwable
t
) {
/*do nothing*/
}
finally
{
try
{
s
.
close
();
}
catch
(
Throwable
t
) {
/*do nothing*/
}
}
System
.
err
.
println
(
"Client processing finished"
);
}
private
void
writeResponse
(
String
s
)
throws
Throwable
{
String
response
=
"HTTP/1.1 200 OK
\r
\n
"
+
"Server: YarServer/2009-09-09
\r
\n
"
+
"Content-Type: text/html
\r
\n
"
+
"Content-Length: "
+
s
.
length
() +
"
\r
\n
"
+
"Connection: close
\r
\n
\r
\n
"
;
String
result
=
response
+
s
;
os
.
write
(
result
.
getBytes
());
os
.
flush
();
}
private
void
readInputHeaders
()
throws
Throwable
{
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
is
));
while
(
true
) {
String
s
=
br
.
readLine
();
if
(
s
==
null
||
s
.
trim
().
length
() ==
0
) {
break
;
}
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL