µ¶ÀÏ »çÀÌÆ®¿¡¼­ ÆÛ¿Â °£´ÜÇÑ °Í..


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

±Û¾´ÀÌ :ÀÌÁö¿¬ 2000³â 5¿ù 18ÀÏ 08:35:03

In Reply to: äÆà ÇÁ·Î±×·¥ ÀÚ¹Ù ¼Ò½º Á» ¾Ë·Á ÁÖ¼¼¿ä! posted by ¸ô¶ó¿ä on 2000³â 5¿ù 17ÀÏ 18:00:59:

/*
Vergessen Sie nicht, die einzelnen Klassen auch in eigenen Dateien mit entsprechenden Namen zu speichern, also die chatserver-Klasse in der Datei "chatserver.java", die connection-Klasse in der Datei "connection.java" und die chatapplet-Klasse in der Datei "chatapplet.java".
*/



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


public class chatserver implements Runnable
{
public static final int PORT = 8765;
protected ServerSocket listen;
protected Vector connections;
Thread connect;


public chatserver()
{
try
{
listen = new ServerSocket(PORT);
} catch (IOException e)
{
System.err.println("Fehler beim Erzeugen der Sockets:"+e);
System.exit(1);
}


connections = new Vector();


connect = new Thread(this);
connect.start();
}


public void run()
{
try
{
while(true)
{
Socket client=listen.accept();


connection c = new connection(this, client);
connections.addElement(c);
}
} catch (IOException e)
{
System.err.println("Fehler beim Warten auf Verbindungen:"+e);
System.exit(1);
}
}


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


public void broadcast(String msg)
{
int i;
connection you;


for (i=0; i<connections.size(); i++)
{
you = (connection) connections.elementAt(i);
you.out.println(msg);
}
}
}



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


class connection extends Thread
{
protected Socket client;
protected DataInputStream in;
protected PrintStream out;
protected chatserver server;


public connection(chatserver server, Socket client)
{
this.server=server;
this.client=client;


try
{
in = new DataInputStream(client.getInputStream());
out = new PrintStream(client.getOutputStream());
} catch (IOException e)
{
try { client.close(); } catch (IOException e2) {} ;
System.err.println("Fehler beim Erzeugen der Streams: " + e);
return;
}


this.start();
}



public void run()
{
String line;


try
{
while(true)
{
line=in.readLine();
if(line!=null)
server.broadcast(line);
}
} catch (IOException e)
{
System.out.println("Fehler:" + e);
}
}
}



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


public class chatapplet extends Applet implements Runnable
{
public static final int PORT = 8765;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;


public void init()
{
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);


this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);


this.setBackground(Color.lightGray);
this.setForeground(Color.black);
inputfield.setBackground(Color.white);
outputarea.setBackground(Color.white);
}


public void start()
{
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Verbindung zum Server fehlgeschlagen!");
System.exit(1);
}


say("Verbindung zum Server aufgenommen...");


if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}



public void stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}


if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}



public void run()
{
String line;


try
{
while(true)
{
line = in.readLine();
if(line!=null)
outputarea.appendText(line+'\n' );
}
} catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
}



public boolean action(Event e, Object what)
{
if (e.target==inputfield)
{
String inp=(String) e.arg;


out.println(inp);
inputfield.setText("");
return true;
}


return false;
}



public void say(String msg)
{
outputarea.appendText("*** "+msg+" ***\n");
}
}



Àû´çÈ÷ ²÷¾î¼­ ÄÄÆÄÀÏÇÏ½Ã°í µ¹¸®¸é Àß µ½´Ï´Ù.


¹°·Ð ¼­¹ö¿¡¼­ chatserver ¾ÖÇø®ÄÉÀ̼ÇÀÌ µ¹°í ÀÖ¾î¾ß °ÚÁÒ..


¾Æ½ÃÁÒ?? ÀϹÝÀûÀÎ °æ¿ì ¾ÖÇø´Àº ÀÚ½ÅÀÌ ¿Â ¼­¹ö¿Ü¿¡´Â ¼ÒÄÏÀ» ¿­¼ö ¾ø´Â´Ù´Â °Í..


Áï chatserver°¡ µ¹°í ÀÖ´Â ¼­¹ö¿¡ chatapplet.class°¡ ÀÖ¾î¾ßÇØ¿ä..


good luck~





´ÙÀ½ ±Ûµé:



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

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


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