Udpの使用と応用

10641 ワード

目次

  • ディレクトリ
  • 入門
  • 送信側
  • 2受信端子
  • は、通信ソフトウェアに情報
  • を送信する.
  • 人以上のチャットの実現
  • 送信側
  • 2受信端子
  • 3呼び出し端子


  • 1.入門


    1.1. 送信側

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    /**
     *  udp ( )
     * @date 2018 2 5 
     */
    public class Sender {
    
        /**
         * udp 
         *  :DatagramSocket,DatagramPacket
         * @param args    
         * @throws SocketException
         */
        public static void main(String[] args) throws Exception {
            //1. udp 
            DatagramSocket datagramSocket = new DatagramSocket();
            //2. 
            String data = " socket ";
            byte datas[] = data.getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(datas, datas.length, InetAddress.getLocalHost(), 9090);
            //3. udp 
            datagramSocket.send(datagramPacket);
            //4. ( )
            datagramSocket.close();
        }
    }

    1.2. じゅしんたんし

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    /**
     *  udp ( )
     * @date 2018 2 5 
     */
    public class Receive {
    
        public static void main(String[] args) throws Exception {
            //1. udp , 9090 
            DatagramSocket socket = new DatagramSocket(9090);
            //2. 
            byte buf[] = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf,buf.length);
            //3. udp 
            socket.receive(packet);//receive , 
            System.out.println(" : "+new String(buf,0,packet.getLength()));//getLength() 
            //4. 
            socket.close();
        }
    }
    

    2.通信ソフトへのメッセージ送信

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    /**
     *  FeiQ 
     * @date 2018 2 5 
     */
    public class SendFeiQ {
    
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket();
            // 
            byte buf[] = getFeiQData("Hi FeiQ!").getBytes();
            DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), 2425);
            socket.send(packet);
            socket.close();
            }
            /**
             *  , 
             * feiQ :
             * version:time:sender:ip:flag:content
             *   : : :IP : (32): ;
             * @param data
             * @return    
             */
            public static String getFeiQData(String centent){
            StringBuilder data = new StringBuilder();
            data.append("1.0:");
            data.append(System.currentTimeMillis()+":");
            data.append(" :");
            data.append("192.168.0.107:");
            data.append("32:");
            data.append(centent);
            return data.toString();
        }
    }
    

    3.マルチチャットの実現


    3.1. 送信側

    /**
     * udp ( )
     * @date 2018 2 5 
     */
    public class CharSender extends Thread{
    
        @Override
        public void run() {
            DatagramSocket socket = null;
            BufferedReader reader = null;
            try {
                socket = new DatagramSocket();
                // 
                reader = new BufferedReader(new InputStreamReader(System.in));
                String line = null;
                DatagramPacket packet = null;
                while ((line = reader.readLine()) != null) {// ( 3 .255)
                    packet = new DatagramPacket(line.getBytes(), line.getBytes().length,InetAddress.getByName("192.168.0.255"),9090);
                    socket.send(packet);
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    socket.close();
                }
            }
        }
    }

    3.2. じゅしんたんし

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    /**
     * udp ( )
     * @date 2018 2 5 
     */
    public class CharRecevice extends Thread{
        @Override
        public void run() {
            DatagramSocket socket = null;
            try {
                socket = new DatagramSocket(9090);
                byte buf[] = new byte[1024];
                DatagramPacket packet = new DatagramPacket(buf,buf.length);
                while (true) {
                    socket.receive(packet);
                    System.out.println(packet.getAddress().getHostAddress()+" :"+new String(buf,0,packet.getLength()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                socket.close();
            }
        }
    }

    3.3. 呼び出し先

    public class ChatMain {
    
        public static void main(String[] args) {
            // , 
            ChatRecevice recevice = new ChatRecevice();
            ChatSender sender = new ChatSender();
            recevice.start();
            sender.start();
        }
    }