À±°æ±¸¾¾ÀÇ ¿¹Á¦ÀÔ´Ï´Ù.


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]

±Û¾´ÀÌ :À±´öÁØ 1999³â 7¿ù 15ÀÏ 00:44:30

In Reply to: [´äº¯]Multi-Chatting ÇÁ·Î±×·¥ÀÇ µ¿ÀÛ posted by Á¶½ÅÁ¦ on 1999³â 7¿ù 13ÀÏ 17:37:31:

Âü°í Çϼ¼¿ä.
ÀÌ°Å ¿Ã·Áµµ µÇ·Á³ª ¸ð¸£°Ú³×¿ä... ¸¶¼Ò.Jr 4¿ùÈ£¿¡ ½Ç·È´ø ¿¹Á¦ÀÔ´Ï´Ù.



[ ¼­¹öÃø ¼Ò½º ]



/**
* BroadcastServer.java
* Copyright (c) 1998 Yoon Kyung Koo. All rights reserved.
*
* First release date 1998/10/07
*
* @version 1.1 1999/03/08
* @author <A HREF="mailto:yoonforh@nuri.net">Yoon Kyung Koo</A>
*/


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


class BroadcastServer
{
final static int PORT = 8001;
ServerSocket server = null;
Vector clients = new Vector();


// inner class
class Client extends Thread {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;


// constructor
Client (Socket socket) throws IOException {
this.socket = socket;
this.in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
this.out =
new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()),
true /* auto flush */);
start(); // run this client thread
}


public void run() {
try {
while (true) {
// note that readLine() is a blocking operation
String line = in.readLine();
if (line == null) { // null means End Of Stream
close();
break;
}
System.out.println(getName()+':'+line);
BroadcastServer.this.broadcast(line);
}
} catch (IOException ie) {
System.err.println(getName()+":IOException. "+ie.getMessage());
close();
}
}


public void close() {
synchronized (BroadcastServer.this) {
try { if (in!=null) in.close(); } catch (Exception e) {}
if (out!=null) out.close();
try { if (socket!=null) socket.close(); } catch (Exception e) {}
socket=null;
}
}
}


// constructor
public BroadcastServer() {
// first, create server socket
try {
server = new ServerSocket(PORT);
} catch (IOException ie) {
System.err.println("Cannot create server socket");
System.exit(1);
}


while (true) {
try {
Socket socket = server.accept();
// add new client to client list
Client client = new Client(socket);
synchronized (this) {
clients.addElement(client);
}
} catch (IOException ie) {
System.err.println("IOException :"+ie.getMessage());
}
}
}


public static void main(String args[]) {
new BroadcastServer();
}


private synchronized void broadcast(String message) {
for (Enumeration enum=clients.elements();
enum.hasMoreElements(); )
{
Client client = (Client) enum.nextElement();
if (client == null)
continue;
if (client.socket == null) { // if closed socket
clients.removeElement(client);
continue;
}
client.out.println(message);
if (client.out.checkError()) { // check error
System.err.println("Error while sending message.");
client.close();
}
}
}
}



[ ¾ÖÇø´ ¼Ò½º]



/**
* BroadcastApplet.java
* Copyright (c) 1998 Yoon Kyung Koo. All rights reserved.
*
* First release date 1998/10/07
*
* @version 1.0b 1999/03/12
* @author <A HREF="mailto:yoonforh@nuri.net">Yoon Kyung Koo</A>
*/


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;


public class BroadcastApplet extends Applet
implements ActionListener
{
int port;
String host;
TextField inputField;
TextArea outputArea;
Socket socket;
BufferedReader in;
PrintWriter out;
Thread readingThread;


public void init() {
inputField = new TextField();
outputArea = new TextArea();


setLayout(new BorderLayout());


add(inputField, "North");
add(outputArea, "Center");


String param=getParameter("PORT");
if (param==null)
port = 8001; // default port
else
port = Integer.parseInt(param);
host=getParameter("HOST");
if (host==null) // default host is applet's host
host = getCodeBase().getHost();


inputField.addActionListener(this);


doLayout();
}


public void start() {
if (socket == null)
connect();


readingThread = new Thread() {
public void run() {
if (socket == null)
return;
while (socket!=null &&
readingThread==Thread.currentThread()) {
try {
String line = in.readLine();
appendLine(line);
} catch (IOException ie) {
appendLine("IOException:"+ie.getMessage());
closeSocket();
}
}
}
};


readingThread.start();
}


public void stop() {
readingThread=null;
closeSocket();
}


public void actionPerformed(ActionEvent ae) {
if (ae.getSource() instanceof TextField) {
String line = inputField.getText();
inputField.setText(""); // clear text
if (socket == null)
return;
try {
out.println(line);
if (out.checkError())
throw new IOException("Print error.");
} catch (IOException ie) {
closeSocket();
}
}
}


public void connect() {
try {
socket = new Socket(host, port);
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()),
true /* auto flush */);
appendLine("Connected...");
} catch (UnknownHostException uhe) {
appendLine("Unknown host:"+uhe.getMessage());
} catch (IOException ie) {
appendLine("IOException:"+ie.getMessage());
}
}


public synchronized void closeSocket() {
try { if (in != null) in.close(); } catch (Exception e) {}
if (out != null) out.close();
try { if (socket != null) socket.close(); } catch (Exception e) {}
socket = null;
}


public synchronized void appendLine(String message) {
outputArea.append(message+"\n");
}


}



´ÙÀ½ ±Ûµé:



À̾ ±Û¿Ã¸®±â(´äÇϱâ)

À̸§:
E-Mail:
Á¦¸ñ:
³»¿ë:
HTML ÅÂ±× Æ÷ÇÔ ¿©ºÎ: HTML ¹®¼­ÀÏ °æ¿ì üũ
°ü·Ã URL(¼±ÅÃ):
URL Á¦¸ñ(¼±ÅÃ):
°ü·Ã À̹ÌÁö URL:


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]