1, 知识点介绍:
UDP:将数据及源和目的封装成数据包中,不需要建立连接每个数据报的大小在限制在64k内因无连接,是不可靠协议不需要建立连接,速度快。
UDP传输需要这些条件:
DatagramSocket与DatagramPacket
建立发送端,接收端。
建立数据包。
调用Socket的发送接收方法。
关闭Socket。
发送端与接收端是两个独立的运行程序。
发送端:
在我们编程中,我们需要明确的ip地址和端口号
<span style="font-size:24px;">DatagramSocket ds = new DatagramSocket(); byte[] by = “hello,udp”.getBytes(); DatagramPacket dp = new DatagramPacket(by,0,by.length, InetAddress.getByName(“127.0.0.1”),10000); ds.send(dp); ds.close(); </span>
接收端:
在接收端,要指定监听的端口,就是DatagramSocket(里面的端口号);
DatagramSocket ds = new DatagramSocket(10000); byte[] by = new byte[1024]; DatagramPacket dp = new DatagramPacket(by,by.length); ds.receive(dp); String str = new String(dp.getData(),0,dp.getLength()); System.out.println(str+"--"+dp.getAddress()); ds.close();2, 我们 做了一个简单(UDP)的聊天程序
只要ip地址和端口号对应上,我们就能通过DatagramSocket,DatagramPacket,这两个类来发送和接收信息。
通过键盘录入获取要发送的信息。
将发送和接收分别封装到两个线程中。主函数1
其实这两个主函数1,2都是一样的,不同的端口号和ip(因为在一台电脑上测试,所有ip也是一样的)
/** * */ package SampleDemo.UDP.chat.other; import java.net.DatagramSocket; import java.net.SocketException; /** * @author xinxin * */ public class UdpDemo2 { public static void main(String[] args) { try { DatagramSocket send2 =new DatagramSocket(9993);//不同的端口号记性匹配 DatagramSocket receive2 =new DatagramSocket(9994); new Thread(new send(send2)).start();//线程启动 new Thread(new receive(receive2)).start(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package SampleDemo.UDP.chat.other; 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 send implements Runnable { DatagramSocket ds =null; DatagramPacket dp =null; public send(DatagramSocket ds) { this.ds = ds; } @Override public void run() { BufferedReader br=null; try { br =new BufferedReader(new InputStreamReader(System.in)); String str=null; while((str=br.readLine())!=null){ byte[] buf=str.getBytes(); dp=new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.31.158"), 9992); ds.send(dp); if("over".equalsIgnoreCase(str)){ break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
主函数1的接收方
package SampleDemo.UDP.chat.other; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class receive implements Runnable { DatagramSocket ds =null; DatagramPacket dp=null; public receive(DatagramSocket ds) { this.ds = ds; } @Override public void run() { try { while(true){ byte[] buf=new byte[1024]; dp=new DatagramPacket(buf, buf.length); ds.receive(dp); String id =dp.getAddress().getHostAddress();//获得ip 要负责任 int port=dp.getPort();//获得端口号 String str =new String(dp.getData(), 0, dp.getLength()); if("over".equalsIgnoreCase(str)){ System.out.println("对方离开了聊天室"); break; } System.out.println("ip:--"+id+",端口:"+port+"::"+str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ ds.close(); } } }
主函数2
/** * */ package SampleDemo.UDP.chat; import java.net.DatagramSocket; import java.net.SocketException; /** * @author xinxin * */ public class UDPdemo { /** * @param args */ public static void main(String[] args) { try { DatagramSocket send1=new DatagramSocket(9991); DatagramSocket receive1=new DatagramSocket(9992); new Thread(new send(send1)).start(); new Thread(new receive(receive1)).start(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
主函数2的发送方
/** * */ package SampleDemo.UDP.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.UnknownHostException; /** * @author xinxin * */ public class send implements Runnable { DatagramSocket ds =null; public send(DatagramSocket ds) { this.ds = ds; } @Override public void run() { //从键盘收字符 BufferedReader br=null; try { br =new BufferedReader(new InputStreamReader(System.in)); String str =""; while((str=br.readLine())!=null){ byte[] buf=str.getBytes();//接收到数据之后变成字符数组封装于包 DatagramPacket dp =new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.31.158"), 9994); ds.send(dp); if("over".equalsIgnoreCase(str)){ break; } } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }主函数的接收方
package SampleDemo.UDP.chat; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class receive implements Runnable { DatagramSocket ds=null; public receive(DatagramSocket ds) { this.ds = ds; } @Override public void run() { byte[] buf =new byte[1024]; DatagramPacket dp =new DatagramPacket(buf, buf.length); while(true){ try { ds.receive(dp); //收到之后,然后从包里面获取所有的数据 String id=dp.getAddress().getHostAddress();//必须获得id,不然会负责法律责任 int port=dp.getPort(); String str=new String(dp.getData(), 0, dp.getLength()); if("over".equalsIgnoreCase(str)){ System.out.println("对方离开了聊天室"); break; } System.out.println("id="+id+",port"+port+",接收到的内容----"+str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } }