【NIO】DatagramChannel

前言

Github:https://github.com/yihonglei/jdk-source-code-reading(java-nio)

一 DatagramChannel概述

Java NIO中的DatagramChannel定义在java.nio.channels包中,是一个能收发UDP包的通道。

因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入。它发送和接收的是数据包。

二 打开DatagramChannel

如下是打开DatagramChannel的方式:

DatagramChannel channel = DatagramChannel.open(); // 获取通道
channel.socket().bind(new InetSocketAddress(8989)); // 绑定端口

这个例子打开的DatagramChannel可以在UDP端口8989上接收数据包。

三 接收数据

通过receive()方法从DatagramChannel接收数据,如:

ByteBuffer buffer = ByteBuffer.allocate(48); // 分配Buffer

buffer.clear(); // 清空Buffer

SocketAddress socketAddress = datagramChannel.receive(buffer); // 接受客户端发送数据

receive()方法会将接收到的数据包内容复制到指定的Buffer。如果Buffer容不下收到的数据,多出的数据将被丢弃。


四 发送数据

通过send()方法从DatagramChannel发送数据,如:

DatagramChannel channel = DatagramChannel.open(); // 获取通道

String newData = "写入文件数据测试" + System.currentTimeMillis(); // 将要发送的数据

ByteBuffer buffer = ByteBuffer.allocate(48); // 缓冲区分配

buffer.clear(); // 清空缓冲区

buffer.put(mes.getBytes("UTF-8")); // 将数据写入缓冲区

buffer.flip(); // 切换数据模式

int bytesSent = channel.send(buffer, new InetSocketAddress(ip, 80)); // 发送数据到ip服务,80端口(port)

这个例子发送一串字符到ip服务器的UDP端口80。因为服务端并没有监控这个端口,所以什么也不会发生。

也不会通知你发出的数据包是否已收到,因为UDP在数据传送方面没有任何保证。

五 连接到特定的地址

可以将DatagramChannel"连接"到网络中的特定地址的。由于UDP是无连接的,连接到特定地址并不会像TCP通道那样

创建一个真正的连接。而是锁住DatagramChannel,让其只能从特定地址收发数据。示例:

channel.connect(new InetSocketAddress(ip, 80));

当连接后,也可以使用read()和write()方法,就像在用传统的通道一样。只是在数据传送方面没有任何保证。示例:

int bytesRead = channel.read(buffer);

int bytesWritten = channel.write(buffer);

六 完整实例

服务端: 接收客户端发送消息,收取到消息后,给发送方一个回应

package com.jpeony.nio.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

/**
 * 服务端: 接收客户端发送消息,收取到消息后,给发送方一个回应
 *
 * @author yihonglei
 */
public class DatagramChannelReceiveTest {
    public static void main(String[] args) throws IOException {
        // 获取通道
        DatagramChannel datagramChannel = DatagramChannel.open();
        // 绑定端口
        datagramChannel.bind(new InetSocketAddress(8989));
        // 分配Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        byte b[];
        while (true) {
            // 清空Buffer
            buffer.clear();
            // 接受客户端发送数据
            SocketAddress socketAddress = datagramChannel.receive(buffer);
            if (socketAddress != null) {
                int position = buffer.position();
                b = new byte[position];
                buffer.flip();
                for (int i = 0; i < position; ++i) {
                    b[i] = buffer.get();
                }
                System.out.println("receive remote " + socketAddress.toString() + ":" + new String(b, "UTF-8"));
                //接收到消息后给发送方回应
                sendReback(socketAddress, datagramChannel);
            }
        }
    }

    public static void sendReback(SocketAddress socketAddress, DatagramChannel datagramChannel) throws IOException {
        String message = "I has receive your message";
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(message.getBytes("UTF-8"));
        buffer.flip();
        datagramChannel.send(buffer, socketAddress);
    }
}

客户端: 发送控制台输入的内容并接收服务端回应

package com.jpeony.nio.channel;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Scanner;

/**
 * 客户端: 发送控制台输入的内容并接收服务端回应
 *
 * @author yihonglei
 */
public class DatagramChannelSendTest {
    public static void main(String[] args) throws IOException {

        final DatagramChannel channel = DatagramChannel.open();
        //接收消息线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                byte b[];
                while (true) {
                    buffer.clear();
                    SocketAddress socketAddress = null;
                    try {
                        socketAddress = channel.receive(buffer);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (socketAddress != null) {
                        int position = buffer.position();
                        b = new byte[position];
                        buffer.flip();
                        for (int i = 0; i < position; ++i) {
                            b[i] = buffer.get();
                        }
                        try {
                            System.out.println("receive remote " + socketAddress.toString() + ":" + new String(b, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
        ;

        //发送控制台输入消息
        while (true) {
            Scanner sc = new Scanner(System.in);
            String next = sc.next();
            try {
                sendMessage(channel, next);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void sendMessage(DatagramChannel channel, String mes) throws IOException {
        if (mes == null || mes.isEmpty()) {
            return;
        }
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.clear();
        buffer.put(mes.getBytes("UTF-8"));
        buffer.flip();
        System.out.println("send msg:" + mes);
        int send = channel.send(buffer, new InetSocketAddress("localhost", 8989));
    }
}

运行服务端程序,然后再运行客户端程序,在客户端控制台输入数据,按回车键发送,在服务端控制台可以收到发送的数据。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值