DatagramPacket and DatagramSocket based on UDP

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
1 Basic Concepts:
  A.DatagramPacket and DatagramSocket in the package
  B.DatagramPacket said the stored data report, DatagramSocket said to send or receive a datagram socket
  C. by the two class all constitute the network link is based on the UDP protocol, is an unreliable protocol.
  The reason is not reliable because the sender is not responsible for the data is sent, the receiver receives data without feedback message to the sender, easily lead to the loss of information.
  But the protocol is efficient, such as CS (half life) game is based on the UDP protocol, otherwise we estimate to play a game after the machine up, so we can see the game sometimes lost frames.
  2 using the method:
  To establish a UDP protocol based on the links, we should first establish a socket<DatagramSocket>(the sending station or the receiving station), then through the socket to send or receive data<DatagramPacket>.
  We first look at the DatagramSocket and DatagramPacket classes
  The DatagramSocket class:
  Construction method:
  That creates a default socket, and bound to the local address and a random port number
  DatagramSocket(int port):
  With the above different is, bound to a specific port number, the other unchanged
  DatagramSocket(int port, InetAddress iad):
  That creates a socket, bind to specific address and port number specified
  DatagramSocket(SocketAddress sad);
  That creates a socket, bind to the socket address specific
  Tip: the so-called local address we can understand this, we adopted the "control panel - local connection support" can be seen in a IP address, this is the local IP address, we will understand the local address.
  The basic method:
  Close the socket
  recevie(DatagramPacket dp):
  Accept the data report
  send(DatagramPacket dp):
 Send a datagram
  It touched the datagram, the datagram:
  The DatagramPacket class:
  Method: (only list the major)
  Accept type:
  DatagramPacket(byte[] buf, int length):
  To accept the length of the buf data length (data stored in an array of bytes in buf)
  Transmission type:
  DatagramPacket(byte[] buf, int length, InetAddress address, int port);
  BUF data will be sent to the port number length at the specified address
  DatagramPacket(byte[] buf, int length, SocketAddress address);
   The buf data transmission length long into the socket address at the specified
  Have mentioned above, we sent the data structure of datagram is sent and receive data type, the time should be accepted types    
  Start writing code
  Code intent:
  1 a receiver, a transmitter
  2 when the receiver receives the sender of the message, the message and the sender address and port number print sends, after sending feedback information to a receiving party: "I received!"
  3 print out the feedback message sender receiver
  See the code with ideas:
[java] view plaincopyprint?
public class Sender { 
    public static void main(String[] args) { 
        try { 
            // Create the sender socket, IP default to the local port number, random
            DatagramSocket sendSocket = new DatagramSocket(); 
            // Sure you want to send the message:
            String mes = "Hello. The receiver!";
            // The data reported data is stored in the form of an array of characters transmitted, so the transmission data
            byte[] buf = mes.getBytes(); 
            // To determine the IP address and port number, address is the local machine.
            int port = 8888; 
            InetAddress ip = InetAddress.getLocalHost(); 
            // Create send type data report:
            DatagramPacket sendPacket = new DatagramPacket(buf, buf.length, ip, 
            // Through the socket send data:
            // To determine the buffer memory to accept feedback data, namely the storage data byte array
            byte[] getBuf = new byte[1024]; 
            // Create the accepted types of datagram
            DatagramPacket getPacket = new DatagramPacket(getBuf, getBuf.length); 
            // Receive data through the socket
            // Analysis of the feedback message, and print
            String backMes = new String(getBuf, 0, getPacket.getLength()); 
            System.out.println("The receiving party returned message: " + backMes);
            // Close the socket
        } catch (Exception e) { 

public class Receive { 
    public static void main(String[] args) { 
        try { 
            // OK to accept the IP and port number to the IP address is local, the machine address
            InetAddress ip = InetAddress.getLocalHost(); 
            int port = 8888; 
            // Create the sockets, and set the port number and IP address
            DatagramSocket getSocket = new DatagramSocket(port, ip); 
            // Determine the datagram received data of array size
            byte[] buf = new byte[1024]; 
            // Create the accepted types of data, data will be stored in the buf
            DatagramPacket getPacket = new DatagramPacket(buf, buf.length); 
            // Receive data through the socket
            // Analytical and transmitting message, and print
            String getMes = new String(buf, 0, getPacket.getLength()); 
            System.out.println("Send the message: " + getMes);
            // Get the IP and port number to the sender through the data, and print
            InetAddress sendIP = getPacket.getAddress(); 
            int sendPort = getPacket.getPort(); 
            System.out.println("Each other's IP address is: " + sendIP.getHostAddress());
            System.out.println("The port number of the other party is: " + sendPort);
            // Get the socket address sender through data report
            SocketAddress sendAddress = getPacket.getSocketAddress(); 
            // To determine the content of feedback to the sender of the message, and converted into an array of bytes
            String feedback = "The receiver said: I received!";
            byte[] backBuf = feedback.getBytes(); 
            // Create send type data report
            DatagramPacket sendPacket = new DatagramPacket(backBuf, 
                    backBuf.length, sendAddress); 
            // Through the socket send data
            // Close the socket
        } catch (Exception e) { 

The test has passed
  Start the receiver, the sender can then start
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Nelson at December 09, 2013 - 9:08 AM