ÀÚ¹Ù Ç÷¡½Ã XML äÆÃ ¼¹ö
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class CommServer {
private Vector clients = new Vector(); // Á¢¼ÓÀÚ À̽ºÆ®
ServerSocket server; // ¼¹ö
/*
¼¹ö »ý¼ºÀÚ
¼¹ö¸¦ ½ÃÀÛ!
*/
public CommServer(int port) { //»ý¼ºÀÚ
startServer(port); //¼¹ö¸¦ ½ÃÀÛÇÑ´Ù. Æ÷Æ®¹øÈ£´Â ù¹øÂ° ÆÄ¶ó¸ÅÅÍ
}
/*
¼¹ö½º¸¦ ½ÃÀÛÇϰí Á¢¼ÓÀÚ¸¦ ±â´Ù¸°´å
*/
private void startServer(int port) { //¼¹ö½ÃÀÛ ¸Þ¼Òµå
writeActivity("Attempting to Start Server");
try {
//¼¹ö »ý¼º
server = new ServerSocket(port); //¼ÒÄÏ »ý¼º
writeActivity("Server Started on Port: " + port);
//¼¹ö°¡ µ¹¾Æ°¡´Â µ¿¾È
while(true) {
//Ŭ¶óÀÌ¾ðÆ®¸¦ ±â´Ù¸°´Ù.
Socket socket = server.accept();
CSClient client = new CSClient(this, socket); //À¯Àú°¡ Á¢¼ÓÇÏ¸é ¾²·¹µå »ý¼º(Ŭ¶óÀ̾ðÆ®)
writeActivity(client.getIP() + " connected to the server.");
//´Ù¸¥ ³ðµéÀÇ À¯Àú¼ö¸¦ ¾Ë¾Æ¿Â´Ù.
Enumeration enum = clients.elements();
while (enum.hasMoreElements()) { //¸ðµç Ŭ¶óÀ̾ðÆ®ÀÇ Ä³¸¯ÅÍ,À§Ä¡,Á¾·ù¸¦ ¹Þ¾Æ¿Â´å
CSClient tclient = (CSClient)enum.nextElement();
String msg = "<ADD><USER>"+tclient.getUname()+
"</USER><TYPE>"+tclient.getUtype()+
"</TYPE><POS_X>"+tclient.getUx()+
"</POS_X><POS_Y>"+tclient.getUy()+
"</POS_Y><NIC>"+tclient.getUnic()+
"</NIC></ADD>"; // XML¹®¼ »ý¼º
msg += '\0';//XMLÀÇ ³¡À» ¾Ë¸°´Ù.
//System.out.println(msg);
client.send(msg); //ÇöÀç Á¢¼ÓÇÑ ³ðÇÑÅ× ¸Þ¼¼Áö¸¦ º¸³½´Ù.
}
//¹æ±Ý Á¢¼ÓÇѳ𿡰Ô
clients.addElement(client); //À¯Àú¸®½ºÆ®¿¡ Ãß°¡
//¾²·¹µå
client.start(); //¾²·¹µå ½ÃÀÛ
//´Ù¸¥ ³à¼®µé¿¡°Ô À¯Àú¼ö¸¦ ¾Ë·ÁÁØ´Ù(¸î¸í Á¢¼ÓÁßÀÎÁö)
broadcastMessage("<NUMCLIENTS>" + clients.size()
+ "</NUMCLIENTS>");
}
} catch(IOException ioe) {
writeActivity("Server Error...Stopping Server");
// kill this server
System.out.println("err"+ioe);
killServer();
}
}
/*
¸ðµÎ¿¡°Ô ¸Þ¼¼Áö º¸³»´Â ºÎºÐ.
*/
public synchronized void broadcastMessage(String message) { //Á¢¼ÓµÈ ³à¼®µé¿¡°Ô ¸Þ¼¼Áö¸¦ º¸³½´Ù.
message += '\0'; //¸Þ¼¼ÁöÀÇ ³¡À» ¾Ë¸°´Ù
Enumeration enum = clients.elements();
while (enum.hasMoreElements()) { //¸ðµç Ŭ¶óÀÌ¾ðÆ®¿¡°Ô..
CSClient client = (CSClient)enum.nextElement();
client.send(message); //¸Þ¼¼Áö¸¦ º¸³½´Ù.
}
}
/*
Ŭ¶óÀÌ¾ðÆ® ¸®½ºÆ®¿¡¼ Ŭ¶óÀÌ ¾ðÆ®¸¦ Á¦°Å
*/
public void removeClient(CSClient client) {
writeActivity(client.getIP() + " has left the server.");
String msg = "<REMOVE><USER>"+client.getUname()+"</USER></REMOVE>"; // XML¹®¼ »ý¼º
msg += '\0';//XMLÀÇ ³¡À» ¾Ë¸°´Ù.
//Ŭ¶óÀÌ¾ðÆ® ¸®½ºÆ®¿¡¼ Á¦°Å
clients.removeElement(client);
//Ŭ¶óÀÌ¾ðÆ® ¸ðµâ¿¡¼ Äɸ¯ÅÍ Á¦°Å
broadcastMessage(msg);
//Ŭ¶óÀÌ¾ðÆ® ¼ýÀÚ¸¦ ´Ù½Ã ¾Ë·ÁÁØ´Ù.
broadcastMessage("<NUMCLIENTS>" + clients.size() + "</NUMCLIENTS>");
}
/*
¸Þ¼¼Áö Ãâ·Â Æû
*/
public void writeActivity(String activity) {
// --- get the current date and time
Calendar cal = Calendar.getInstance();
activity = "[" + cal.get(Calendar.MONTH)
+ "/" + cal.get(Calendar.DAY_OF_MONTH)
+ "/" + cal.get(Calendar.YEAR)
+ " "
+ cal.get(Calendar.HOUR_OF_DAY)
+ ":" + cal.get(Calendar.MINUTE)
+ ":" + cal.get(Calendar.SECOND)
+ "] " + activity + "\n";
// --- display the activity
System.out.print(activity);
}
/*
¼¹ö¸¦ ¸ØÃç
*/
private void killServer() {
try {
//¸ØÃá´Ù
server.close();
writeActivity("Server Stopped");
} catch (IOException ioe) {
writeActivity("Error while stopping Server");
}
}
public static void main(String args[]) {
//Æ÷Æ® ¾Æ±Ô¸ÕÆ® ý
if(args.length == 1) {
CommServer myCS = new CommServer(Integer.parseInt(args[0]));
} else {
//Ʋ¸®¸é ¹æ¹ýÀ» °¥ÃÄÁØ´Ù
System.out.println("Usage: java CommServer [port]");
}
}
}
¿ø¼Ò½º´Â ¹«Å©²¨ÀÔ´Ï´Ù.
°Å±â ¼Ò½º´Â ´Ü¼øÈ÷ Ç÷¡½Ã¿¡¼ ¿À´Â ¹®ÀÚ¿À» ºê·Îµå ij½ºÆ®¸¸ ÇØÁÝ´Ï´Ù.
¿©±â¿¡ XMLÆÄ¼¸¦ Ãß°¡Çؼ Ç÷¡½Ã¿¡¼ ƯÁ¤ÇÑ Á¤º¸¸¦ º¸³¾ ¼ö ÀÖµµ·Ï ÇßÁö¿ä.