-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
74 lines (69 loc) · 2.71 KB
/
Server.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
//imports for network communication
import java.util.ArrayList;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class Server {
final int PORT = 5001;
ServerSocket serverSocket;
Socket clientSocket;
PrintWriter output;
BufferedReader input;
int clientCounter = 0;
Game game = new Game();
ArrayList<Thread> clients = new ArrayList<Thread>();
public static void main(String[] args) throws Exception{
Server server = new Server();
server.go();
}
public void go() throws Exception{
serverSocket = new ServerSocket(PORT);
while(true) {
System.out.println("Waiting for a connection request from a client ...");
clientSocket = serverSocket.accept();
clientCounter = clientCounter +1;
System.out.println("Client "+clientCounter+" connected");
Thread connectionThread = new Thread(new ConnectionHandler(clientSocket));
connectionThread.start();
clients.add(connectionThread);
}
}
//------------------------------------------------------------------------------
class ConnectionHandler extends Thread {
Socket socket;
PrintWriter output;
BufferedReader input;
String name;
public ConnectionHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream());
//receive a message from the client
name = input.readLine();
//send a response to the client
output.println(clientCounter);
output.flush();
Card attackCard = new AttackCard("Attack","AttackCard.png");
Card bombCard = new BombCard("Exploding Kitten","BombCard.png");
Card catCard = new CatCard("Tacocat","CatCard.png");
Card defuseCard = new DefuseCard("Defuse","DefuseCard.png");
Card favourCard = new FavourCard("Favor","FavourCard.png");
Card futureCard = new FutureCard("See The Future","FutureCard.png");
Card nopeCard = new NopeCard("Nope","NopeCard.png");
Card shuffleCard = new ShuffleCard("Shuffle","ShuffleCard.png");
Card skipCard = new SkipCard("Skip","SkipCard.png");
output.println(attackCard.toString()+";"+bombCard.toString()+";"+catCard.toString()+";"+defuseCard.toString()+";"+favourCard.toString()+";"+futureCard.toString()+";"+nopeCard.toString()+";"+shuffleCard.toString()+";"+skipCard.toString());
output.flush();
input.close();
output.close();
}catch (IOException e) {e.printStackTrace();}
}
}
}