-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.java
More file actions
207 lines (185 loc) · 7.06 KB
/
server.java
File metadata and controls
207 lines (185 loc) · 7.06 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class server {
private static List<connectionContainer> connections = new ArrayList<>();
public static void main(String[] args) {
while (true) {
try {
ServerSocket ss = new ServerSocket(9500);
Socket s = ss.accept();
saveConnection(s, connections, writeConnection(s));
ss.close();
} catch (Exception e) {
System.out.println("Connect failed");
e.printStackTrace();
}
}
}
private void getBoard(int[][] board, String ip) {
try {
File file = new File("connections.txt");
Scanner sc = new Scanner(file);
boolean connectionNotFound = true;
while (connectionNotFound) {
String connection = sc.nextLine();
if (connection.contains(ip)) {
System.out.println(connection);
} else {
}
}
} catch (Exception e) {
}
}
public void dataTransfer(int[][] board, int elementNumber){
try {
connectionContainer cContainer = connections.get(elementNumber);
cContainer.output(cContainer.boardToString(board));
}catch(Exception e){
e.printStackTrace();
}
}
private static void saveConnection(Socket s, List<connectionContainer> sConnections, String opponent) {
boolean notConnected = true;
String answer = " ";
for(int x = 0; x < sConnections.size(); x++){
if(sConnections.get(x).getIP().equals(opponent)){
outputToClient(s,"Y");
answer = inputFromClient(s);
if(answer.equalsIgnoreCase("Y")){
boolean cOpponent = opponentResponse(sConnections.get(x).getSocket(), "Y");
System.out.print("Hello");
if(cOpponent) {
outputToClient(s, "Nooo");
notConnected = updateConnection(s, x, sConnections);
}
}else if(answer.equals("Connection Failed")){
x--;
}
}
}
for (int x = 0; x < sConnections.size(); x++) {
if (!sConnections.get(x).getConnected() && notConnected) {
notConnected = updateConnection(s, x, sConnections);
}else if(!notConnected){
x = sConnections.size();
}
}
if(notConnected) {
connectionContainer sc = new connectionContainer(s);
sConnections.add(sc);
}
}
private static int setPlayer(){
int counter;
Random rand = new Random();
if(rand.nextInt(2) == 0){
counter = 1;
}else{
counter = 2;
}
return counter;
}
private static String writeConnection(Socket s) {
List<String> connections = new ArrayList<>();
String clientIP = s.getInetAddress().toString() + ":" + s.getPort();
try {
FileReader reader = new FileReader("connections.txt");
Scanner sc = new Scanner(reader);
while (sc.hasNextLine()) {
connections.add(sc.nextLine());
}
sc.close();
reader.close();
for(int x = 0; x < connections.size(); x++){
if(connections.get(x).contains(" ")){
String[] splitConnections = connections.get(x).split(" ");
if(clientIP.equals(splitConnections[0])){
return splitConnections[1];
}
else if(clientIP.equals(splitConnections[1])){
return splitConnections[0];
}
}
}
boolean written = false;
for (int x = 0; x < connections.size(); x++) {
System.out.println(connections.get(x));
if (connections.get(x).contains("/") && !connections.get(x).contains(" ")) {
FileWriter writer = new FileWriter("connections.txt", true);
PrintWriter write = new PrintWriter(writer);
write.println(" " + s.getInetAddress().toString() + ":" + s.getPort());
write.close();
writer.close();
x = connections.size();
written = true;
}
}
if(!written){
FileWriter writer = new FileWriter("connections.txt", true);
PrintWriter write = new PrintWriter(writer);
write.print(s.getInetAddress().toString() + ":" + s.getPort());
write.close();
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return "Not Found";
}
private static boolean updateConnection(Socket s, int x, List<connectionContainer> sConnections){
Thread t1 = new Thread(sConnections.get(x));
t1.start();
int counter = setPlayer();
connectionContainer sc = new connectionContainer(s, true, x);
sc.setPlayer(counter);
sConnections.add(sc);
int index = sConnections.indexOf(sc);
sConnections.get(x).setConnected(true);
sConnections.get(x).setElementNumber(index);
if(counter == 1){
sConnections.get(x).setPlayer(2);
}else{
sConnections.get(x).setPlayer(1);
}
try {
sc.output(Integer.toString(sc.getPlayer()));
sConnections.get(x).output(Integer.toString(sConnections.get(x).getPlayer()));
}catch(Exception e){
e.printStackTrace();
}
Thread t2 = new Thread(sc);
t2.start();
return false;
}
private static void outputToClient(Socket s, String data){
try {
OutputStream output = s.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(data);
}catch(Exception e){
e.printStackTrace();
}
}
private static String inputFromClient(Socket s){
String response = "Connection Failed";
try {
InputStream input = s.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
response = reader.readLine();
}catch(Exception e){
e.printStackTrace();
}
return response;
}
private static boolean opponentResponse(Socket s, String data){
String answer = " ";
outputToClient(s, data);
answer = inputFromClient(s);
return answer.equalsIgnoreCase("Y");
}
}