FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 2/J2Lesson7/SimpleServer.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
/
Java 2
/
J2Lesson7
/
SimpleServer.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
123 lines (116 loc) · 4.2 KB
Breadcrumbs
JavaExercises
/
Java 2
/
J2Lesson7
/
SimpleServer.java
Copy path
File metadata and controls
123 lines (116 loc) · 4.2 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
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
/**
* Java. Level 2. Lesson 7
* Simple server for chat
*
* @author Sergey Iryupin
* @version 0.2.2 dated Jan 15, 2018
*/
import
java
.
io
.*;
import
java
.
net
.*;
import
java
.
sql
.*;
class
SimpleServer
implements
IConstants
{
public
static
void
main
(
String
[]
args
) {
new
SimpleServer
();
}
SimpleServer
() {
int
clientCount
=
0
;
System
.
out
.
println
(
SERVER_START
);
try
(
ServerSocket
server
=
new
ServerSocket
(
SERVER_PORT
)) {
while
(
true
) {
Socket
socket
=
server
.
accept
();
System
.
out
.
println
(
"#"
+ (++
clientCount
) +
CLIENT_JOINED
);
new
Thread
(
new
ClientHandler
(
socket
,
clientCount
)).
start
();
}
}
catch
(
Exception
ex
) {
System
.
out
.
println
(
ex
.
getMessage
());
}
System
.
out
.
println
(
SERVER_STOP
);
}
/**
* checkAuthentication: check login and password
*
* @param login for checking
* @param passwd for checking
*
* @return if the pair login/passwd is found in the database,
* authentication is successful
*/
private
boolean
checkAuthentication
(
String
login
,
String
passwd
) {
boolean
result
=
false
;
try
{
// loads a class, including running its static initializers
Class
.
forName
(
DRIVER_NAME
);
// connect db
Connection
connect
=
DriverManager
.
getConnection
(
SQLITE_DB
);
// looking for login && passwd in db
PreparedStatement
pstmt
=
connect
.
prepareStatement
(
SQL_SELECT
);
// replace "?" to the login
pstmt
.
setString
(
1
,
login
);
// returns ResultSet object generated by the query
ResultSet
rs
=
pstmt
.
executeQuery
();
// process rows from the query result
while
(
rs
.
next
())
result
=
rs
.
getString
(
PASSWD_COL
).
equals
(
passwd
);
// close all
rs
.
close
();
pstmt
.
close
();
connect
.
close
();
}
catch
(
ClassNotFoundException
|
SQLException
ex
) {
ex
.
printStackTrace
();
return
false
;
}
return
result
;
}
/**
* ClientHandler: service requests of clients
*/
class
ClientHandler
implements
Runnable
{
BufferedReader
reader
;
PrintWriter
writer
;
Socket
socket
;
String
name
;
ClientHandler
(
Socket
clientSocket
,
int
clientCount
) {
try
{
socket
=
clientSocket
;
reader
=
new
BufferedReader
(
new
InputStreamReader
(
socket
.
getInputStream
()));
writer
=
new
PrintWriter
(
socket
.
getOutputStream
());
name
=
"Client #"
+
clientCount
;
}
catch
(
Exception
ex
) {
System
.
out
.
println
(
ex
.
getMessage
());
}
}
@
Override
public
void
run
() {
String
message
;
try
{
do
{
message
=
reader
.
readLine
();
if
(
message
!=
null
) {
System
.
out
.
println
(
name
+
": "
+
message
);
if
(
message
.
startsWith
(
AUTH_SIGN
)) {
String
[]
wds
=
message
.
split
(
" "
);
if
(
checkAuthentication
(
wds
[
1
],
wds
[
2
])) {
name
=
wds
[
1
];
writer
.
println
(
"Hello, "
+
name
);
writer
.
println
(
"
\0
"
);
}
else
{
System
.
out
.
println
(
name
+
": "
+
AUTH_FAIL
);
writer
.
println
(
AUTH_FAIL
);
message
=
EXIT_COMMAND
;
}
}
else
if
(!
message
.
equalsIgnoreCase
(
EXIT_COMMAND
)) {
writer
.
println
(
"echo: "
+
message
);
writer
.
println
(
"
\0
"
);
}
writer
.
flush
();
}
}
while
(!
message
.
equalsIgnoreCase
(
EXIT_COMMAND
));
socket
.
close
();
System
.
out
.
println
(
name
+
CLIENT_DISCONNECTED
);
}
catch
(
Exception
ex
) {
System
.
out
.
println
(
ex
.
getMessage
());
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL