-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
53 lines (45 loc) · 1.49 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
package CarRace3D;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;
import javafx.application.Platform;
public class Server implements Runnable, CarRaceConstants{
private ServerSocket serverSocket;
private static Socket socketRace1, socketRace2, socketRace3;
public Server() {
new Thread( () -> {
try {
// Create a server socket
serverSocket = new ServerSocket(8000);
// Ready to create a session for every two players
socketRace1 = serverSocket.accept();
new DataOutputStream(socketRace1.getOutputStream()).writeInt(CONNECT_TO_SERVER);
socketRace2 = serverSocket.accept();
new DataOutputStream(socketRace2.getOutputStream()).writeInt(CONNECT_TO_SERVER);
socketRace3 = serverSocket.accept();
new DataOutputStream(socketRace3.getOutputStream()).writeInt(CONNECT_TO_SERVER);
// Launch a new thread for this session of two players
//Runnable hr = new HandleRaces(socketRace1, socketRace2, socketRace3);
//new Thread(hr).start();
} catch(SocketException ex) {
}
catch(IOException ex) {
}
}).start();
}
@Override
public void run() {
new Server();
}
public ArrayList<Socket> getSockets() {
ArrayList<Socket> sockets = new ArrayList<>();
sockets.add(socketRace1);
sockets.add(socketRace2);
sockets.add(socketRace3);
return sockets;
}
}