public class GoinServer {
static final int DEFAULT_PORT = 3135;
static ServerSocket serverSocket;
static Vector<Socket> connections;
static Vector<ClientProc> clients;
public static void sendAll(String s) {
if (connections != null) {
for (Enumeration e = connections.elements(); e.hasMoreElements();) {// send
// the
// msg
// to
// every
// client
// sockets
try {
PrintWriter pw = new PrintWriter(((Socket) e.nextElement())
.getOutputStream());
pw.println(s);
pw.flush();
} catch (IOException ex) {
}
}
}
System.out.println("sendAll:" + s);
}
public static boolean sendOne(String name, String msg) {
if (clients != null) {
for (Enumeration e = clients.elements(); e.hasMoreElements();) {
ClientProc cp = (ClientProc) e.nextElement();
if ((cp.getName()).equals(name)) {// send the msg to a special
// client socket.
try {
PrintWriter pw = new PrintWriter((cp.getSocket())
.getOutputStream());
pw.println(msg);
pw.flush();
return true;
} catch (IOException ioe) {
}
}
}
}
return false;
}
public static void addConnection(Socket s, ClientProc cp) {
if (connections == null) {
connections = new Vector<Socket>();
}
connections.addElement(s);
if (clients == null) {
clients = new Vector<ClientProc>();
}
clients.addElement(cp);
}
public static void deleteConnection(Socket s, ClientProc cp)
throws IOException {
if (connections != null) {
connections.removeElement(s);
s.close();
}
if (clients != null) {
clients.removeElement(cp);
}
}
public static Vector getClients() {
return clients;
}
public static void main(String[] arg) {
int port = DEFAULT_PORT;
try {
serverSocket = new ServerSocket(port);
System.out.println("Goin server has started!");
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
while (true) {
try {
Socket cs = serverSocket.accept();
ClientProc cp = new ClientProc(cs);// for every user, the
// server start a thread to
// communicate with he/she.
Thread ct = new Thread(cp);
ct.start();// the thread take a socket of one user start here.
addConnection(cs, cp);// make a communicate channel between
// user and server.
} catch (IOException e) {
System.err.println(e);
}
}
}
}
// ClientProc deal the data exchange logic between users and the server.
class ClientProc implements Runnable {
Socket s;
BufferedReader in;
PrintWriter out;
private String name = null;
private String sex = null;
// Date now;
public ClientProc(Socket s) throws IOException {
this.s = s;
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
this.updateList();
}
// Thread's run
public void run() {
while (name == null) {// the clientpro is a thread, when it begins,
// name=null.
try {
String inmsg;
inmsg = in.readLine();
// ChatServer.sendAll("Notic: "+inmsg+" has come!");
String[] userInfo;
userInfo = inmsg.split("&");
name = userInfo[0];
sex = userInfo[1];
GoinServer.sendAll("Notic: " + name + " has come!");
} catch (IOException ee) {
}
}
while (true) {// listening...
try {
String line = in.readLine();
if (line.equals("quit")) {
GoinServer.sendAll("Notic: " + this.name + " has quit!");
GoinServer.deleteConnection(s, this);
return;
}
else if (line.equals("refresh")) {
this.updateList();
}
else {// deliver the msg!
// now = new Date();
// String datestr =
// DateFormat.getTimeInstance().format(now);
// ChatServer.sendAll(datestr+" "+name + "\n " +line);
// name is the msg sender. inmsg[2] is the sender's aim
// buddy.
// inmsg[3] is the sender's words
if (line.startsWith("ToAll")) {
String[] inmsg = line.split("&");
GoinServer.sendAll("ToAll" + "&" + name + "&"
+ inmsg[1]);
} else if (line.startsWith("privateChat")) {
String[] inmsg = line.split("&");