您的位置:首页 > 理论基础 > 计算机网络

JAVA实现NIO非阻塞TCP通信--客户端

2014-03-12 15:52 465 查看
package com.abkj.platform.person.test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

import org.junit.Test;

import com.abkj.platform.person.comm.command.Command;
import com.abkj.platform.person.comm.command.CommandMain;
import com.abkj.platform.util.PublicTool;

public class TCPClientTest {
private static ByteBuffer sendbuffer;
private static ByteBuffer receivebuffer;
static byte[] sendbuf;
static byte[] receivebuf;

static SocketChannel socketChannel;
static Selector selector;
static SelectionKey selectionKey;

static int i = 0;//分站数组控制

@Test
public void TCPTest() throws Exception {
run();
}

public void run() throws Exception {

Iterator<SelectionKey> iterator;
connect();
while (true) {
Thread.sleep(1000l);
int n = selector.select(500l);
if (n == 0) {
socketChannel.register(selector, SelectionKey.OP_WRITE);
continue;
}
iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
selectionKey = iterator.next();
iterator.remove();
handleKey(selectionKey, sendbuf);
}
}

}

private static void handleKey(SelectionKey selectionKey, byte[] sendbuf)
throws IOException {
byte[] stationid = { 1, 2,3, 31 };
boolean clearposition = true;
boolean clear = false;
int max = 8;
Command command = null;

if (selectionKey.isConnectable()) {
socketChannel = (SocketChannel) selectionKey.channel();
if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
}
socketChannel.register(selector, SelectionKey.OP_WRITE);
} else if (selectionKey.isReadable()) {
socketChannel = (SocketChannel) selectionKey.channel();
receivebuffer = ByteBuffer.allocate(10 * 1024);
receivebuffer.clear();
if (socketChannel.read(receivebuffer) > 0) {
byte[] all = receivebuffer.array();
receivebuf = new byte[all[1]];
System.arraycopy(all, 0, receivebuf, 0, all[1]);
System.out.print("接收: ");
PublicTool.printHexString(receivebuf);
}
socketChannel.register(selector, SelectionKey.OP_WRITE);
} else if (selectionKey.isWritable()) {
socketChannel = (SocketChannel) selectionKey.channel();
if (sendbuffer != null)
sendbuffer.clear();
sendbuf = CommandMain.makeCommandWhole(stationid[i], clear,
clearposition, max, command);
i++;
if (i == stationid.length)i = 0;
System.out.print("发送: ");
PublicTool.printHexString(sendbuf);
sendbuffer = ByteBuffer.wrap(sendbuf);
socketChannel.write(sendbuffer);
socketChannel.register(selector, SelectionKey.OP_READ);
}
}

private static void connect() throws IOException {
InetSocketAddress address = new InetSocketAddress("192.168.1.200",
10000);
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT);
socketChannel.connect(address);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: