±Û¾´ÀÌ :ºñ¾È 1999³â 7¿ù 13ÀÏ 14:10:17
Applet¿¡¼ Thread¸¦ ±¸ÇöÇÒ¶§ run() ÇÔ¼ö¸¦ ¾ðÁ¦ ºÒ·¯ ÁÖ¾î¾ß Çϳª¿ä?
AppletÀ¸·Î Chtting programÀ» ÀÛ¼ºÇߴµ¥ ¼¹ö¿¡¼ º¸³»ÁÖ´Â ¸Þ½ÃÁö¸¦ ¹Þ¾Æ¼ ȸ鿡 »Ñ·ÁÁÖ´Â ºÎºÐÀº run() ¸Þ¼Òµå ºÎºÐ¿¡ ³Ö¾î ÁÖ¾ú´Âµ¥ run() ¸Þ½îµå°¡ ºÒ·ÁÁöÁö ¾Ê³×¿ä.
init() ¸Þ¼ÒµåºÎºÐ¿¡´Â chatting client ȸéÀ» ±¸¼ºÇØÁÖ°í
client¿¡¼ ¸Þ½ÃÁö¸¦ Àû¾î¼ ¼¹ö·Î º¸³»´Â ¹öÆ°À» ´©¸£´Â À̺¥Æ®°¡ ¹ß»ýÇÏ¸é ¸Þ½ÃÁö¸¦ ¼¹ö¿¡ º¸³»ÁÝ´Ï´Ù.
ÀÌ º¸³»ÁØ ¸Þ½îµå¸¦ ¼¹ö¿¡¼ °¢ clientÀÌ°Ô º¸³»ÁÖ°í clientÀÇ run() ¸Þ¼Òµå¿¡¼ ¸Þ½ÃÁö¸¦ ¹ÞÀ» Áغñ¸¦ ÇÏ°í ÀÖ´Ù°¡ ¼¹ö¿¡¼ ¸Þ½ÃÁö°¡ ¿À¸é Ŭ¶óÀ̾ðÅ× Ã¢¿¡ º¸¿©ÁÖµµ·Ï µÇ¾îÀÖ½À´Ï´Ù.
±×·±µ¥ run() ÇÔ¼ö°¡ ºÒ·ÁÁöÁú ¾Ê´Â±º¿ä..
¾î´À ºÎºÐ¿¡¼ run() ÇÔ¼ö¸¦ ºÒ·¯¾ß run() ÇÔ¼ö°¡ Á¦´ë·Î ºÒ¸®°í Àß ½ÇÇàÀÌ µÉ±î¿ä?
¼¹ö¿¡ ¸Þ½ÃÁö¸¦ º¸³»´Â À̺¥Æ®°¡ ¹ß»ýÇÒ¶§ ¼¹ö·ÎºÎÅÍ ¸Þ½ÃÁö¸¦ ¹ÞÀ¸´Ï±î ³»°¡ º¸³½ ¸Þ½ÃÁö¸¸À» µÇµ¹·Á ¹Þ´õ±º¿ä..
´Ù¸¥»ç¶÷ÀÌ º¸³½ ¸Þ½ÃÁö´Â ¹ÞÁö ¸øÇϱ¸¿ä..
±×¸®°í init() ÇÔ¼öºÎºÐ¿¡¼µµ ºÒ·¯ ºÃ´Âµ¥ ºÒ·Á ÁöÁö ¾Ê±¸¿ä..
Á¦ ¼Ò½º¸¦ °°ÀÌ ¿Ã¸³´Ï´Ù. ÁöÀúºÐ ÇÏÁö¸¸ ¼öÁ¤À» Á» ÇØÁÖ¼ÌÀ¸¸é ÁÁ°Ú±º¿ä..
public class SocketClientApplet extends Applet implements Runnable, ActionListener
{
DataInputStream i;
DataOutputStream o;
TextField t1,t2;
TextArea ta;
TextField tf;
Button b1, b2;
Socket s;
protected Thread listener;
public void init()
{
setLayout(new BorderLayout());
Panel p1 = new Panel();
t1 = new TextField(20);
t2 = new TextField(10);
b1 = new Button("Á¢ ¼Ó");
b1.addActionListener(this);
t1.requestFocus();
p1.add(new Label("Server IP"));
p1.add(t1);
p1.add(new Label("Port "));
p1.add(t2);
p1.add(b1);
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
ta = new TextArea(20,20);
ta.setText("");
tf = new TextField(30);
tf.requestFocus();
b2 = new Button("send");
b2.addActionListener(this);
Panel p3 = new Panel();
ta.setEditable(false);
p2.add("Center",ta);
p3.add(tf);
p3.add(b2);
p2.add("South",p3);
add("North",p1);
add("Center",p2);
listener = new Thread(this);
listener.start();
}
public void run()
{
try
{
while(true)
{
String s = i.readUTF();
ta.append(s + "\n");
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
String name = e.getActionCommand();
try
{
if(name.equals("Á¢ ¼Ó"))
{
System.out.println("¼¹ö¿¡ Á¢¼ÓÇÕ´Ï´Ù.");
s = new Socket(t1.getText(), Integer.parseInt(t2.getText()));
i = new DataInputStream(new BufferedInputStream(s.getInputStream()));
o = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
}
if(name.equals("send"))
{
System.out.println("TextFieldÀÇ String..." + tf.getText());
o.writeUTF(tf.getText());
o.flush();
tf.setText("");
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
¿¡·¯´Â ¹ß»ýÇÏÁö ¾Ê°í run() ÇÔ¼ö°¡ ºÒ·Á ÁöÁö ¾Ê´Â±º¿ä..
±×·¡¼ ¿©·¯ »ç¿ëÀÚ°¡ °°ÀÌ Åë½ÅÀ» ÇÒ ¼ö ¾ø°í °¢ client¿Í ¼¹ö¿Í 1:1 Åë½Å¸¸ÀÌ °¡´ÉÇØ Áö´Â±º¿ä..
run() ÇÔ¼ö¸¦ ¾îµð¿¡¼ ½ÇÇà½ÃÄÑ¾ß ÇÏ´ÂÁö ¼Ò½º¿¡¼À߸øµÈ ºÎºÐÀÌ ¾î´À ºÎºÐÀÎÁö ²À Á» °¡¸£ÃÄ ÁÖ¼¼¿ä..
´äº¯ ºÎŹµå¸³´Ï´Ù.