Page 471 - 2020학년도 MDP과제발표회 자료집 (통신과) (3)
P. 471

private  int  PORT;


                     private  SocketChannel  m_SocketChannel;
                     private  Selector  m_Selector;


                     private  readDataThread  m_readData;
                     private  sendDataThread  m_sendData;


                     private  Handler  m_handler;


                     private  final  static  int  SOCKET_CREATE_SUCCESS  =  0;
                     private  final  static  int  DATA_RECV_SUCCESS  =  1;


                     public  SocketManager(String  ip,  int  port,  Handler  h)  {
                             this.IP  =  ip;
                             this.PORT  =  port;
                             this.m_handler  =  h;


                             //  Thread  Objects 의  작업  할당  및  초기화
                             m_readData  =  new  readDataThread();
                             m_readData.start();
                     }


                     private  void  setSocket(String  ip,  int  port)  throws  IOException  {


                             //  selector  생성
                             m_Selector  =  Selector.open();
                             //  채널  생성
                             m_SocketChannel  =  SocketChannel.open(new  InetSocketAddress(ip,  port));
                             //  논블로킹  모드  설정
                             m_SocketChannel.configureBlocking(false);
                             //  소켓  채널을  selector 에  등록
                             m_SocketChannel.register(m_Selector,    SelectionKey.OP_CONNECT              |
             SelectionKey.OP_READ  |  SelectionKey.OP_WRITE);
                     }


                     public  void  sendData(String  data){
                             m_sendData  =  new  sendDataThread(m_SocketChannel,  data);
                             m_sendData.start();
                     }


                     private  void  read(SelectionKey  key)  throws  Exception  {
                             //  SelectionKey 로부터  소켓채널을  얻어온다.
                             SocketChannel  sc  =  (SocketChannel)  key.channel();



                                                                                      인천전자마이스터고등학교
                                                         -  491  -                       정보통신기기과         491
   466   467   468   469   470   471   472   473   474   475   476