Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


Servlet°ú Applet°£ÀÇ TCP/IP Åë½Å
 
Applet°ú Servlet°£ÀÇ TCP/IP¸¦ ÀÌ¿ëÇÏ¿© ¼­·Î Åë½ÅÇÏ´Â ¿¹¸¦ ChattingÀ» ÀÌ¿ëÇÏ¿© ±¸ÇöÇغ¸ÀÚ ( 2003/04/01 ) 502
Written by ienvyou - ÃÖÁö¿õ
2 of 2
 

ChatServlet.java ¿¡¼­´Â ¼ø¼öÇÏ°Ô Client ¿¡¼­ Request¸¸À» ´ã´çÇÏ°í 

ChatThread.java ¿¡¼­´Â Service¸¦ ´ã´çÇÏ´Â code°¡ µé¾î°£´Ù

¿©±â¼­ Server SocketÀ» ÀÏ´øÇÏ°Ô OpenÇÏ°í ±â´Ù¸°´Ù

¢º AppletÄÚµåÀÇ ÀÛ¼º

ChatClient.java 


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class ChatClient extends Applet implements Runnable, ActionListener {
  Label lbl1 = new Label();
  TextField txtName = new TextField("", 10);
  Button btnConn = new Button();
  TextArea txtReceive = new TextArea("");
  TextField txtSend = new TextField("");

  Socket socket;
  BufferedReader din;
  PrintWriter dout;
  boolean fConnecting = false;
  Thread listener;
  String name="";
  Label label=new Label("Á¢¼ÓÀÚ");
  TextField count=new TextField("",2);

  public ChatClient() {
  }

  public void init() {
 
    makeUi();
  	
  }
  public void makeUi(){
    setLayout(new BorderLayout());
  	
  	Panel p = new Panel();
  	lbl1.setText("À̸§");
    btnConn.setLabel("¿¬°á");
    count.setEditable(false); 
    btnConn.addActionListener(this);
    p.add(lbl1);
    p.add(txtName);
    p.add(btnConn);
	p.add(label);
	p.add(count);
    
    txtReceive.setEditable(false);
	txtReceive.setBackground(Color.white);
	txtReceive.setForeground(Color.blue);
    txtSend.addActionListener(this);

    add(p, BorderLayout.NORTH);
    add(txtReceive, BorderLayout.CENTER);
    add(txtSend, BorderLayout.SOUTH);

  }

  public void actionPerformed(ActionEvent e) {
  	Object s = e.getSource();
 	
    if(s == btnConn) btnConn_actionPerformed(e);
    else if(s == txtSend) txtSend_actionPerformed(e);
  }

  void btnConn_actionPerformed(ActionEvent e) {
    try {
      if(fConnecting) {
        btnConn.setLabel("¿¬°á");
        txtName.setEditable(true);
        txtSend.setEditable(false);
        txtName.requestFocus();
        fConnecting = false;
        listener.destroy();
        din.close();
        dout.close();
        socket.close();
      } else {
        if(txtName.getText().equals("")) {
          return;
        }
		name=txtName.getText();
        btnConn.setLabel("Á¾·á");
        txtName.setEditable(false);
        txtSend.setEditable(true);
        txtSend.requestFocus();
        fConnecting = true;
        socket = new Socket("localhost", 7777);
        din = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        dout = new PrintWriter(socket.getOutputStream(), true);
	    listener = new Thread(this);
        listener.start();
      }
    } catch(IOException ioe) {}
  }

  void txtSend_actionPerformed(ActionEvent e) {
      dout.println("["+name+"]"+txtSend.getText());
      txtSend.setText("");
  }

  public void run() {
  	while(true) {
    	try {
      		String s = din.readLine();
			StringTokenizer st=new StringTokenizer(s);
			String msg=st.nextToken("|");
			String size=st.nextToken();
      		txtReceive.append(msg + "\n");
			count.setText(size);
    	} catch(IOException ioe) {}
  	}
  }
}
		
¿©±â¼­´Â Client SocketÀ» ¸¸µé¾î¼­ Server¿ÍÀÇ Á¢¼ÓÀ» ÇÏ°Ô µÈ´Ù. ´ÙÀ½ Server·Î ºÎÅÍ ¹ÞÀº °á°ú¸¦ È­¸é¿¡ Ãâ·Â ÇÑ´Ù. * ½ÇÇà ¹æ¹ý 1. ServletÀ» ServerÀÇ Servlet À§Ä¡ÀÌ Á¸ÀçÇÏ´Â À§Ä¡¿¡ ¿Ã·Á ³õ´Â´Ù. 2. AppletÀÌ Á¸ÀçÇÏ´Â À§Ä¡¿¡ ´ëÇØ codebase¸¦ ÁöÁ¤ÇÑ´Ù 3. AppletÀº DocÁï web Service dir¿¡ À§Ä¡ ½ÃŲ´Ù. 4. client ¿¡¼­ browser¿¡¼­ ServletÀ» È£ÃâÇÑ´Ù. °£´ÜÇÏ°Ô Applet°ú Servlet°úÀÇ TCP/IP Åë½Å¿¡ ´ëÇØ »ìÆ캸¾Ò´Ù. ÀÌ ¿¹¿¡¼­´Â ServerÂÊÀÇ Code°¡ »ó´çÈ÷ ÁÙ¾î µé¸é¼­ °ü¸®°¡ ÆíÇØÁø´Ù. ÇÏÁö¸¸ ÀÌ·¯ÇÑ ¿¹¿¡¼­ÀÇ ¹®Á¦Á¡Àº ServerÂÊ¿¡¼­´Â 80 Port Áï HTTP Port°¡ ¾Æ´Ñ ´Ù¸¥ ÀÏÁ¤ÇÑ Server Socket Port¸¦ ¿­¾î ³õ¾Æ¾ß ÇÑ´Ù´Â °ÍÀÌ °¡Àå ¹®Á¦ÀÌ´Ù. ¶ÇÇÑ Server ¿Í Client °£¿¡ TCP/IP·Î ÀÏÁ¤ÇÏ°Ô °è¼Ó Á¢¼ÓÇÏ°í ÀÖ´Ù´Â °Íµµ ServerÂÊ¿¡ ¹«¸®¸¦ ÁÖ´Â ÇϳªÀÇ ¿¹°¡ µÉ¼ö ÀÖ´Ù´Â °ÍÀÌ´Ù. ÀÌ·± ChattingÀÌ ¾Æ´Ñ ´Ù¸¥ Applet°ú Servlet°úÀÇ Åë½ÅÀº ´ÜÁö Applet¿¡¼­´Â get,post¹æ½ÄÀ» ÀÌ¿ëÇÏ¿© servlet°ú Á¢¼ÓÇÏ´Â ¿¹¸¦ ÀÌ¿ëÇÏ¿© ClientÂÊ¿¡¼­ Á¢¼ÓÇÑ´Ù¸é Server ÂÊÀÇ SecurityÃø¸é¿¡¼­µµ ¸¹Àº ÀÕÁ¡À» °¡Áú¼ö ÀÖ´Ù.
 
1 2
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD