Java UDP uses sockets for network communication (0)

Source: Internet
Author: User

Author: Ching

Original address: http://blog.csdn.net/qingdujun/article/details/39300293

The following shows a program that uses a client to send a message to the server, which is printed on the control Panel after it is received.

1) client, send data to server

2) server, after receiving the data, print it on the control Panel

First, the client, the main steps:

1) Create a UDP service. Through the Datagramsocket object.

2) Create the data and encapsulate it into a packet.

3) Send the existing data packets through the socket service. by send mode.

4) Close the resource.

Client, send data code as follows:

[Java]View Plaincopyprint?
  1. Package UDP.SEND.QDJ;
  2. Import Java.net.DatagramPacket;
  3. Import Java.net.DatagramSocket;
  4. Import java.net.InetAddress;
  5. UDP Send Side
  6. Public class Cudpsend {
  7. public static void Main (string[] args) throws Exception {
  8. //1. Create a UDP service.  Through the Datagramsocket object.
  9. Datagramsocket ds = new Datagramsocket (1234);
  10. //2. Create the data and encapsulate it into a packet.
  11. //datagrampacket (byte[] buf, int length, inetaddress address, int port)
  12. //Constructs a datagram package that is used to send packets of length to the specified port number on the specified host.
  13. byte[] buf = "UDP send from Server". GetBytes ();
  14. Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("10.100.56.210"),10000);
  15. //3. Send the existing data packets through the socket service.  by send mode.
  16. Ds.send (DP);
  17. //4. Closing Resources
  18. Ds.close ();
  19. }
  20. }

Second, the server side, the main steps are as follows:

1) Create the UDP Socket and set up the endpoint. Listening to a port, in fact, is to give this network program a digital indicator, convenient and clear, which data the application can handle.

2) define the packet. Used to store data.

3) The received data is stored in the packet via the Receive method of the service.

4) Get the data in the data packet method.

5) Close the resource.

Server side, accept the data and print it on the control Panel, the code is as follows:

[Java]View Plaincopyprint?
  1. Package UDP.RECE.QDJ;
  2. Import Java.net.DatagramPacket;
  3. Import Java.net.DatagramSocket;
  4. UDP Receive End
  5. Public class Cudprece {
  6. public static void Main (string[] args) throws exception{
  7. //1. Create a UDP Socket to establish an endpoint. Listening to a port, in fact, is to give this network program a digital indicator,
  8. //convenient with clear, which data the application can handle.
  9. Datagramsocket ds = new Datagramsocket (10000);
  10. //2. Define the packet.  Used to store data.
  11. byte[] buf = new byte[1024];
  12. Datagrampacket DP = new Datagrampacket (buf,buf.length);
  13. //3. The received data is stored in a packet through the receive method of the service.
  14. //This method is a blocking method and is blocked when no data is accepted.
  15. Ds.receive (DP);
  16. //4. Gets the data in the data packet method.
  17. //Get an IP address
  18. String IP = dp.getaddress (). gethostaddress ();
  19. //Get Transfer data
  20. String data = new String (Dp.getdata (),0,dp.getlength ());
  21. //Get port number
  22. int port = Dp.getport ();
  23. SYSTEM.OUT.PRINTLN ("IP address:" +ip+"\ nthe content data:" +data+"\ n port number port:" +port);
  24. //5. Closing Resources
  25. Ds.close ();
  26. }
  27. }

The running results show:

References: Java video Bi Xiangdong presenter

Original address: http://blog.csdn.net/qingdujun/article/details/39300293

Java UDP uses sockets for network communication (0)

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.