Java - Beginner - Network Programming - UDP


===============================

One of the three elements IP address




In class A:

         (1) 10.XXX is a private address (a private address is not used on the Internet)

         (2) 127.XXX is a reserved address

            Category A China has only three government telecommunications military departments, most of which are in the United States

In class B:

         (1) 172.16.0.0 --- 172.31.255.255 is a private address

         (2) 169.254.XX is a reserved address

            Class B is generally the campus network of the university

In class C:

           192.168.XX is a private address

            We generally use class C


======================

Two related DOS commands:

            ipconfig to view the local ip address

            Ping is followed by the ip address to test whether there is a problem with the communication between the machine and the specified ip address

 Special IP address:

            127.0.0.1 loopback address (representing this machine)  

             XXX255 broadcast address

             XXX0 network address 




The second of the three elements Port number



The third of the three elements Agreement

rules of communication

three-way handshake protocol TCP

         A: Send request B: Reply agree A: Send



===================================================



========================================

UDP protocol sending protocol

A: Create the sender's Socket object

B: Create data and package data

C: Call the Socket object send method to send data

D: release resources

package cn.itcast_02;

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

public class SendDemo {
   public static void main(String[] args) throws IOException {
	   //Socket sk = new Socket();
	   DatagramSocket ds = new DatagramSocket();
	   //create packet
	   
	   // send data packet
	   //public void send(DatagramPacket p);
	   byte[] bys = "hello, udp, here I come.".getBytes();
	   int length = bys.length;
	   InetAddress address = InetAddress.getByName("DESKTOP-0257CLH");
	   int port = 10086;
	   
	   //DatagramSocket(byte[] buf,int length, InetAddress address,int port)
	   DatagramPacket dp = new DatagramPacket(bys,length,address,port);
	   ds.send(dp);
	   
	   // release resources
	   ds.close();
	   
   }

}

UDP protocol receiving protocol

A: Create the sender's Socket object

B: Create a packet (accept container)

C: Call the Socket object receive method to receive data

D: Parse the packet and display it in the console

E: release resources


package cn.itcast_02;

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

public class receiveDemo {
   public static void main(String[] args) throws IOException {
	   
	   //Create Socket object
	   //Construct DatagramSocket(int port) with parameters
	   DatagramSocket ds = new DatagramSocket(10086);
	   
	   //Create a packet to receive the object
	   //DatagramPacket(byte[] buf, int length)   
	   byte[] bys = new byte[1024];
	   int length = bys.length;
	   DatagramPacket dp = new DatagramPacket(bys,length);
	   
	   //Call the receive method of the Socket object to receive data
	   // public void receive(DatagramPacket p)
	   ds.receive(dp);
	   
	   // Parse the packet and display it in the console
	   //Get the other party's ip
	   InetAddress address = dp.getAddress();
	   String ip = address.getHostAddress();
	     
	   //public byte[] getData(): Get the data buffer
	   byte[] bys2 = dp.getData();
	   //public int getLength(): Get the actual length of the data
	   int len = dp.getLength();
	   String s = new String(bys2,0,len);
	   System.out.println(ip+"The data passed is: "+s);
	   
 
	   // release resources
	   ds.close();
   }
}

====================

diagram


=====================


Main program:

package cn.itcast_03;

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

/*
 * Improve the method of changing talents through multi-threading, so that you can send and receive data in one window
 *
 *
 */
public class ChatRoom {
    public static void main(String[] args) throws IOException {
		DatagramSocket dsSend = new DatagramSocket();
		DatagramSocket dsReceive = new DatagramSocket(12306);
		
		SendThread  st = new SendThread(dsSend);
		ReceiveThread  rt = new ReceiveThread(dsReceive);
		
		Thread t1 = new Thread(st);
		Thread t2 = new Thread(rt);
		
		t1.start();
		t2.start();
	}
}

sender:

package cn.itcast_03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class SendThread implements Runnable {

	private DatagramSocket ds;

	public SendThread(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {
		// Encapsulate keyboard input data
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		while ((line = br.readLine()) != null) {

			byte[] bys = line.getBytes();
			// create packet
			// send packet
			// public void send(DatagramPacket p);
			// byte[] bys = "hello,udp, here I come.".getBytes();
			// int length = bys.length;
			// InetAddress address = InetAddress.getByName("DESKTOP-0257CLH");
			// int port = 10086;

			// DatagramPacket(byte[] buf,int length, InetAddress address,int port)
			// DatagramPacket dp = new DatagramPacket(bys,length,address,port);

			DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("DESKTOP-0257CLH"), 12306);

			ds.send(dp);

		}
		// release resources
		ds.close();
		}catch(IOException e) {
			e.printStackTrace ();
		}

	}

}

receiver

package cn.itcast_03;

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

public class ReceiveThread implements Runnable {

	private DatagramSocket ds;

	public ReceiveThread(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {
		while (true) {

			// create a packet to receive the object
			// DatagramPacket(byte[] buf, int length)
			byte[] bys = new byte[1024];
			// int length = bys.length;
			DatagramPacket dp = new DatagramPacket(bys, bys.length);

			// Call the receive method of the Socket object to receive data
			// public void receive(DatagramPacket p)
			ds.receive(dp);

			// Parse the packet and display it in the console
			// get the other party's ip
			// InetAddress address = dp.getAddress();
			// String ip = address.getHostAddress();
			String ip = dp.getAddress().getHostAddress();

			// public byte[] getData(): Get the data buffer
			// byte[] bys2 = dp.getData();
			// public int getLength(): Get the actual length of the data
			// int len = dp.getLength();
			String s = new String(dp.getData(), 0, dp.getLength());
			System.out.println(ip + "The data passed is: " + s);

		}
		}catch(IOException e) {
			e.printStackTrace ();}
	}

}











Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726322&siteId=291194637