一.UDP网络编程
1.面向无连接的数据传输,不可靠的,但效率高(音频,视频等).
2.UDP一次发送的数据不能超过64kb.
3.UDP编程所需要的类
(1)DatagramSocket此类表示用来发送和接收数据报包的套接字
(2)DatagramPacket此类表示数据报包
方法:DatagramPacket(byte[] buf, int length, InetAddress address, int port)
参数表示:buf - 包数据
length - 包长度
address - 目的地址
port - 目的端口
4.DatagramSocket常用方法
(1)构造方法:public DatagramSocket(int port)
(2)常用方法:#public void send(DatagramPacket p)
#public void receive(DatagramPacket p)
5.DatagramPacket常用方法
(1)构造方法:public DatagramPacket(byte[] buf,int length)此构造方法用来接收
数据
(2)public DatagramPacket(byte[] buf, int length, InetAddress address, int port) 此构造方法用来组装将要发送的数据
(3)public InetAddress getAddress():返回将要发送ign方的IP或者接收到的数据的IP
(4)public byte[] getData():返回将要发送的数据缓冲区或已经接收到的数据缓冲区
(5)public int getLength():返回要发送的数据长度或接收到的数据长度
6.基于TCP与UDP协议的网络编程(区别):
(1)基于TCP协议的Socket编程:#.通信双发需要建立连接;#.连接建立时双方存在主次之分
(2)基于UDP协议的Socket编程:#.通信双发不需要建立连接;#.通信双方完全平等
7.基于UDP协议的Socket编程
(1)UDP Socket通信模型:
例(UDP示例):
信息发送线程类:
package udp;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 SendRunnable implements Runnable{ private String destHost; private int port; private DatagramSocket ds; public SendRunnable(String destHost, int port, DatagramSocket ds) { this.destHost = destHost; this.port = port; this.ds = ds; } @Override public void run() { while(true){ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print(Thread.currentThread().getName()+":"); String say; DatagramPacket dp=null; try { say=br.readLine(); dp=new DatagramPacket(say.getBytes(),say.getBytes().length ,InetAddress.getByName(destHost), port); ds.send(dp); } catch (IOException e) { e.printStackTrace(); } } }}
信息接收线程类:
package udp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;/*接收线程类 */public class ReceiveRunnable implements Runnable { private DatagramSocket ds; public ReceiveRunnable(DatagramSocket ds) { this.ds = ds; } @Override public void run() { while (true) { byte[] b = new byte[1024]; DatagramPacket dp = new DatagramPacket(b, b.length); try { ds.receive(dp);// 接收之前一直阻塞 String ip = dp.getAddress().getHostName();// 获取发送方的IP int post = dp.getPort();// 获取发送方的端口号 System.out.println(ip + ":" + post + "说:" + new String(dp.getData(), 0, dp.getLength())); } catch (IOException e) { e.printStackTrace(); } } }}
聊天端口一:
package udp;import java.net.DatagramSocket;import java.net.SocketException;class Chat1 { public static void main(String[] args) throws SocketException { DatagramSocket ds=new DatagramSocket(9999); new Thread(new ReceiveRunnable(ds)).start(); new Thread(new SendRunnable("localhost", 8888, ds),"白雪公主").start(); }}
聊天端口二:
package udp;import java.net.DatagramSocket;import java.net.SocketException;public class Chat2 { public static void main(String[] args) throws SocketException { DatagramSocket ds=new DatagramSocket(8888); new Thread(new ReceiveRunnable(ds)).start(); new Thread(new SendRunnable("localhost", 9999, ds),"青蛙王子").start(); }
}
运行结果
白雪公主:127.0.0.1:8888说:你好,公主
你好,王子
白雪公主:127.0.0.1:8888说:你在干什摸,公主
我在和你聊天
白雪公主:
:
青蛙王子:你好,公主
青蛙王子:127.0.0.1:9999说:你好,王子
你在干什摸,公主
青蛙王子:127.0.0.1:9999说:我在和你聊天