forked from hamzaouaddafe/AnnonceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerTCP.java
72 lines (60 loc) · 2.56 KB
/
ServerTCP.java
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
import java.io.IOException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
public class ServerTCP {
private int port;
private SSLServerSocket server = null;
private SSLSocket client = null;
private int clientCount = 0;
private String password = "415263";
public ServerTCP() {}
public ServerTCP(int port) {
this.port = port;
}
public int getClientCount() {
return clientCount;
}
public void setClientCount(int clientCount) {
this.clientCount = clientCount;
}
public void startServer() {
System.out.println(">> start running encyrted tcp server.");
try {
/*
* System.setProperty("javax.net.ssl.keyStore", "server.jsk");
* System.setProperty("javax.net.ssl.keyStorePassword" , "123456");
* SSLServerSocketFactory serverSocketFactory = (SSLServerSocketFactory)
* SSLServerSocketFactory.getDefault(); server =
* (SSLServerSocket)serverSocketFactory.createServerSocket(1027);
* server.setEnabledCipherSuites(serverSocketFactory.getDefaultCipherSuites());
*/
System.setProperty("javax.net.ssl.keyStore", "server.jsk");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
SSLServerSocketFactory factory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket server = (SSLServerSocket) factory.createServerSocket(1027);
// server = new ServerSocket(this.port);
System.out.println("## Server start listening on port (" + this.port + ")");
while (true) {
client = (SSLSocket) server.accept();
clientCount++;
ServerThread serverThread = new ServerThread(client, clientCount, this);
serverThread.start();
}
} catch (Exception e) {
System.err.println("Error in startServer() : " + e.getMessage());
}
}
public static void main(String[] args) throws IOException {
if (args.length == 0) {
ServerTCP serverobj = new ServerTCP(1027);
serverobj.startServer();
} else if (args.length == 1) {
if ("no-encryption".equals(args[0])) {
ServerTCP serverTCP = new ServerTCPNotSecure(1027);
serverTCP.startServer();
}
} else System.out.println("## bad arguments : java ServerTCP <option 'no-encryption'>");
}
}