-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
89 lines (87 loc) · 2.59 KB
/
Copy pathServer.java
File metadata and controls
89 lines (87 loc) · 2.59 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
package com.crumbdev.PluginServer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;
import java.util.ArrayList;
public class Server extends Thread
{
private static final int CRUMB_PORT = 30480;
private static final int CON_THROTTLE = 30;
private ServerSocket serverSocket;
private InetAddress hostAddress;
private Socket socket;
private ArrayList<Client> users = new ArrayList<Client>();
public Server()
{
// Attempt to get the host address
try
{
hostAddress = InetAddress.getLocalHost();
}
catch(UnknownHostException e)
{
System.out.println("Could not get the host address.");
return;
}
// Announce the host address
System.out.println("Server host address is: "+hostAddress);
// Attempt to create server socket
try
{
serverSocket = new ServerSocket(CRUMB_PORT,0,hostAddress);
}
catch(IOException e)
{
System.out.println("Could not open server socket.");
return;
}
// Announce the socket creation
System.out.println("Socket "+serverSocket+" created.");
}
/**
* Starts the client accepting process.
*/
public void run()
{
// Announce the starting of the process
System.out.println("Room has been started.");
// Enter the main loop
while(true)
{
// Remove all disconnected clients
for(int i = 0;i < users.size();i++)
{
// Check connection, remove on dead
if(!users.get(i).isConnected())
{
System.out.println(users.get(i)+" removed due to lack of connection.");
users.remove(i);
}
}
// Get a client trying to connect
try
{
socket = serverSocket.accept();
}
catch(IOException e)
{
System.out.println("Could not get a client.");
}
// Client has connected
System.out.println("Client "+socket+" has connected.");
// Add user to list
users.add(new Client(socket));
// Sleep
try
{
Thread.sleep(CON_THROTTLE);
}
catch(InterruptedException e)
{
System.out.println("Room has been interrupted.");
}
}
}
}