Java-- realization of Socket UDP protocol

UDP protocol (User Datagram Protocol)

Internet protocol suite supports a connectionless transport protocol, the protocol called User Datagram Protocol (UDP, User Datagram Protocol). UDP provides a method of establishing a connection can be sent without the encapsulated IP packet for the application.
Internet has two main transport layer protocols, complement each other. No connection is UDP, in addition to its function to send packets to the application and allow them to own protocol architecture at the desired level, hardly anything special to do. Is connection-oriented TCP, the protocol to do almost everything.

Java implementation

The sender: send
the receiving end: receive
properly understood in real life, then sent out to the sender.
However, the program is just the opposite, is to start the receiving end .
(As will be appreciated, the process of transmission, the receiver is to buy a courier, anxiously await the transmitting side and transmits it to the process to get their hands).

The transmitting end to realize the steps of:

1. requires a socket object ---- DatagramSocket (sending and receiving ends are used in this object)
the UDP protocol does not need to create a connection, but requires a bridge (the Socket), is required DatagramSocket object, this object can be at the bottom help us to build a pipeline, the data sent. Socket object for each protocol needs are not the same, because the underlying each different protocol implementations are not the same, but we do not have the underlying to realize, because Java has helped us a good package, so we can directly create a Socket object.
2. The data transmission requires a labeled packet -DatagramPacket the object
when the object is created packet requires five parameters:

  1. Transmitting real data, byte [] Byte
  2. Where this array of end 0
  3. The length of the end of transmission byte length
  4. Receiving end address ip
  5. Open receiving end port number

:( find the IP address of the default is localhost)
Here Insert Picture Description
3. the Socket object is sent out


UDP transmission-side code:

package send;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Send {
    public static void main(String[] args) {
        System.out.println("==send端==");
        try {
            //1.需要一个socket对象
            DatagramSocket socket = new DatagramSocket();
            //2.发送的数据需要打成一个包
            //packet对象创建时需要5个参数:
//            1. 发送的真实数据, byte[]
//            2. 从这个数组的哪里结束  0
//            3. 发送字节的长度  末尾  length
//            4. 接收端的ip地址
//            5.接收端开放的端口号
            Scanner input = new Scanner(System.in);
            System.out.println("跟接收端说点什么吧:");
            String message = input.nextLine();
            byte[] bMsg = message.getBytes();//将字符串转化成byte类型数组
            InetAddress ip = InetAddress.getByName("localhost");//将字符串转化为IP对象
            int port = 9999;//端口号
            DatagramPacket packet = new DatagramPacket(bMsg,0,bMsg.length,ip,port);
            //3.Socket对象发送出去
            socket.send(packet);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

The receiving end to realize the steps of:

1. The need to create a socket object -DatagramSocket
must be given the port number receiving end, whether listening port data come in.
2. The need to prepare an empty packet received -DatagramPacket objects
null packets need three parameters

  1. An empty array
  2. Informing the beginning of the reception which position the array
  3. Receiving a long array

3. The received data
4 show, using the data
in accordance with the number of valid bytes received constructed.


UDP receiving side code:

package receive;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Receive {
    public static void main(String[] args) {
        System.out.println("==receive==");
        try {
//            1.需要创建一个socket对象—DatagramSocket
            DatagramSocket socket = new DatagramSocket(9999);
            //2.需要准备一个空的数据包进行接收
            byte[] b = new byte[100];
            DatagramPacket packet = new DatagramPacket(b,0,b.length);
//        空数据包需要三个参数
            //        1.一个空的数组
            //        2.告知用数组的哪个位置开始接收
            //        3.用数组的多长进行接收
//           3.接收数据
            socket.receive(packet);//参数不是真正的参数含义,这个参数是为了接收数据,
            //这个参数的含义更像是返回值,把接受的数据放在参数里作为返回值.
//           4.展示、使用数据
            String value = new String(b,0,packet.getLength());//按照接收到的有效字节个数来构建
            System.out.println("接收到的数据:"+value);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

test

First start the receiving end, then start transmitting end.
Here Insert Picture Description

Published 46 original articles · won praise 15 · views 2617

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/104407232