1, the construction method of Datagrampacket class
2, the construction method of Datagramsocket class
3. Common methods of Datagramsocket class
4. Service Side
package com.ljb.app.datagram;import java.io.IOException;import java.net.DatagramPacket; import java.net.datagramsocket;import java.net.socketaddress;import java.net.socketexception;/* * * Customer Advisory Service * @author LJB * @version 2015 March 12 */public class askserver { /** * @param args */ public static void main (String[] args) { try { // Create receiver Sockets (server), and bind the port number datagramsocket ds = new datagramsocket (8800); // Determines the size of the array of data received by the packet byte[] buf = new byte[1024]; // Create a packet of the receiving type, and the data is stored in the array DatagramPacket dp = new Datagrampacket (buf,buf.length); // receive data via sockets Ds.receive (DP); // parsing the data sent by the sender string mess = new string (buf 0 , dp.getlength ());    SYSTEM.OUT.PRINTLN ("client says:" + mess); // response Information String reply = "Hello, I am in, please inquire." "; byte[] buf2 = reply.getbytes (); // Response Address socketaddress sa = dp.getsocketaddress (); // packet datagrampacket dp2 = new datagrampacket (buf2 ,  BUF2.LENGTH , SA); // send ds.send (DP2) ; // Release Resources ds.close (); } catch ( socketexception e) { e.printstacktrace (); } catch (IOException e) { &nbsP;e.printstacktrace (); } }}
5, client
Package com.ljb.app.datagram;import java.io.ioexception;import java.net.datagrampacket;import java.net.datagramsocket;import java.net.inetaddress;import java.net.socketexception;import java.net.UnknownHostException;/** * Customer Advisory Client * @author LJB * @version 2015 March 12 */public class AskClient { /** * @param args */ public static void main (String[] args) { // Determine the information sent to the server, server address, Port String mess = "Hello, I want to ask a question?" "; byte[] buf = mess.getbytes (); inetaddress ia = null; try { ia = inetaddress.getbyname ("localhost"); } catch (unknownhostexception e) { e.printstacktrace (); } int port = 8800; // Create a packet, send the specified length of information to the specified port on the specified server DatagramPacket dp = new Datagrampacket (Buf , buf.length , ia , port); // Create a Socket object datagramsocket ds = null; try { ds = new datagramsocket (); } catch (socketexception e) { e.printstacktrace (); } // send packet try { ds.send (DP); } catch (IOEXCEPTION E) { e.printstacktrace (); } // create receive array byte[] buf2 = new byte[ 1024]; // creating a Receive packet DatagramPacket dp2 = new Datagrampacket (buf2 , buf2.length); // receive data try { ds.receive (DP2); &nBSP;} catch (ioexception e) { e.printstacktrace (); } // parse the received data string reply = new string (buf2 , 0 , dp2.getlength ()); system.out.println ("server-side said:" + reply); // Release Resources ds.close (); }}
Operation Result:
Service side:
Client said: Hello, I would like to ask a question?
Client:
Server side said: Hello, I am in, please consult.
Customer advisory < socket programming based on UDP protocol (datagram socket) >