Java method for implementing UDP based on socket Service

Source: Internet
Author: User

Java method for implementing UDP based on socket Service

This example describes how Java implements UDP based on the socket service. Share it with you for your reference. The details are as follows:

Example 1:

Receiving class:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Package com. socket. demo;

Import java. io. IOException;

Import java.net. DatagramPacket;

Import java.net. DatagramSocket;

Public class UDPReceiveDemo {

Public static void main (String [] args) throws IOException {

System. out. println ("acceptor starts ............ ");

/*

2. To create a UDP socket service, you must specify a port number.

3. Create a data packet to store the received data and use the data packet object method to parse the data.

4. Use the receive method of DatagramSocket to store the received data into the data packet.

5. parse the data in the data packet through the data packet Method

5. Disable the socket service.

*/

// Udpsocket service, using the initramsocket object

DatagramSocket ds = new DatagramSocket (10002 );

// Use DatagramPacket to encapsulate data in this object

Byte [] buf = new byte [1, 1024];

DatagramPacket dp = new DatagramPacket (buf, buf. length );

// Send data packets through the udp socket service.

Ds. receive (dp );

// Parse the data in the data packet through the data packet method, such as the address, port, and data content.

String ip = dp. getAddress (). getHostAddress ();

// String name = dp. getAddress (). getHostName ();

Int port = dp. getPort ();

String text = new String (dp. getData (), 0, dp. getLength ());

// System. out. println ("-----" + ip + "-----" + name + "-----" + port + "-----" + text );

System. out. println ("-----" + ip + "----------" + port + "-----" + text );

// Close the resource

Ds. close ();

}

}

Sending class:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Package com. socket. demo;

Import java. io. IOException;

Import java.net. DatagramPacket;

Import java.net. DatagramSocket;

Import java.net. InetAddress;

Import java.net. SocketException;

Import java.net. UnknownHostException;

Public class UDPSendDemo {

Public static void main (String [] args) throws IOException {

System. out. println ("the sender starts ............ ");

/*

* 1. Create a udp transmission sender.

2. Establish UDP socket Service

3. encapsulate the data to be sent into the data packet

4. Send data packets through the udp socket Service

5. Disable the socket service.

*/

// Udpsocket service, using the initramsocket object

DatagramSocket ds = new DatagramSocket (8888); // listening port

// Encapsulate the data to be sent into the data packet

String str = "udp transmission demonstration, go ";

// Use DatagramPacket to encapsulate data in this object

Byte [] buf = str. getBytes ();

DatagramPacket dp =

New DatagramPacket (buf, buf. length, InetAddress. getByName ("192.168.1.100"), 10002 );

// Send data packets through the udp socket service.

Ds. send (dp );

// Close the resource

Ds. close ();

}

}

Example 2:

Receiving class:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

Package com. socket. demo;

Import java. io. IOException;

Import java.net. DatagramPacket;

Import java.net. DatagramSocket;

Public class UDPReceiveDemo2 {

Public static void main (String [] args) throws IOException {

System. out. println ("acceptor starts ............ ");

/*

2. To create a UDP socket service, you must specify a port number.

3. Create a data packet to store the received data and use the data packet object method to parse the data.

4. Use the receive method of DatagramSocket to store the received data into the data packet.

5. parse the data in the data packet through the data packet Method

5. Disable the socket service.

*/

// Udpsocket service, using the initramsocket object

DatagramSocket ds = new DatagramSocket (10003 );

While (true ){

// Use DatagramPacket to encapsulate data in this object

Byte [] buf = new byte [1, 1024];

DatagramPacket dp = new DatagramPacket (buf, buf. length );

// Send data packets through the udp socket service.

Ds. receive (dp); // blocked.

// Parse the data in the data packet through the data packet method, such as the address, port, and data content.

String ip = dp. getAddress (). getHostAddress ();

// String name = dp. getAddress (). getHostName ();

Int port = dp. getPort ();

String text = new String (dp. getData (), 0, dp. getLength ());

// System. out. println ("-----" + ip + "-----" + name + "-----" + port + "-----" + text );

System. out. println ("-----" + ip + "----------" + port + "-----" + text );

}

// Close the resource

// Ds. close ();

}

}

Sending class:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

Package com. socket. demo;

Import java. io. BufferedReader;

Import java. io. IOException;

Import java. io. InputStreamReader;

Import java.net. DatagramPacket;

Import java.net. DatagramSocket;

Import java.net. InetAddress;

Public class UDPSendDemo2 {

Public static void main (String [] args) throws IOException {

System. out. println ("the sender starts ............ ");

/*

* 1. Create a udp transmission sender.

2. Establish UDP socket Service

3. encapsulate the data to be sent into the data packet

4. Send data packets through the udp socket Service

5. Disable the socket service.

*/

// Udpsocket service, using the initramsocket object

DatagramSocket ds = new DatagramSocket (9999); // listening port

// Encapsulate the data to be sent into the data packet

// String str = "udp transmission demonstration, go ";

BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); // keyboard input

String line = null;

// Use DatagramPacket to encapsulate data in this object

While (line = bufr. readLine ())! = Null ){

Byte [] buf = line. getBytes ();//

DatagramPacket dp =

New DatagramPacket (buf, buf. length, InetAddress. getByName ("192.168.1.100"), 10003 );

// Send data packets through the udp socket service.

Ds. send (dp );

If ("886". equals (line )){

Break;

}

}

// Close the resource

Ds. close ();

}

}

Run the following command:

Receive:

Send:

I hope this article will help you with java programming.

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.