|
SSISO Community검색 |
|
SSISO Community메뉴 |
|
SSISO Community카페 |
|
블로그 카테고리 |
|
|
Synchronized로 검색한 결과 |
|
등록일:2008-03-06 12:54:22 작성자: 제목:자바네트워크 3 |
|
예: 자바 채팅 프로그램
다음 채팅 프로그램은 "Java Network programming"의 저자인 Hughes가 자바월드에 기고한
프로그램 소스이다. 프로그램을 너무 간단하고도 잘 작성했기 때문에 저자의 허락없이 인용
한다. 프로그램의 저작권은 hughes에 있슴을 밝혀둔다.(국내에서는 출처만 밝히면 인용해도
저작권법에 저촉되지 않는 것으로 알고 있다. -_-;;;)
서버 실행시키기
% java ChatServer 9830 &
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 {
if (args.length != 1)
throw new RuntimeException ("Syntax: ChatServer ");
new ChatServer (Integer.parseInt (args[0]));
}
}
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);
}
}
|
|
|
|
|
|