码迷,mamicode.com
首页 > 编程语言 > 详细

使用JAVA NIO实现的UDP client和server

时间:2014-12-12 16:34:22      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   使用   sp   java   on   数据   bs   

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

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();

}

}

 

使用JAVA NIO实现的UDP client和server

标签:io   ar   os   使用   sp   java   on   数据   bs   

原文地址:http://www.cnblogs.com/qingchen1984/p/4159767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!