使用JAVA NIO实现的UDP client和server

时间:2023-02-14 17:12:36

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

client.java

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package udp.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Arrays;
import java.util.Iterator;

import test.PublicTool;

public class UDPClient {
static DatagramChannel channel;
static Selector selector;
SocketAddress sa;
static ByteBuffer byteBuffer1;
static ByteBuffer byteBuffer2 = ByteBuffer.allocate(100);

public static void run() {
try {
channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(10000));
} catch (Exception e) {
e.printStackTrace();
}
try {
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
byte[] ret = { 0x7C, 0x13, 0x1F, 0x47, 0x0E, 0x03, 0x06, 0x0F,
0x13, 0x30, 0x00, 0x08, 0x00, 0x00, 0x00, 0x55,
(byte) 0xAA, 0x70, 0x7A };
byteBuffer1 = ByteBuffer.wrap(ret);
channel.send(byteBuffer1, new InetSocketAddress("192.168.1.200",
10000));
} catch (Exception e) {
e.printStackTrace();
}

int num = 0;
while (num < 2) {
try {
System.out.println("1111");
int n = selector.select();
System.out.println(n);
if (n > 0) {
Iterator iterator = selector.selectedKeys().iterator();
System.out.println(iterator);
while (iterator.hasNext()) {
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
if (key.isReadable()) {
channel = (DatagramChannel) key.channel();
channel.receive(byteBuffer2);
byte[] all = byteBuffer2.array();
byte[] receiveBuf = new byte[all[1]];
System.arraycopy(all, 0, receiveBuf, 0, all[1]);
PublicTool.printHexString(receiveBuf);
byteBuffer2.clear();
if(receiveBuf == null || receiveBuf == new byte[0]);break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
while (true) {
run();
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

server

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package udp.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Arrays;
import java.util.Iterator;

import test.PublicTool;

public class UDPServer
{
DatagramChannel channel;

Selector selector;

public void work()
{
try
{
// 打开一个UDP Channel
channel = DatagramChannel.open();

// 设定为非阻塞通道
channel.configureBlocking(false);
// 绑定端口
channel.socket().bind(new InetSocketAddress(10000));

// 打开一个选择器
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
} catch (Exception e)
{
e.printStackTrace();
}

ByteBuffer byteBuffer = ByteBuffer.allocate(19);
while (true)
{
try
{
// 进行选择
int n = selector.select();
if (n > 0)
{
// 获取以选择的键的集合
Iterator iterator = selector.selectedKeys().iterator();

while (iterator.hasNext())
{
SelectionKey key = (SelectionKey) iterator.next();

// 必须手动删除
iterator.remove();

if (key.isReadable())
{
DatagramChannel datagramChannel = (DatagramChannel) key
.channel();

byteBuffer.clear();
// 读取
InetSocketAddress address = (InetSocketAddress) datagramChannel
.receive(byteBuffer);

PublicTool.printHexString(byteBuffer.array());

// 删除缓冲区中的数据
byteBuffer.clear();
byte[] ret = {0x55, (byte) 0xAA, 0x70, 0x7A };

byteBuffer.put(ret);

byteBuffer.flip();

// 发送数据
datagramChannel.send(byteBuffer, address);
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}

}

public static void main(String[] args)
{
new UDPServer().work();

}

}