-
Notifications
You must be signed in to change notification settings - Fork 9
/
LoadBalanceSocket.java
63 lines (44 loc) · 1.91 KB
/
LoadBalanceSocket.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
package loadBalance;
import java.net.Socket;
public class LoadBalanceSocket implements Runnable {
Socket conSock;
ServerObject selectedServer;
public LoadBalanceSocket(Socket conSock) {
this.conSock = conSock;
}
@Override
public void run() {
int serverNumber=LoadBalanceListener.serverSelector;
LoadBalanceListener.serverSelector = (LoadBalanceListener.serverSelector + 1) % LoadBalance.servers.size();
this.selectedServer = LoadBalance.servers.get(serverNumber);
System.out.println(selectedServer.getHost());
// Open connection to selected server
try {
Socket selectedServerSocket = new Socket(selectedServer.getHost(),selectedServer.getPort());
//Socket selectedServerSocket = new Socket("localhost", 17100);
/*
BufferedOutputStream serverBufOutStream = new BufferedOutputStream(
selectedServerSocket.getOutputStream());
BufferedInputStream serverBufInStream = new BufferedInputStream(
selectedServerSocket.getInputStream());
BufferedInputStream clientBufInStream = new BufferedInputStream(
conSock.getInputStream());
BufferedOutputStream clientBufOutStream = new BufferedOutputStream(conSock.getOutputStream());
*/
/*
BufferedReader serverBufReader = new BufferedReader(new InputStreamReader(selectedServerSocket.getInputStream()));
BufferedReader clientBufReader = new BufferedReader(new InputStreamReader(conSock.getInputStream()));
PrintStream clientPrinter = new PrintStream(conSock.getOutputStream());
PrintStream serverPrinter= new PrintStream(selectedServerSocket.getOutputStream());
*/
WriteToClient clientWriter = new WriteToClient(conSock, selectedServerSocket);
WriteToServer serverWriter = new WriteToServer(conSock, selectedServerSocket);
Thread cw = new Thread(clientWriter);
Thread sw = new Thread(serverWriter);
cw.start();
sw.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}