컴퓨터/JAVA

[Java]chatting server & client.

Hikasiru 2006. 3. 24. 23:55
원문: http://www.javaworld.com/javaworld/jw-01-1997/jw-01-chat_p.html

한글문서: http://www.javastudy.co.kr/docs/jm/jm8.html

ChatServer.java
import java.net.*; 
import java.io.*; 
import java.util.*; 
 
public class ChatServer { 
public ChatServer (int port) throws IOException { 
   ServerSocket server = new ServerSocket (port); 
   while (true) { 
     Socket client = server.accept (); 
     System.out.println ("Accepted from " + client.getInetAddress ()); 
     ChatHandler c = new ChatHandler (client); 
     c.start (); 
   } 

 
public static void main (String args[]) throws IOException { 
     int port=0;
   if (args.length != 1) 
       port = 9830;
   else
       port = Integer.parseInt(args[0]);
   new ChatServer (port); 

}  


ChatHandler.java
import java.net.*;
import java.io.*;
import java.util.*;

public class ChatHandler extends Thread {
   protected Socket s;
   protected DataInputStream i;
   protected DataOutputStream o;

   public ChatHandler (Socket s) throws IOException {
       this.s = s;
       i = new DataInputStream (new BufferedInputStream (s.getInputStream()));
       o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream()));
   }

   protected static Vector handlers = new Vector ();

   public void run () {
       String name = s.getInetAddress ().toString ();
       try {
           broadcast (name + " has joined. ");
           handlers.addElement(this);
           while(true) {
               String msg = i.readUTF();
               broadcast (name + " - " + msg);
           }
       } catch (IOException ex) {
           ex.printStackTrace();
       } finally {
           handlers.removeElement(this);
           broadcast(name + " has left.");
           try  {
               s.close();
           } catch (IOException ex ) {
               ex.printStackTrace();
           }
       }
   }


   protected static void broadcast (String message) {
       synchronized (handlers) {
           Enumeration e = handlers.elements ();
           while (e.hasMoreElements ()) {
               ChatHandler c = (ChatHandler) e.nextElement ();
               try {
                   synchronized (c.o) {
                       c.o.writeUTF(message);
                   }
                   c.o.flush();
               } catch (IOException ex) {
                   c.stop();
               }
           }
       }
   }
}


ChatApplet.java
import java.net.*; 
import java.io.*; 
import java.awt.*; 
import java.applet.*; 
 
// Applet parameters: 
//   host = host name 
//   port = host port 
 
public class ChatApplet extends Applet implements Runnable { 
protected DataInputStream i; 
protected DataOutputStream o; 
 
protected TextArea output; 
protected TextField input; 
 
protected Thread listener; 
 
public void init () { 
   setLayout (new BorderLayout ()); 
   add ("Center", output = new TextArea ()); 
   output.setEditable (false); 
   add ("South", input = new TextField ()); 
   input.setEditable (false); 

 
public void start () { 
   listener = new Thread (this); 
   listener.start (); 

 
public void stop () { 
   if (listener != null) 
     listener.stop (); 
   listener = null; 

 
public void run () { 
   try { 
     String host = getParameter ("host"); 
     if (host == null) 
   host = getCodeBase ().getHost (); 
     String port = getParameter ("port"); 
     if (port == null) 
   port = "9830"; 
     output.appendText ("Connecting to " + host + ":" + port + "..."); 
     Socket s = new Socket (host, Integer.parseInt (port)); 
     i = new DataInputStream (new BufferedInputStream (s.getInputStream ())); 
     o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ())); 
     output.appendText (" connected.\n"); 
     input.setEditable (true); 
     input.requestFocus (); 
     execute (); 
   } catch (IOException ex) { 
     ByteArrayOutputStream out = new ByteArrayOutputStream (); 
     ex.printStackTrace (new PrintStream (out)); 
     output.appendText ("\n" + out); 
   } 

 
public void execute () { 
   try { 
     while (true) { 
       String line = i.readUTF (); 
       output.appendText (line + "\n"); 
     } 
   } catch (IOException ex) { 
     ByteArrayOutputStream out = new ByteArrayOutputStream (); 
     ex.printStackTrace (new PrintStream (out)); 
     output.appendText (out.toString ()); 
   } finally { 
     listener = null; 
     input.hide (); 
     validate (); 
     try { 
       o.close (); 
     } catch (IOException ex) { 
       ex.printStackTrace (); 
     } 
   } 

 
public boolean handleEvent (Event e) { 
   if ((e.target == input) && (e.id == Event.ACTION_EVENT)) { 
     try { 
       o.writeUTF ((String) e.arg); 
       o.flush (); 
     } catch (IOException ex) { 
       ex.printStackTrace(); 
       listener.stop (); 
     } 
     input.setText (""); 
     return true; 
   } else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) { 
     if (listener != null) 
       listener.stop (); 
     hide (); 
     return true; 
   } 
   return super.handleEvent (e); 




ChatApplet.html
<HTML>
<HEAD><TITLE></TITLE></HEAD>

<BODY>
<APPLET code=ChatApplet.class width=500 height=500></APPLET>
</BODY>

</HTML>