UDP transport multithreading

A transmission and reception windows created 

        public  class Demo3_MoreThread { 

            / ** 
             * @param args
              * / 
            public  static  void main (String [] args) {
                 new new the Receive () Start ();. 
                
                New new the Send () Start ();. 
            } 
        
        } 

        class the Receive the extends the Thread {
             public  void RUN () {
                 the try { 
                    DatagramSocket socket = new new DatagramSocket (6666);                     // Create a socket equivalent to creating Harbor
                    = Packet of DatagramPacket new new of DatagramPacket ( new new  byte [1024], 1024);     // Create Create Container packet corresponding to 
                    
                    the while ( to true ) { 
                        Socket.Receive (packet);                                                 // receiving goods 
                        byte [] = ARR packet.getData ();
                         int len = packet.getLength (); 
                        String IP = packet.getAddress () getHostAddress ();. 
                        System.out.println (IP + ":" + new new String (ARR, 0  , len));
                    }
                } The catch (IOException E) { 
                    
                    e.printStackTrace (); 
                } 
            } 
        } 

        class the Send the extends the Thread {
             public  void RUN () {
                 the try { 
                    DatagramSocket socket = new new DatagramSocket ();         // Create a socket equivalent to creating Harbor 
                    Scanner SC = new new Scanner (the System.in); 
                    
                    the while ( to true ) { 
                        String STR =sc.nextLine ();
                         
            }IF ( "quit" .equals (str))
                             BREAK ; 
                        DatagramPacket packet =                              // create packet is equivalent to creating container 
                                new new DatagramPacket (str.getBytes (), str.getBytes () length, InetAddress.getByName ( "127.0.0.1." ), 6666 ); 
                        socket.send (Packet);             // delivery 
                    } 
                    the Socket.close (); 
                }   the catch (IOException E) { 
                    
                    e.printStackTrace (); 
                } 
        }

 

Guess you like

Origin www.cnblogs.com/yaobiluo/p/11357791.html