本文共 3288 字,大约阅读时间需要 10 分钟。
的client比较简单,下面是一个复杂点的:
- package org.guojje.grizzly;
-
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.UnknownHostException;
- import java.nio.ByteBuffer;
- import java.nio.channels.SelectionKey;
- import java.util.concurrent.CountDownLatch;
- import java.util.logging.Level;
-
- import com.sun.grizzly.CallbackHandler;
- import com.sun.grizzly.Context;
- import com.sun.grizzly.Controller;
- import com.sun.grizzly.ControllerStateListenerAdapter;
- import com.sun.grizzly.IOEvent;
- import com.sun.grizzly.TCPConnectorHandler;
- import com.sun.grizzly.TCPSelectorHandler;
- import com.sun.grizzly.Controller.Protocol;
- import com.sun.grizzly.util.WorkerThreadImpl;
-
- public class Client {
-
- public static void main(String[] args) throws UnknownHostException,
- IOException {
-
- Controller controller = new Controller();
-
-
- TCPSelectorHandler tcpHandler = new TCPSelectorHandler(true);
- controller.setSelectorHandler(tcpHandler);
-
- final CountDownLatch latch = new CountDownLatch(1);
- controller.addStateListener(new ControllerStateListenerAdapter() {
-
- public void onStarted() {
- System.out.println("on started");
- }
-
- public void onReady() {
- System.out.println("on ready");
- latch.countDown();
- }
-
- public void onException(Throwable e) {
- if (latch.getCount() > 0) {
- Controller.logger().log(Level.SEVERE,
- "Exception during " + "starting the controller", e);
- latch.countDown();
- } else {
- Controller.logger().log(Level.SEVERE,
- "Exception during " + "controller processing", e);
- }
- }
- });
-
-
- new WorkerThreadImpl("client_work", controller).start();
-
- try {
- latch.await();
- } catch (InterruptedException e1) {
-
- }
-
- if (!controller.isStarted()) {
- throw new IllegalStateException("Controller is not started!");
- }
-
- final CountDownLatch waitLatch = new CountDownLatch(1);
-
- final TCPConnectorHandler tch = (TCPConnectorHandler) controller
- .acquireConnectorHandler(Protocol.TCP);
- tch.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1900),
- new CallbackHandler<Context>() {
-
- public void onConnect(IOEvent<Context> ioEvent) {
- try {
- SelectionKey k = ioEvent.attachment()
- .getSelectionKey();
- try {
- tch.finishConnect(k);
-
- } catch (java.io.IOException ex) {
-
- return;
- } catch (Throwable ex) {
-
- return;
- }
-
- ioEvent.attachment().getSelectorHandler().register(
- k, SelectionKey.OP_READ);
-
- } finally {
- if (waitLatch != null) {
- waitLatch.countDown();
- }
- }
- }
-
- public void onRead(IOEvent<Context> ioEvent) {
- ByteBuffer bb = ByteBuffer.allocate(100);
- try {
- tch.read(bb, false);
- } catch (IOException e) {
- e.printStackTrace();
- }
- bb.flip();
- System.out.println(new String(bb.array(), 0, bb
- .remaining()));
-
- SelectionKey k = ioEvent.attachment().getSelectionKey();
-
- ioEvent.attachment().getSelectionKey().interestOps(
- k.interestOps() | SelectionKey.OP_READ);
- }
-
- public void onWrite(IOEvent<Context> ioEvent) {
- }
- });
-
- System.out.println("current connection:" + tch.getUnderlyingChannel());
-
- try {
- waitLatch.await();
- } catch (Exception e) {
-
- }
-
- ByteBuffer bb = ByteBuffer.wrap("xxxx".getBytes());
- tch.write(bb, true);
- }
- }
转载地址:http://zndta.baihongyu.com/