forked from liuxt/mini_messenger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer_v2.java
More file actions
126 lines (112 loc) · 4.35 KB
/
Server_v2.java
File metadata and controls
126 lines (112 loc) · 4.35 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
import com.sun.xml.internal.fastinfoset.sax.SystemIdResolver;
import com.sun.xml.internal.ws.api.server.ThreadLocalContainerResolver;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import java.util.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.Thread;
/**
* Created by liuxt on 15/12/20.
*/
public class Server_v2 {
private static final int PORT = 4444;
private static final int BACKLOG = 100;
private UserInfoManager userInfoManager;
private ServerSocket server;
private class ClientHandler implements Runnable{
private BufferedReader reader;
private PrintWriter writer;
private Socket connection;
public ClientHandler(Socket sock, BufferedReader bufferedReader, PrintWriter printWriter){
try {
System.out.println("Try to new a Thread");
this.reader = bufferedReader;
this.writer = printWriter;
this.connection = sock;
} catch (NullPointerException nullPointerException){
System.out.println("Failed to new a ClientHandler");
nullPointerException.printStackTrace();
}
}
public void run(){
try {
System.out.println("Thread running, try to listen from client");
String message;
while ((message = reader.readLine()) != null){
System.out.println(message);
}
} catch(Exception exception){
System.out.println("Failed to receive message");
exception.printStackTrace();
} finally{
closeCrap(connection, writer, reader);
}
}
}
// constructor
public Server_v2(){
System.out.println("Server is created, waiting for activation");
}
// Start the server
public void startRunning(){
try {
server = new ServerSocket(PORT, BACKLOG);
userInfoManager = new UserInfoManager();
while(true) {
try {
Socket connection = waitForConnection();
SocketPair sockPair = setupStreams(connection);
Thread listener = new Thread(new ClientHandler(sockPair.getSock(), sockPair.getReader(), sockPair.getWriter()));
listener.start();
} catch (Exception eofException) { // later use
System.out.println("Server ended the connection");
} finally {
//closeCrap();
}
}
} catch(IOException ioException){
System.out.println("Fail to start run server");
ioException.printStackTrace();
}
}
private Socket waitForConnection(){
System.out.println("Wait for incoming connection");
try {
Socket connection = server.accept();
System.out.println("Successfully get connection from " + connection.getInetAddress().getHostName());
return connection;
} catch(IOException ioException){
System.out.println("Unable to accept client socket");
ioException.printStackTrace();
return null;
}
}
private SocketPair setupStreams(Socket connection){
try{
System.out.println("Trying to setup Streams");
PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
return new SocketPair(connection, bufferedReader, printWriter);
} catch (IOException ioexception){
System.out.println("Failed to setup Streams");
ioexception.printStackTrace();
return null;
}
}
private void closeCrap(Socket connection, PrintWriter writer, BufferedReader reader){
try{
writer.close();
reader.close();
connection.close();
System.out.println("Successfully close streams");
} catch (IOException ioException){
System.out.println("Failed to close streams.");
ioException.printStackTrace();
}
}
public static void main(String[] args){
Server_v2 server_v2 = new Server_v2();
server_v2.startRunning();
}
}