본문 바로가기

공부방/Flex

FLEX 소켓통신 테스트 해보기

서버 : 자바 -> 받는 것만 됨 클라 : 플렉스 -> 받는거 보내는거 다됨 자바
import java.net.*;

import java.io.*;


//테스트용임 .....  


public class TestServer extends Thread{
    private BufferedReader recv_msg = null;
    private Socket soc = null;
    
    public TestServer(){
        ServerSocket server = null;
        try {
            server = new ServerSocket (300);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        System.out.println ("Server Running");
        
        
        
        while (true) {

            try{
                soc = server.accept();
                System.out.println(soc.toString());
                recv_msg = new BufferedReader(new InputStreamReader(soc.getInputStream()));                
                recv_message rm = new recv_message();
                rm.start();                
            }catch (Exception e) {
                e.printStackTrace();
            }            
        }
        
    }

    public class recv_message extends Thread{
        public void run(){
            while(true){
                if(soc==null) return;
                try {
                    String msg = recv_msg.readLine().trim();
                    System.out.println("size : ["+msg.length()+"]"+msg.trim());
                    if(msg.equals("exit")){
                        System.out.println("종료");
                        soc.close();
                        soc=null;                                        
                    }
                } catch (Exception e) {                    
                    e.printStackTrace();
                }



                try{
                    Thread.sleep(200);
                }catch(Exception e){
                    
                }
                
            }
        }
    }
    
    
    public static void main (String args[]) throws Exception{
        new TestServer();
    }
}

Flex