-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
178 lines (140 loc) · 5.33 KB
/
Server.java
File metadata and controls
178 lines (140 loc) · 5.33 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package sudoku;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class Server extends JFrame implements Runnable {
// Text area for displaying contents
private JTextArea ta;
// db connection url string
private String url = "jdbc:sqlite:javabook.db";
// Number a client
private int clientNo = 0;
public Server() {
ta = new JTextArea(10,10);
JScrollPane sp = new JScrollPane(ta);
this.add(sp);
this.setTitle("MultiThreadServer");
this.setSize(400,200);
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
ta.append("MultiThreadServer started at "
+ new Date() + '\n');
while (true) {
// Listen for a new connection request
Socket socket = serverSocket.accept();
// Increment clientNo
clientNo++;
ta.append("Starting thread for client " + clientNo +
" at " + new Date() + '\n');
// Find the client's host name, and IP address
InetAddress inetAddress = socket.getInetAddress();
ta.append("Client " + clientNo + "'s host name is "
+ inetAddress.getHostName() + "\n");
ta.append("Client " + clientNo + "'s IP Address is "
+ inetAddress.getHostAddress() + "\n");
// Create and start a new thread for the connection
new Thread(new HandleAClient(socket, clientNo)).start();
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
// Define the thread class for handling new connection
class HandleAClient implements Runnable {
private Socket socket; // A connected socket
private int clientNum;
private PreparedStatement queryStmt;
/** Construct a thread */
public HandleAClient(Socket socket, int clientNum) {
this.socket = socket;
this.clientNum = clientNum;
}
/** Run a thread */
public void run() {
try {
// Create data input and output streams
ObjectInputStream inputFromClient = new ObjectInputStream(
socket.getInputStream());
ObjectOutputStream outputToClient = new ObjectOutputStream(
socket.getOutputStream());
// Continuously serve the client
while (true) {
// Send back to the client
int searchId;
int state = inputFromClient.readInt();
try {
Connection connect = DriverManager.getConnection(url);
/// For Saving a game state to the database
if(state == 100) {
System.out.println("Save State");
// SQL Query for saving the state to the DB Table
// 'Players' table has only two columns - Id of type integer and gamestate of type blob
queryStmt = connect.prepareStatement("INSERT INTO Players (gamestate) VALUES (?)");
queryStmt.setBytes(1, getBytes(inputFromClient.readObject())); // read the incoming object
queryStmt.executeUpdate();
queryStmt.close();
ta.append("Game -"+this.clientNum+"\' has been stored to the database");
}
// For Loading a saved game from the database
else if(state == 101){
searchId = inputFromClient.readInt();
queryStmt = connect.prepareStatement("SELECT * FROM Players WHERE id=?");
queryStmt.setInt(1, searchId);
ResultSet rset = queryStmt.executeQuery();
while(rset.next()) {
outputToClient.writeInt(rset.getInt("id"));
outputToClient.writeObject(getObject(rset.getBytes("gamestate")));
outputToClient.flush();
}
queryStmt.close();
ta.append("Game "+this.clientNum+"\' restored");
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
catch(Exception ex) {
System.out.println("SERVER:C");
ex.printStackTrace();
}
}
}
public static byte[] getBytes(Object obj) throws java.io.IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte[] data = bos.toByteArray();
return data;
}
public static Object getObject(byte[] bytearr) throws IOException, ClassNotFoundException{
ByteArrayInputStream bis = new ByteArrayInputStream(bytearr);
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
public static void main(String[] args) {
Server new_s = new Server();
new_s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new_s.setVisible(true);
}
}