JAVA实现NIO非阻塞UDP通信--客户端
2014-03-12 15:52
369 查看
package com.abkj.platform.person.test;
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.Iterator;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.abkj.platform.person.comm.command.Command;
import com.abkj.platform.person.comm.command.CommandMain;
import com.abkj.platform.person.domain.Station;
import com.abkj.platform.person.service.BaseService;
import com.abkj.platform.person.service.ExcuteService;
import com.abkj.platform.util.PublicTool;
public class UDPClientTest {
@Autowired
private static ExcuteService excuteService;
@Autowired
private static BaseService baseService;
static DatagramChannel channel;
static Selector selector;
SocketAddress sa;
static ByteBuffer bytebuffer1;
static ByteBuffer bytebuffer2 = ByteBuffer.allocate(100);
static byte[] stationid = {1,2,3,31};
static boolean clearposition = true;
static boolean clear = false;
static int max = 8;
static Command command = null;
static int i = 0;//分站数组控制
@Test
public void UDPTest() throws Exception {
run();
}
public static void run() {
Iterator<SelectionKey> iterator;
try {
channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(10000));
selector = Selector.open();
channel.register(selector, SelectionKey.OP_WRITE);
while (true) {
Thread.sleep(1000l);
int n = selector.select(500l);
if (n == 0) {
channel.register(selector, SelectionKey.OP_WRITE);
continue;
}
iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
if (key.isValid() && key.isReadable()) {
handleRead(key);
} else if (key.isValid() && key.isWritable()) {
handleWrite(key);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channel.close();
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void handleRead(SelectionKey key) throws IOException {
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]);
System.out.print("接收:");
PublicTool.printHexString(receivebuf);
bytebuffer2.clear();
channel.register(selector, SelectionKey.OP_WRITE);
}
private static void handleWrite(SelectionKey key) throws IOException {
channel = (DatagramChannel) key.channel();
byte[] ret = CommandMain.makeCommandWhole(stationid[i], clear,
clearposition, max, command);
i++;
if (i == stationid.length)i = 0;
bytebuffer1 = ByteBuffer.wrap(ret);
System.out.print("发送:");
PublicTool.printHexString(ret);
channel.send(bytebuffer1, new InetSocketAddress("192.168.1.200",
10000));
key.interestOps(SelectionKey.OP_READ);
}
}
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.Iterator;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.abkj.platform.person.comm.command.Command;
import com.abkj.platform.person.comm.command.CommandMain;
import com.abkj.platform.person.domain.Station;
import com.abkj.platform.person.service.BaseService;
import com.abkj.platform.person.service.ExcuteService;
import com.abkj.platform.util.PublicTool;
public class UDPClientTest {
@Autowired
private static ExcuteService excuteService;
@Autowired
private static BaseService baseService;
static DatagramChannel channel;
static Selector selector;
SocketAddress sa;
static ByteBuffer bytebuffer1;
static ByteBuffer bytebuffer2 = ByteBuffer.allocate(100);
static byte[] stationid = {1,2,3,31};
static boolean clearposition = true;
static boolean clear = false;
static int max = 8;
static Command command = null;
static int i = 0;//分站数组控制
@Test
public void UDPTest() throws Exception {
run();
}
public static void run() {
Iterator<SelectionKey> iterator;
try {
channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(10000));
selector = Selector.open();
channel.register(selector, SelectionKey.OP_WRITE);
while (true) {
Thread.sleep(1000l);
int n = selector.select(500l);
if (n == 0) {
channel.register(selector, SelectionKey.OP_WRITE);
continue;
}
iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
if (key.isValid() && key.isReadable()) {
handleRead(key);
} else if (key.isValid() && key.isWritable()) {
handleWrite(key);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channel.close();
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void handleRead(SelectionKey key) throws IOException {
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]);
System.out.print("接收:");
PublicTool.printHexString(receivebuf);
bytebuffer2.clear();
channel.register(selector, SelectionKey.OP_WRITE);
}
private static void handleWrite(SelectionKey key) throws IOException {
channel = (DatagramChannel) key.channel();
byte[] ret = CommandMain.makeCommandWhole(stationid[i], clear,
clearposition, max, command);
i++;
if (i == stationid.length)i = 0;
bytebuffer1 = ByteBuffer.wrap(ret);
System.out.print("发送:");
PublicTool.printHexString(ret);
channel.send(bytebuffer1, new InetSocketAddress("192.168.1.200",
10000));
key.interestOps(SelectionKey.OP_READ);
}
}
相关文章推荐
- JAVA实现NIO非阻塞TCP通信--客户端
- Java review--NIO实例:实现服务端和客户端的简单通信
- Java基于UDP实现服务器和多客户端之间的通信
- java分布式开发TCP/IP NIO无阻塞 Socket((基于消息方式实现系统间的通信) )(转)
- Java网络编程——使用NIO实现非阻塞Socket通信
- JAVA实现阻塞UDP通信
- Java简单实现UDP服务端和客户端的通信
- Java网络编程——使用NIO实现非阻塞Socket通信
- NIO实现的简单的客户端与服务端通信(非阻塞)
- Java 基于 UDP 实现 Socket中的多客户端通信
- tornado实现高性能无阻塞udp通信(2)——实现异步udp客户端
- Java实现单个客户端与服务器UDP通信
- 基于TCP/UDP的NIO服务端/客户端代码实现damo(java)
- [分布式java]基于JavaAPI实现消息方式的系统间通信:UDP/IP+NIO
- java udp网络编程及实例代码,实现客户端和服务端通信
- java中使用nio包实现非阻塞的UDP通信
- Java 基于 UDP 实现 Socket中的多客户端通信
- java的nio之:java的bio流下实现的socket服务器同步阻塞模型和socket的伪异步的socket服务器的通信模型
- Android编程实现TCP、UDP客户端通信功能示例
- 利用Java实现UDP通信