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

socketConnect("192.168.13.17",  8402);        // 소켓  연결
                               sendData("Shiba  connected!");
                       }

                       public  void  sendData(String  data){            // 데이터  발신
                               Log.d("Send",  data);


                               try  {
                                       ByteBuffer  buffer  =  null;
                                       Charset  charset  =  Charset.forName("UTF-8");
                                       buffer  =  charset.encode(data);


                                       socketChannel.write(buffer);                        // 데이터  전송


                                       buffer  =  ByteBuffer.allocate(15000);
                                       int  len  =  socketChannel.read(buffer);      // 데이터  받기
                                       buffer.flip();


                                       String  recive  =  charset.decode(buffer).toString();


                               }  catch  (Exception  e)  {
                                       e.printStackTrace();
                               }
                       }


                       public  void  socketConnect(String  host,  int  port){              // 소켓  연결
                               // 연결  부분
                               try{
                                       socketChannel  =  SocketChannel.open();
                                       socketChannel.configureBlocking(true);
                                       socketChannel.connect(new  InetSocketAddress(host,  port));


                               }  catch  (Exception  e){
                                       e.printStackTrace();
                               }
                       }















                                                                                      인천전자마이스터고등학교
                                                         -  531  -                       정보통신기기과         531
   504   505   506   507   508   509   510   511   512   513   514