-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNimServer.java
More file actions
68 lines (60 loc) · 2.41 KB
/
NimServer.java
File metadata and controls
68 lines (60 loc) · 2.41 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
import java.net.ServerSocket;
import java.net.Socket;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class NimServer{
public static void main(String[] args){
//initiate vars
Socket player1=null, player2=null;
boolean hasUnpairedPlayer=false;
ClientHandler handler=null;
String firstPlayer="", secondPlayer="", input="";
ServerSocket serverSock=null;
BufferedReader clientInput1=null, clientInput2=null;
BufferedWriter clientOutput1=null,clientOutput2=null;
try{
System.out.println("Waiting for client connections on port 7654.");
serverSock = new ServerSocket(7654);
while(true){
//connect player 1
player1=serverSock.accept();
clientInput1 = new BufferedReader(new InputStreamReader(player1.getInputStream()));
clientOutput1= new BufferedWriter(new OutputStreamWriter(player1.getOutputStream()));
input=clientInput1.readLine();
if(input.startsWith("HELLO ")){
clientOutput1.write("100\n");
clientOutput1.flush();
firstPlayer=input.substring(6);
System.out.println(firstPlayer+" is connected and waiting for opponent.");
//connect player 2
player2=serverSock.accept();
clientInput2 = new BufferedReader(new InputStreamReader(player2.getInputStream()));
clientOutput2= new BufferedWriter(new OutputStreamWriter(player2.getOutputStream()));
input=clientInput2.readLine();
secondPlayer=input.substring(6);
clientOutput1.write("200 "+secondPlayer+"\n");
clientOutput1.flush();
clientOutput2.write("200 "+firstPlayer+"\n");
clientOutput2.flush();
//pair clients for game
System.out.println(firstPlayer+" is paired with "+secondPlayer+".");
NimBoard board = new NimBoard();
clientOutput1.write(board.toString());
clientOutput1.flush();
clientOutput2.write(board.toString());
clientOutput2.flush();
handler = new ClientHandler(player1,player2,board);
Thread theThread=new Thread(handler);
theThread.start();
}
}
}
//exception
catch(IOException e){
System.out.println(e.getMessage());
}
}
} // NimServer