Java Basic Knowledge Enhancement Network programming note 04:UDP data from the sending end of the keyboard input case

Source: Internet
Author: User

1. Data from the keyboard input

Keyboard input data to control the entry to the end.

2. Code implementation:

(1) Sending side:

1  PackageCom.himi.updDemo1;2 3 Importjava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 Importjava.net.InetAddress;7 ImportJava.util.Scanner;8 9 /**Ten  *  One * UDP protocol sends data: A * 1. Create the socket object on the sending side - * 2. Create the data and package the data - * 3. Call the Send method of the socket object, send the packet the * 4. Releasing Resources -  * -  */ -  Public classUdpsenddemo { +      Public Static voidMain (string[] args)throwsIOException { -         //Create a send-side socket object, where UDP uses the client's socket object class Datagramsocket. +         //Datagramsocket:datagramsocket () ADatagramsocket ds =NewDatagramsocket (); at          -         //Create the data and package the data -         //Datagrampacket: This class represents a datagram packet -         /**Datagrampacket (byte[] buf, int length, inetaddress address, int port) - * * Constructs a datagram package that is used to send packets of length to the specified port number on the specified host.  -         */ in          -          while(true) { toScanner sc =NewScanner (system.in); +String data =sc.nextline (); -         if(Data.equals ("886")){ the              Break; *         } $             Panax Notoginseng         byte[] bytes =data.getbytes (); -         //IP Address Object theInetAddress address = Inetaddress.getbyname ("49.123.72.145"); +         //Port number A         intPort = 10086; the         //Data Packaging +Datagrampacket DP =Newdatagrampacket (Bytes, bytes.length, address, port); -          $              $         //call the Send method of the socket object, send the packet -         //Public void Send (Datagrampacket p) - Ds.send (DP); the          -         }Wuyi         //Freeing Resources the ds.close (); -          Wu     } -  About}

(2) Receiving end:

1  PackageCom.himi.updDemo1;2 3 Importjava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 Importjava.net.InetAddress;7 8 /**9  * Ten * UDP protocol receives data: One * 1. Create a socket object on the receiving end A * 2. Create a packet, receive data (Receive container) - * 3. Call the Receive method of the socket object, receive the packet - * 4. Parse the packet and display it in the console the * 5. Releasing Resources -  * -  */ -  +  Public classUdpreceivedemo { -  +      Public Static voidMain (string[] args)throwsIOException { A         //create a socket object on the receiving end at         //datagramsocket (int port): Creates a datagram socket and binds it to the specified port on the local host -Datagramsocket ds =NewDatagramsocket (10086); -          -          -          while(true) { -             //Create a packet, receive data (Receive container) in             //Datagrampacket (byte[] buf, int length) -             byte[] bytes =New byte[1024]; to             intLength =bytes.length; +  -Datagrampacket DP =Newdatagrampacket (bytes, length); the  *             //calls the Receive method of the socket object, receives the packet $             //Public void receive (Datagrampacket p)Panax NotoginsengDs.receive (DP);//Blocking Method -  the             //parse the packet and display it in the console +             //get the IP of each other A             //Public inetaddress getaddress () theInetAddress address =dp.getaddress (); +String IP =address.gethostaddress (); -  $             //Public byte[] GetData (): Buffer to get data $             //public int GetLength (): Gets the actual length of the data -             byte[] Bys =Dp.getdata (); -             intLen =dp.getlength (); theString s =NewString (bys, 0, Len); - WuyiSystem.out.println ("Send side" + IP + "Data:" +s); the         } -          Wu         //Freeing Resources -         //ds.close (); About          $                  -  -     } -  A}

The results are as follows:

Java Basic Knowledge Enhancement Network programming note 04:UDP data from the sending end of the keyboard input case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.