Implementation through socket object
##############################################################
The UDP sender needs to be established:
The way of thinking is:
1. Set up a socket service that can realize UDP transmission
2. Specify the data to be sent
3. Send data through socket service
4. Shut down service
Steps:
1. Create DatagramSocket object
2. Create datagram packet object, pay attention to parameters, (array, array length, input ip and port through InetAddress.getByName("127.0.0.1")
3. Send datagram packet object through send of datagram socket object
4. Close DatagramSocket object
//###Sender Code:
System.out.println("UDP Transmitter start"); //1,Establish UDP service DatagramSocket ds = new DatagramSocket(); //2,Clear data String str = "miss"; //3,Send data, encapsulate data into data package //3.1 Encapsulation, clear purpose and port byte[] buf = str.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10000); //3.2 Send out ds.send(dp); ds.close();
##################################################################
receiving end
The way of thinking is:
1. Create socket service and specify port
2. Receiving data
3. Take out the required data, ip, port and data
4. Close resources
Steps:
1. Create DatagramSocket object
2. To create a datagram packet object, the parameter needs a byte array and length
3. Receive the information sent by the sender through the receive method of the datagram socket object, and fill in the datagram packet object with the parameter
4. Use dp.getAddress().getHostAddress() method of datagram packet object to get IP, and getPort method to get port
dp.getData(),dp.getLength(), get data and length
5. Close DatagramSocket object
//###Receiver code
System.out.println("UDP Receiver start"); //1,Establish socket service DatagramSocket ds = new DatagramSocket(10000); //2,Use socket The receiving method of, receive data, store the received data in the data packet, and analyze it through the method of data packet //2.1 Create package byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp); //2.2 Parsing data through packet objects String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); //Get text data String str = new String(dp.getData(),0,dp.getLength()); System.out.println(ip+"-"+port+":"+str); //close resource ds.close();
The above implementation is single sending and single receiving, which is not in line with the reality, so it is necessary to use multithreading to realize multiple sending and multiple receiving
######################################################################################
Read the following code for yourself, and use it in combination with multithreading and flow
UDP chat multiple sending and receiving code:
#######main.java
public class UDPChatTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { /* * Case 1: implement group chat program through udp. Idea: This program has both receiving and sending, needs to be executed at the same time, and needs to use multi-threaded technology. * One thread is responsible for sending and one thread is responsible for receiving. Two tasks are required. */ //Sending end socket Receiver side socket DatagramSocket sendSocket = new DatagramSocket(55555); DatagramSocket receSocket = new DatagramSocket(10002); //Create a task object. Send send = new Send(sendSocket); Rece rece = new Rece(receSocket); //Create a thread and turn it on. Thread t1 = new Thread(send); Thread t2 = new Thread(rece); t1.start(); t2.start(); } }
//Send task
class Send implements Runnable { private DatagramSocket ds; public Send(DatagramSocket ds) { super(); this.ds = ds; } @Override public void run() { try { BufferedReader bufr = new BufferedReader(new InputStreamReader( System.in)); String line = null; while ((line = bufr.readLine()) != null) { byte[] buf = line.getBytes();// Converts data to a byte array. DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.223"), 10002); //IP 255 for broadcast ds.send(dp); if ("886".equals(line)) { break; } } // 4,Close the resource. ds.close(); } catch (IOException e) { } } }
//Receive tasks.
class Rece implements Runnable { private DatagramSocket ds; public Rece(DatagramSocket ds) { super(); this.ds = ds; } @Override public void run() { while (true) { try { byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp);// block String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ":" + port + ":" + text); if(text.equals("886")){ System.out.println(ip+"....Leave the chat room"); } } catch (IOException e) { } } } }