UDP--User Datagram Protocol,用户数据报协议.是一个无连接的协议,以数据报文单位进行数据传输,每个数据报是一个独立信息,其中包括目标主机IP地址和端口.
主要用到了DatagramSocket类和DatagramPacket类来实现
UDP特点:
1)将数据及源和目的封装成数据包中,不需要建立连接
2)每个数据报的大小在限制在64k内
3)因无连接,是不可靠协议
4)不需要建立连接,速度快
实现步骤:
1)DatagramSocket与DatagramPacket
2)建立发送端,接收端。
3)建立数据包。
4)调用Socket的发送接收方法。
5)关闭Socket。
发送端与接收端是两个独立的运行程序。
☆发送端
在发送端,要在数据包对象中明确目的地IP及端口。
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();
☆接收端
在接收端,要指定监听的端口。
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();
实战 练习:UDP聊天程序
1)通过键盘录入获取要发送的信息。
2)将发送和接收分别封装到两个线程中。
路人甲:
package cn.hucu.UDP.udp.chat.one;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPChat {
public static void main(String[] args) throws IOException {
//1创建ds--socket
//如果空参数,由系统随便给我们分配一个端口号发送--凡是socket通讯都要有端口号
DatagramSocket send = new DatagramSocket(10003);
DatagramSocket receive = new DatagramSocket(10004);
new Thread(new Send(send)).start(); //使用线程
new Thread(new Receive(receive)).start();
}
}
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
// 源:System.in
// 目的:socket + dp
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
//2创建dp--数据包
byte buf[] = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("127.0.0.1"), 10002); // 改成对方IP
//3发送 ---选用socket把数据包发送出去
ds.send(dp);
if ("over".equalsIgnoreCase("line")) {
break;
}
}
//4关流
ds.close();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(ds!=null){
ds.close();
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
throw new RuntimeException("关流失败,很危险!");
}
}
}
}
}
class Receive implements Runnable {
private DatagramSocket ds;
public Receive(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
byte buf[] = new byte[512];
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
// 解析数据包
//4从dp中把数据解析出来----获取对方的ip
String ip = dp.getAddress().getHostAddress();// 记录发送方的IP地址
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + "说:" + str);
if ("Over".equalsIgnoreCase(str)) {
System.out.println(ip + "离开聊天室....");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
路人乙:
package cn.hucu.UDP.udp.chat.two;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPChat {
public static void main(String[] args) throws IOException {
DatagramSocket send = new DatagramSocket(10001);
DatagramSocket receive = new DatagramSocket(10002);
new Thread(new Send(send)).start();
new Thread(new Receive(receive)).start();
}
}
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
byte buf[] = new byte[512];
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("192.168.0.1"), 10004); //IP要改成对方的IP
ds.send(dp);
if ("voer".equalsIgnoreCase("line")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
throw new RuntimeException("关流失败!!");
}
if (ds != null) {
ds.close();
}
}
}
}
}
class Receive implements Runnable {
private DatagramSocket ds;
public Receive(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
byte buf[] = new byte[512];
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
// 解析数据包
String ip = dp.getAddress().getHostAddress();
// int port = dp.getPort(); 获取端口号
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + "说:" + str);
if ("Over".equalsIgnoreCase(str)) {
System.out.println(ip + "离开聊天室....");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这里应该是不够完善的,不过会再改进!!!!!