xieegai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

UDP是用户数据包协议,而数据报是不需要通道即可传递,所以UDP是无连接协议,具有不稳定,但是效率高的特点。

UDP协议传输数据时没有客户端和服务端之分,只有接收方和发送方,双方采取的接收和发送方案是一致的的,只是有顺序上的区别。双方都会创建DatagramPocket对象来打包和解析数据(包括数据包内容、数据长度、目标IP、目标端口等),也都会创建DatagramSocket对象(“套接字”)发送和接收DatagramPocket数据包。

基本步骤:创建DatagramPocket对象打包数据--->创建DatagramSocket对象send数据包--->创建空DatagramPocket对象准备接收数据--->DatagramSocket对象receive数据包--->DatagramSocket对象解析数据

package udp;

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

/**
 * 消息接收方
 * @date 2018年1月27日 下午7:07:44
 */
public class Receiver {

    public static void main(String[] args) {
        DatagramPacket dp = null;
        DatagramSocket ds = null;
        DatagramPacket dpto = null;
        try {
            // 创建DatagramPacket对象,准备接收数据包
            byte[] buf = new byte[1024];
            dp = new DatagramPacket(buf, buf.length);
            // 创建DatagramSocket对象,用于接收数据
            ds = new DatagramSocket(8000);
            ds.receive(dp);
            // 显示接收到的信息
            String info = new String(dp.getData(), 0, dp.getLength());
            System.out.println("收到" + dp.getAddress().getHostAddress() + "的信息是:" + info);
            
            // 回复
            String reply = "你好,欢迎登陆";
            System.out.println("接收方的回复是:" + reply);
            
            // 创建DatagramSocket对象,封装数据
            SocketAddress sa = dp.getSocketAddress();
            dpto = new DatagramPacket(reply.getBytes(), reply.getBytes().length, sa);
            
            // 发送信息
            ds.send(dpto);
            
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ds.close();
        }
    }
}
package udp;

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

/**
 * 消息发送方
 * @date 2018年1月27日 下午7:25:57
 */
public class Sender {
    public static void main(String[] args) {
        InetAddress ia = null;
        DatagramPacket dp = null;
        DatagramSocket ds = null;
        DatagramPacket dpre = null;
        try {
            // 要发送的信息
            String info = "你好,我是小A";
            System.out.println("发送方要发送的信息是:" + info);
            
            // 获取本地主机地址
            ia = InetAddress.getByName("localhost");
            
            //创建DatagramPacket对象,封装数据
            dp = new DatagramPacket(info.getBytes(), info.getBytes().length, ia, 8000);
            
            // 创建DatagramSocket对象,发送消息
            ds = new DatagramSocket();
            ds.send(dp);
            
            // 创建DatagramPacket对象,准备解析数据
            byte[] buf = new byte[1024];
            dpre = new DatagramPacket(buf, buf.length);
            
            // 接收数据
            ds.receive(dpre);
            
            // 显示接收到的数据
            String reply = new String(dpre.getData(), 0, dpre.getLength());
            System.out.println("收到接收方" + dpre.getAddress().getHostAddress() + "的反馈信息是" + reply);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ds.close();
        }
    }
}

 

posted on 2018-01-27 20:04  xieegai  阅读(372)  评论(0编辑  收藏  举报