-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
100 lines (82 loc) · 2.08 KB
/
Server.java
File metadata and controls
100 lines (82 loc) · 2.08 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
package heaven.nchat.server;
import java.net.*;
//import java.security.*;
import java.util.ArrayList;
public class Server {
static ServerSocket serverSocket;
public static ArrayList<ClientThread> clientThreads = new ArrayList<ClientThread>();
public static String motd = "§1"+"\0"+"1"+"\0"+"0.5.0"+"\0"+"nchat test server"+"\0"+"0"+"\0"+"0";
public static String welcome = "Welcome on ncraft test server";
public static ArrayList<String> clientList = new ArrayList<String>();
//
/**
* @param args
*/
public static String[] listUsers()
{
ArrayList<String> users = new ArrayList<String>();
for (ClientThread cThread : Server.clientThreads)
{
if (cThread.clientSocket.isClosed())
{
Server.clientThreads.remove(cThread);
}
users.add(cThread.username);
}
return (String[]) users.toArray();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
serverSocket = new ServerSocket(14668);
while (true)
{
Socket client = serverSocket.accept();
ClientThread cThread;
clientThreads.add(cThread = new ClientThread(client));
cThread.start();
}
}
catch (Exception e) {
System.out.println("Could not listen on port: 14668");
System.exit(-1);
}
}
public static void broadcastMessage(String msg)
{
broadcastMessage(msg, (byte)0x04);
}
public static void broadcastMessage(String msg, byte type)
{
if (type==0x04)
{
System.out.println(msg);
}
for (ClientThread cThread : clientThreads)
{
if (cThread.noErrors == true)
{
try
{
if (cThread.clientSocket.isClosed())
{
clientThreads.remove(cThread);
}
boolean result = cThread.sendMessage(msg, type);
if (result == false)
{
clientThreads.remove(cThread);
}
} catch (Exception e)
{
try
{
clientThreads.remove(cThread);
} catch (Exception e2)
{
}
}
}
}
}
}