2 loki8888 loki8888 于 2016.03.17 11:55 提问

mina客户端(SerialConnector)实现串口通信,结果不能与server端通信

代码如下:
public class MinaClient {

public static void main(String []args)throws Exception{

    //Create TCP/IP connection

// NioSocketConnector connector = new NioSocketConnector();
IoConnector connector = new SerialConnector();
//创建接受数据的过滤器
DefaultIoFilterChainBuilder chain = connector.getFilterChain();

    //设定这个过滤器将一行一行(/r/n)的读取数据
    chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

    //绑定处理handler
    connector.setHandler(new MinaClientHandler());
    //客户端的消息处理器:一个SamplMinaServerHander对象

// connector.setHandler(new MinaClientHandler());

    //set connect timeout
    connector.setConnectTimeout(30);
    SerialAddress address = new SerialAddress
            ("COM3", 9600, SerialAddress.DataBits.DATABITS_8, SerialAddress.StopBits.BITS_1, SerialAddress.Parity.NONE, SerialAddress.FlowControl.NONE);

    ConnectFuture cf = connector.connect(address);

    //Wait for the connection attempt to be finished.
    cf.awaitUninterruptibly();

// cf.getSession().getCloseFuture().awaitUninterruptibly(1000);
// //连接到服务器:
// ConnectFuture cf1 = connector.connect(new InetSocketAddress("localhost",8888));
// cf1.awaitUninterruptibly();
//
// cf1.getSession().getCloseFuture().awaitUninterruptibly(1000);

    connector.dispose();
}
    这是客户端代码,如果在客户端获取串口的数据,并新建一个客户端,与server 端产生通信??

1个回答

loki8888
loki8888   2016.03.17 11:58

补充
public class MinaClientHandler extends IoHandlerAdapter {
// 当客户端连接进入时
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("incomming 客户端: " + session.getRemoteAddress());
}

@Override
public void exceptionCaught(IoSession session, Throwable cause)
        throws Exception {
    System.out.println("客户端发送信息异常....");
}

// 当客户端发送消息到达时
@Override
public void messageReceived(IoSession session, Object message)
        throws Exception {

    System.out.println("服务器返回的数据:" + message.toString());
}

    在Client handler的messageReceived是可以获得数据的,问如何在client端获取数据并新启线程产生soket通信?
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!