-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
117 lines (91 loc) · 3.27 KB
/
server.java
File metadata and controls
117 lines (91 loc) · 3.27 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
package sample;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.function.Consumer;
public class ServerSide {
ArrayList<clientThread> cl = new ArrayList<clientThread>();
Consumer<Serializable> callback;
int port;
serverThread s;
clientThread c1;
clientThread c2;
ServerSide(int port,Consumer<Serializable> callback){
this.port = port;
this.callback = callback;
s = new serverThread();
s.start();
}
class serverThread extends Thread{
int clientNum = 1;
public void run() {
try(ServerSocket server = new ServerSocket(port)){
callback.accept("Connection Estb.");
while(true) {
clientThread c = new clientThread(server.accept(),clientNum);
FXNet.setNumClients(clientNum++);
cl.add(c);
c.start();
}
}
catch(Exception e) {
callback.accept("Connection Closed Server Thread");
}
}
}
public void sendList() throws Exception{
ArrayList<Integer> l = new ArrayList<Integer>();
for(clientThread p: cl){
l.add(p.clientNum);
}
int i = 0;
for(clientThread p: cl){
System.out.println("Send in array to client");
p.out.writeObject("ArrayList");
System.out.println("After Send in array to client");
p.out.writeObject(l);
// if(i != cl.indexOf(p)){
// p.out.writeObject("Array");
// System.out.println("After Send in array to client");
// p.out.writeObject(l);
// }
// i++;
}
}
class clientThread extends Thread{
int clientNum;
Socket socket;
ObjectOutputStream out;
ObjectInputStream in;
clientThread(Socket socket,int clientNum){
this.socket = socket;
this.clientNum = clientNum;
}
public void run() {
try(ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream())){
this.out = out;
this.in = in;
callback.accept("Client Connected " + clientNum);
//out.writeObject("Connected to server.");
sendList();
while(true) {
Serializable data = (Serializable) in.readObject();
//callback.accept((Serializable) data);
if(data.toString().equals("--")){
Serializable tem = (Serializable) in.readObject();
Serializable temp = (Serializable) in.readObject();
System.out.println("========= "+ temp.toString());
}
System.out.println("After Send in array to client thread");
//game goes here
}
}catch(Exception e) {
callback.accept("Connection Closed Client Thread");
}
}
}
}