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?
- Package UDP.SEND.QDJ;
- Import Java.net.DatagramPacket;
- Import Java.net.DatagramSocket;
- Import java.net.InetAddress;
- UDP Send Side
- Public class Cudpsend {
- public static void Main (string[] args) throws Exception {
- //1. Create a UDP service. Through the Datagramsocket object.
- Datagramsocket ds = new Datagramsocket (1234);
- //2. Create the data and encapsulate it into a 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.
- byte[] buf = "UDP send from Server". GetBytes ();
- Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("10.100.56.210"),10000);
- //3. Send the existing data packets through the socket service. by send mode.
- Ds.send (DP);
- //4. Closing Resources
- Ds.close ();
- }
- }
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?
- Package UDP.RECE.QDJ;
- Import Java.net.DatagramPacket;
- Import Java.net.DatagramSocket;
- UDP Receive End
- Public class Cudprece {
- public static void Main (string[] args) throws exception{
- //1. Create a UDP Socket to establish an endpoint. Listening to a port, in fact, is to give this network program a digital indicator,
- //convenient with clear, which data the application can handle.
- Datagramsocket ds = new Datagramsocket (10000);
- //2. Define the packet. Used to store data.
- byte[] buf = new byte[1024];
- Datagrampacket DP = new Datagrampacket (buf,buf.length);
- //3. The received data is stored in a packet through the receive method of the service.
- //This method is a blocking method and is blocked when no data is accepted.
- Ds.receive (DP);
- //4. Gets the data in the data packet method.
- //Get an IP address
- String IP = dp.getaddress (). gethostaddress ();
- //Get Transfer data
- String data = new String (Dp.getdata (),0,dp.getlength ());
- //Get port number
- int port = Dp.getport ();
- SYSTEM.OUT.PRINTLN ("IP address:" +ip+"\ nthe content data:" +data+"\ n port number port:" +port);
- //5. Closing Resources
- Ds.close ();
- }
- }
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)