-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
128 lines (96 loc) · 3.19 KB
/
ChatServer.java
File metadata and controls
128 lines (96 loc) · 3.19 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
//
// ChatServer.java
// Created by Ting on 2/18/2003
// Modified : Priyank K. Patel <pkpatel@cs.stanford.edu>
//
// Java General
import java.util.*;
import java.math.BigInteger;
// socket
import java.net.*;
import java.io.*;
// Crypto
import java.security.*;
import java.security.cert.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import javax.security.auth.x500.*;
//import sun.security.x509.*;
public class ChatServer {
private Hashtable _clients;
private Hashtable _chatRoomA;
private Hashtable _chatRoomB;
private int _clientID = 0;
private int _port;
private String _hostName = null;
// Some hints: security related fields.
private String SERVER_KEYSTORE = "serverKeys";
private char[] SERVER_KEYSTORE_PASSWORD = "123456".toCharArray();
private char[] SERVER_KEY_PASSWORD = "123456".toCharArray();
private ServerSocket _serverSocket = null;
private SecureRandom secureRandom;
private KeyStore serverKeyStore;
// private KeyManagerFactory keyManagerFactory;
// private TrustManagerFactory trustManagerFactory;
public ChatServer(int port) {
try {
_clients = new Hashtable<Integer,ClientRecord>();
_chatRoomA = new Hashtable();
_chatRoomB = new Hashtable();
_serverSocket = null;
_clientID = -1;
_port = port;
InetAddress serverAddr = InetAddress.getByName(null);
_hostName = serverAddr.getHostName();
} catch (UnknownHostException e) {
_hostName = "0.0.0.0";
}
}
public static void main(String args[]) {
try {
int port = Integer.parseInt("10500");
ChatServer server = new ChatServer(port);
server.run();
} catch (NumberFormatException e) {
System.out.println("Useage: java ChatServer host portNum");
e.printStackTrace();
return;
} catch (Exception e) {
System.out.println("ChatServer error: " + e.getMessage());
e.printStackTrace();
}
}
/***
*
* Your methods for setting up secure connection
*
*/
public void run() {
try {
_serverSocket = new ServerSocket(_port);
System.out.println("ChatServer is running on "
+ _hostName + " port " + _port);
while (true) {
Socket socket = _serverSocket.accept();
ClientRecord clientRecord = new ClientRecord(socket);
_clients.put(_clientID, clientRecord);
//todo bi alt satirda clientID hatali bi is olabilir.
ChatServerThread thread = new ChatServerThread(this, socket,_clientID++);
thread.start();
}
//_serverSocket.close();
} catch (IOException e) {
System.err.println("Could not listen on port: " + _port);
System.exit(-1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public Hashtable getClientRecords() {
return _clients;
}
}