首页 > 代码库 > UDP协议

UDP协议

UDP发送端:

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

public class UDPSend {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		byte[] data = "http://www.mamicode.com/你好UDP".getBytes();
         DatagramPacket  dp = new DatagramPacket(data, data.length,InetAddress.getLocalHost(),8000);
//		DatagramPacket  dp = new DatagramPacket
         DatagramSocket ds= new DatagramSocket();
         ds.send(dp);
         ds.close();
	}

}

UDP接收端:

public class UDPReceive {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
         DatagramSocket ds = new DatagramSocket(8000);
         byte[] data = http://www.mamicode.com/new byte[1024];"...."+add+":"+port);
	}

}

  

UDP协议