-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiServer1.java
112 lines (81 loc) · 2.96 KB
/
MultiServer1.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
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
/***
*
* Created by andri on 02/07
*/
import java.io.*;
import java.net.*;
import java.util.*;
class MultiServerToClientDaemon1 implements Runnable{
final private ServerSocket sSocket;
final private int sId;
MultiServerToClientDaemon1(ServerSocket sSocket) {
this.sSocket = sSocket;
sId = sSocket.getLocalPort() % 4000;
}
public void run(){
Socket cSocket;
//System.out.println("Starting Server " + sId);
try{
while(true){
// System.out.println("Waiting for a new client request...");
cSocket = sSocket.accept();
//System.out.println("New client connection established");
//create MultiServerThread1 to serve client request
new Thread(new MultiServerToClientThread1(cSocket)).start();
}
}
catch (Exception e) {
//System.out.println("Server" + sId + " " + MultiServer1.accounts);
}
}
}
class MultiServerToServerDaemon1 implements Runnable{
final private ServerSocket sSocket;
final private int sId;
MultiServerToServerDaemon1(ServerSocket sSocket) {
this.sSocket = sSocket;
sId = sSocket.getLocalPort() % 8000;
}
public void run(){
Socket cSocket;
System.out.println("Starting Server " + sId);
try{
while(true){
//System.out.println("Waiting for a new server request...");
cSocket = sSocket.accept();
//System.out.println("New server connection established");
//create MultiServerThread1 to serve other server's request
new Thread(new MultiServerToServerThread1(cSocket)).start();
}
}
catch (Exception e) {
//System.out.println("Server" + sId + " " + MultiServer1.accounts);
}
}
}
public class MultiServer1 {
protected static Map<Integer, Object> account_lock = new HashMap<>();
public static Map<Integer, Integer> accounts = new HashMap<>();
protected static int[] servers_ports = {8002, 8003};
protected static final int serverId = 1;
public static void main(String[] args){
try{
//initialize the accounts of the server
Thread initialization_thread = new Thread(new InitializeThread1());
initialization_thread.start();
initialization_thread.join();
try {
ServerSocket server_client_socket = new ServerSocket(4000 + serverId);
new Thread(new MultiServerToClientDaemon1(server_client_socket)).start();
ServerSocket server_server_socket = new ServerSocket(8000 + serverId );
new Thread(new MultiServerToServerDaemon1(server_server_socket)).start();
}
catch(Exception e) {
e.printStackTrace();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}