`
Donald_Draper
  • 浏览: 985475 次
社区版块
存档分类
最新评论

Netty UDT网络通信示例

阅读更多
netty 网络通信示例一 :http://donald-draper.iteye.com/blog/2383326
netty 网络通信示例二:http://donald-draper.iteye.com/blog/2383328
netty 网络通信示例三:http://donald-draper.iteye.com/blog/2383392
netty 网络通信示例四:http://donald-draper.iteye.com/blog/2383472
Netty 构建HTTP服务器示例:http://donald-draper.iteye.com/blog/2383527
     前面的文章我们看了Netty tcp,http协议通信实例,大部分项目用Netty一般用TCP协议,很少用UDP协议,官方文档中,UDP协议的实例都很少,我们在实例可以看到一个UDT的实例包,UDT协议是基于UDP的数据传输协议(UDP-based Data Transfer Protocol,简称UDT),是一种互联网数据传输协议。UDT的主要目的是支持高速广域网上的海量数据传输,而互联网上的标准数据传输协议TCP在高带宽长距离网络上性能很差。 顾名思义,UDT建于UDP之上,并引入新的拥塞控制和数据可靠性控制机制。UDT是面向连接的双向的应用层协议。它同时支持可靠的数据流传输和部分可靠的数据报传输。 由于UDT完全在UDP上实现,它也可以应用在除了高速数据传输之外的其它应用领域,例如点到点技术(P2P),防火墙穿透,多媒体数据传输等等。
    关于UDT协议,可以参见以下这个一些的文章:
UDT协议-基于UDP的可靠数据传输协议的实现分析(1)-准备工作:http://jimmee.iteye.com/blog/2037451,以后再认真研究一下UDT协议。
     在netty实例中,UDT实例有UDT Byte Stream(TCP-like byte streaming mode) ,UDT Message Flow(UDP-like message delivery mode)及相关的对等Peer通信,测试结果UDT Message Flow类型的实例存在bug, see #https://github.com/netty/netty/issues/6934.  Netty官方显示UDT将被丢弃,不在维护。所以我们只看下UDT Byte Stream的实例及Peer通信:
服务端:
package netty.main.udt.bytes;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;
import netty.handler.udt.bytes.ByteEchoServerHandler;

import java.net.InetSocketAddress;
import java.util.concurrent.ThreadFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * UDT Byte Stream Server
 * use UDT in TCP-like byte streaming mode
 * @author donald
 * 2017年7月1日
 * 下午4:11:20
 */
public final class ByteEchoServer {
	private static final Logger log = LoggerFactory.getLogger(ByteEchoServer.class);
	public static final String ip = System.getProperty("host", "192.168.31.153");
    static final int port = Integer.parseInt(System.getProperty("port", "10020"));
    @SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception {
        final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
        final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
        final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER);
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER);

        // Configure the server.
        try {
            final ServerBootstrap boot = new ServerBootstrap();
            boot.group(acceptGroup, connectGroup)
                    .channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
                    .option(ChannelOption.SO_BACKLOG, 10)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new ByteEchoServerHandler());
                        }
                    });
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip,port);
            // Start the server.
            final ChannelFuture future = boot.bind(inetSocketAddress).sync();
            log.info("=========UDT Server is start=========");
            // Wait until the server socket is closed.
            future.channel().closeFuture().sync();
        } finally {
            // Shut down all event loops to terminate all threads.
            acceptGroup.shutdownGracefully();
            connectGroup.shutdownGracefully();
        }
    }
}

服务端处理器:
package netty.handler.udt.bytes;

import io.netty.channel.ChannelHandler.Sharable;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.udt.nio.NioUdtProvider;

/**
 * 
 * @author donald
 * 2017年7月1日
 * 下午4:53:46
 */
@Sharable
public class ByteEchoServerHandler extends ChannelInboundHandlerAdapter {
	private static final Logger log = LoggerFactory.getLogger(ByteEchoServerHandler.class);

	@SuppressWarnings("deprecation")
	@Override
	public void channelActive(final ChannelHandlerContext ctx) {
		 log.info("ECHO active " +
		 NioUdtProvider.socketUDT(ctx.channel()).toStringOptions());
	}

	@Override
	public void channelRead(final ChannelHandlerContext ctx, Object msg) {
		ByteBuf in = (ByteBuf)msg;
    	byte[] bytes = new byte[in.writerIndex()];
    	in.readBytes(bytes);
    	//针对堆buf,direct buf不支持
//    	byte[] bytes = in.array();
    	String message = null;
		try {
			message = new String(bytes,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	try{
    		log.info("===Server reciever message:" +message);
    	}
    	finally{
    		//如果msg为引用计数对象,在使用后注意释放,一般在通道handler中释放
//            ReferenceCountUtil.release(msg);
    	}
    	String ackMessage = "hello client ...";
    	in.clear();
    	try {
			in.writeBytes(ackMessage.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	ctx.write(in);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
		cause.printStackTrace();
		ctx.close();
	}
}

客户端:
package netty.main.udt.bytes;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;
import netty.handler.udt.bytes.ByteEchoClientHandler;

import java.net.InetSocketAddress;
import java.util.concurrent.ThreadFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * UDT Byte Stream Client
 * use UDT in TCP-like byte streaming mode
 * Sends one message when a connection is open and echoes back any received data
 * to the server. Simply put, the echo client initiates the ping-pong traffic
 * between the echo client and server by sending the first message to the
 * server.
 * @author donald
 * 2017年7月1日
 * 下午4:11:20
 */
public final class ByteEchoClient {
	private static final Logger log = LoggerFactory.getLogger(ByteEchoClient.class);
    public static final String ip = System.getProperty("host", "192.168.31.153");
    static final int port = Integer.parseInt(System.getProperty("port", "10020"));
    public static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

    @SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception {
        // Configure the client.
        final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
                connectFactory, NioUdtProvider.BYTE_PROVIDER);
        try {
            final Bootstrap boot = new Bootstrap();
            boot.group(connectGroup)
                    .channelFactory(NioUdtProvider.BYTE_CONNECTOR)
                    .handler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new ByteEchoClientHandler());
                        }
                    });
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip,port);
            // Start the client.
            final ChannelFuture f = boot.connect(inetSocketAddress).sync();
            log.info("=========UDT Client is start=========");
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down the event loop to terminate all threads.
            connectGroup.shutdownGracefully();
        }
    }
}

客户端处理器:
package netty.handler.udt.bytes;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.udt.nio.NioUdtProvider;
import netty.main.udt.bytes.ByteEchoClient;

/**
 * Handler implementation for the echo client. It initiates the ping-pong
 * traffic between the echo client and server by sending the first message to
 * the server on activation.
 * @author donald
 * 2017年7月1日
 * 下午4:53:59
 */
public class ByteEchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
	private static final Logger log = LoggerFactory.getLogger(ByteEchoClientHandler.class);
	private final ByteBuf message;
	public ByteEchoClientHandler() {
		super(false);
		String hello = "Hello peer...";
		message = Unpooled.buffer(ByteEchoClient.SIZE);//堆buffer
        try {
        	message.writeBytes(hello.getBytes("UTF-8"));
        	message.retainedDuplicate();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public void channelActive(final ChannelHandlerContext ctx) {
		log.info("ECHO active " +
		 NioUdtProvider.socketUDT(ctx.channel()).toStringOptions());
		ctx.writeAndFlush(message);
	}

	@Override
	public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
		ByteBuf in = (ByteBuf)msg;
    	byte[] bytes = new byte[in.writerIndex()];
    	in.readBytes(bytes);
    	//针对堆buf,direct buf不支持
//    	byte[] bytes = in.array();
    	String message = null;
		try {
			message = new String(bytes,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	log.info("=== reciever ack message from peer:" +message);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) {
//		ctx.flush();
	}

	@Override
	public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
		cause.printStackTrace();
		ctx.close();
	}

}

启动服务端与客户端,控制台输出:
服务端:
[INFO ] 2017-07-06 22:50:43 com.barchart.udt.SocketUDT library location : ./lib/bin
[INFO ] 2017-07-06 22:50:43 com.barchart.udt.SocketUDT loader provider  : com.barchart.udt.lib.LibraryLoaderUDT
[INFO ] 2017-07-06 22:50:43 com.barchart.udt.lib.PluginPropsUDT ARCH/OS/LINK = amd64/Windows/gpp
[INFO ] 2017-07-06 22:50:43 com.barchart.udt.lib.LibraryLoaderUDT Platform supported.
[INFO ] 2017-07-06 22:50:43 com.barchart.udt.lib.LibraryLoaderUDT Loading release libraries.
[INFO ] 2017-07-06 22:50:43 com.barchart.udt.lib.LibraryLoaderUDT Release libraries loaded.
[INFO ] 2017-07-06 22:50:44 io.netty.handler.logging.LoggingHandler [id: 0xf9f14d4c] REGISTERED
[INFO ] 2017-07-06 22:50:44 io.netty.handler.logging.LoggingHandler [id: 0xf9f14d4c] BIND: /192.168.31.153:10020
[INFO ] 2017-07-06 22:50:44 netty.main.udt.bytes.ByteEchoServer =========UDT Server is start=========
[INFO ] 2017-07-06 22:50:44 io.netty.handler.logging.LoggingHandler [id: 0xf9f14d4c, L:/192.168.31.153:10020] ACTIVE
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf9f14d4c, L:/192.168.31.153:10020] READ: [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767]
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf9f14d4c, L:/192.168.31.153:10020] READ COMPLETE
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767] REGISTERED
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767] ACTIVE
[INFO ] 2017-07-06 22:50:54 netty.handler.udt.bytes.ByteEchoServerHandler ECHO active
[id: 0x04f589e8]
0) Maximum_Transfer_Unit = 1,500
1) Is_Send_Synchronous = false
2) Is_Receive_Synchronous = false
3) Custom_Congestion_Control = null
4) Flight_Window_Size = 25,600 (25 K)
5) Protocol_Send_Buffer_Size = 12,058,624
6) Protocol_Receive_Buffer_Size = 12,058,624
7) Time_To_Linger_On_Close = 180
8) System_Send_Buffer_Size = 65,536
9) System_Receive_Buffer_Size = 12,288,000
12) Is_Randezvous_Connect_Enabled = false
13) Send_Timeout = -1
14) Receive_Timeout = -1
15) Is_Address_Reuse_Enabled = true
16) Maximum_Bandwidth = -1
17) Status_Code = 5
18) Epoll_Event_Mask = 4
19) Send_Buffer_Consumed = 0
20) Receive_Buffer_Available = 0
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767] READ: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 20 70 65 65 72 2e 2e 2e          |Hello peer...   |
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:50:54 netty.handler.udt.bytes.ByteEchoServerHandler ===Server reciever message:Hello peer...
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767] WRITE: 16B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 20 63 6c 69 65 6e 74 20 2e 2e 2e |hello client ...|
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767] READ COMPLETE
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xf515214b, L:/192.168.31.153:10020 - R:/192.168.31.153:53767] FLUSH

客户端:

[INFO ] 2017-07-06 22:50:53 com.barchart.udt.SocketUDT library location : ./lib/bin
[INFO ] 2017-07-06 22:50:53 com.barchart.udt.SocketUDT loader provider  : com.barchart.udt.lib.LibraryLoaderUDT
[INFO ] 2017-07-06 22:50:53 com.barchart.udt.lib.PluginPropsUDT ARCH/OS/LINK = amd64/Windows/gpp
[INFO ] 2017-07-06 22:50:53 com.barchart.udt.lib.LibraryLoaderUDT Platform supported.
[INFO ] 2017-07-06 22:50:53 com.barchart.udt.lib.LibraryLoaderUDT Loading release libraries.
[INFO ] 2017-07-06 22:50:53 com.barchart.udt.lib.LibraryLoaderUDT Release libraries loaded.
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55] REGISTERED
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55] CONNECT: /192.168.31.153:10020
[INFO ] 2017-07-06 22:50:54 netty.main.udt.bytes.ByteEchoClient =========UDT Client is start=========
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55, L:/192.168.31.153:53767 - R:/192.168.31.153:10020] ACTIVE
[INFO ] 2017-07-06 22:50:54 netty.handler.udt.bytes.ByteEchoClientHandler ECHO active
[id: 0x0d729ae4]
0) Maximum_Transfer_Unit = 1,500
1) Is_Send_Synchronous = false
2) Is_Receive_Synchronous = false
3) Custom_Congestion_Control = null
4) Flight_Window_Size = 25,600 (25 K)
5) Protocol_Send_Buffer_Size = 10,485,056
6) Protocol_Receive_Buffer_Size = 10,485,056
7) Time_To_Linger_On_Close = 0
8) System_Send_Buffer_Size = 1,048,576
9) System_Receive_Buffer_Size = 1,048,576
12) Is_Randezvous_Connect_Enabled = false
13) Send_Timeout = -1
14) Receive_Timeout = -1
15) Is_Address_Reuse_Enabled = true
16) Maximum_Bandwidth = -1
17) Status_Code = 5
18) Epoll_Event_Mask = 4
19) Send_Buffer_Consumed = 0
20) Receive_Buffer_Available = 0
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55, L:/192.168.31.153:53767 - R:/192.168.31.153:10020] WRITE: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 20 70 65 65 72 2e 2e 2e          |Hello peer...   |
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55, L:/192.168.31.153:53767 - R:/192.168.31.153:10020] FLUSH
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55, L:/192.168.31.153:53767 - R:/192.168.31.153:10020] READ: 16B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 20 63 6c 69 65 6e 74 20 2e 2e 2e |hello client ...|
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:50:54 netty.handler.udt.bytes.ByteEchoClientHandler === reciever ack message from peer:hello client ...
[INFO ] 2017-07-06 22:50:54 io.netty.handler.logging.LoggingHandler [id: 0xa43a1a55, L:/192.168.31.153:53767 - R:/192.168.31.153:10020] READ COMPLETE

再来看一个peer对等UDT Byte Stream示例:
peer端:
package netty.main.udt.peer;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;
import netty.handler.udt.peer.ByteEchoPeerHandler;
import java.net.SocketAddress;
import java.util.concurrent.ThreadFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * UDT Byte Stream Peer
 * @author donald
 * 2017年7月3日
 * 上午9:01:35
 */
public class ByteEchoPeerBase {
	private static final Logger log = LoggerFactory.getLogger(ByteEchoPeerBase.class);
	protected final String peerName;
    protected final int messageSize;
    protected final SocketAddress myAddress;
    protected final SocketAddress peerAddress;

    public ByteEchoPeerBase(String peerName,int messageSize, SocketAddress myAddress, SocketAddress peerAddress) {
        this.peerName = peerName; 
    	this.messageSize = messageSize;
        this.myAddress = myAddress;
        this.peerAddress = peerAddress;
    }

    @SuppressWarnings("deprecation")
	public void run() throws Exception {
        final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
                connectFactory, NioUdtProvider.BYTE_PROVIDER);
        try {
            final Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(connectGroup)
                    .channelFactory(NioUdtProvider.BYTE_RENDEZVOUS)
                    .handler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        protected void initChannel(UdtChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new ByteEchoPeerHandler(messageSize));
                        }
                    });
            final ChannelFuture future = bootstrap.connect(peerAddress, myAddress).sync();
            log.info("========="+peerName +" is start=========");
            future.channel().closeFuture().sync();
        } finally {
            connectGroup.shutdownGracefully();
        }
    }
}


peer one:
package netty.main.udt.peer;

import io.netty.util.internal.SocketUtils;
import netty.constant.udt.Config;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * UDT Byte Stream Peer one
 * @author donald
 * 2017年7月3日
 * 上午9:01:58
 */
public class ByteEchoPeerOne extends ByteEchoPeerBase {
    private static final String PEER_NAME = "peerOne";
    public ByteEchoPeerOne(int messageSize, SocketAddress myAddress, SocketAddress peerAddress) {
        super(PEER_NAME,messageSize, myAddress, peerAddress);
    }

    public static void main(String[] args) throws Exception {
        final int messageSize = 64 * 1024;
        final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
        final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
        new ByteEchoPeerOne(messageSize, myAddress, peerAddress).run();
    }
}

peer two:
package netty.main.udt.peer;

import io.netty.util.internal.SocketUtils;
import netty.constant.udt.Config;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * UDT Byte Stream Peer two
 * @author donald
 * 2017年7月3日
 * 上午9:02:14
 */
public class ByteEchoPeerTwo extends ByteEchoPeerBase {
	 private static final String PEER_NAME = "peerTwo";
    public ByteEchoPeerTwo(int messageSize, SocketAddress myAddress, SocketAddress peerAddress) {
        super(PEER_NAME,messageSize, myAddress, peerAddress);
    }
    public static void main(String[] args) throws Exception {
        final int messageSize = 64 * 1024;
        final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
        final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
        new ByteEchoPeerTwo(messageSize, myAddress, peerAddress).run();
    }
}


peer ip和port配置:
package netty.constant.udt;

/**
 * Peer to Peer Config
 * @author donald
 * 2017年7月3日
 * 上午9:22:01
 */
public final class Config {
    private Config() {
    }
    public static final String hostOne = "192.168.31.153";
    public static final int portOne = 10010;
    public static final String hostTwo = "192.168.31.153";
    public static final int portTwo = 10011;

}

启动peer one&two,控制台输出:
peer one:
[INFO ] 2017-07-06 22:54:56 com.barchart.udt.SocketUDT library location : ./lib/bin
[INFO ] 2017-07-06 22:54:56 com.barchart.udt.SocketUDT loader provider  : com.barchart.udt.lib.LibraryLoaderUDT
[INFO ] 2017-07-06 22:54:56 com.barchart.udt.lib.PluginPropsUDT ARCH/OS/LINK = amd64/Windows/gpp
[INFO ] 2017-07-06 22:54:56 com.barchart.udt.lib.LibraryLoaderUDT Platform supported.
[INFO ] 2017-07-06 22:54:56 com.barchart.udt.lib.LibraryLoaderUDT Loading release libraries.
[INFO ] 2017-07-06 22:54:56 com.barchart.udt.lib.LibraryLoaderUDT Release libraries loaded.
[INFO ] 2017-07-06 22:54:56 io.netty.handler.logging.LoggingHandler [id: 0xbd0fcb89] REGISTERED
[INFO ] 2017-07-06 22:54:56 io.netty.handler.logging.LoggingHandler 17CONNECT218218[id: 0xbd0fcb89] CONNECT: /192.168.31.153:10011, /192.168.31.153:10010
[INFO ] 2017-07-06 22:55:03 netty.main.udt.peer.ByteEchoPeerBase =========peerOne is start=========
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0xbd0fcb89, L:/192.168.31.153:10010 - R:/192.168.31.153:10011] ACTIVE
[INFO ] 2017-07-06 22:55:03 netty.handler.udt.peer.ByteEchoPeerHandler ECHO active
[id: 0x1c7738ed]
0) Maximum_Transfer_Unit = 1,500
1) Is_Send_Synchronous = false
2) Is_Receive_Synchronous = false
3) Custom_Congestion_Control = null
4) Flight_Window_Size = 25,600 (25 K)
5) Protocol_Send_Buffer_Size = 10,485,056
6) Protocol_Receive_Buffer_Size = 10,485,056
7) Time_To_Linger_On_Close = 0
8) System_Send_Buffer_Size = 1,048,576
9) System_Receive_Buffer_Size = 1,048,576
12) Is_Randezvous_Connect_Enabled = true
13) Send_Timeout = -1
14) Receive_Timeout = -1
15) Is_Address_Reuse_Enabled = true
16) Maximum_Bandwidth = -1
17) Status_Code = 5
18) Epoll_Event_Mask = 4
19) Send_Buffer_Consumed = 0
20) Receive_Buffer_Available = 0
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0xbd0fcb89, L:/192.168.31.153:10010 - R:/192.168.31.153:10011] WRITE: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 20 70 65 65 72 2e 2e 2e          |Hello peer...   |
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0xbd0fcb89, L:/192.168.31.153:10010 - R:/192.168.31.153:10011] FLUSH
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0xbd0fcb89, L:/192.168.31.153:10010 - R:/192.168.31.153:10011] READ: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 20 70 65 65 72 2e 2e 2e          |Hello peer...   |
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:55:03 netty.handler.udt.peer.ByteEchoPeerHandler ===reciever message from UDT Byte Stream Peer:Hello peer...
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0xbd0fcb89, L:/192.168.31.153:10010 - R:/192.168.31.153:10011] READ COMPLETE

peer two:
[INFO ] 2017-07-06 22:55:02 com.barchart.udt.SocketUDT library location : ./lib/bin
[INFO ] 2017-07-06 22:55:02 com.barchart.udt.SocketUDT loader provider  : com.barchart.udt.lib.LibraryLoaderUDT
[INFO ] 2017-07-06 22:55:02 com.barchart.udt.lib.PluginPropsUDT ARCH/OS/LINK = amd64/Windows/gpp
[INFO ] 2017-07-06 22:55:02 com.barchart.udt.lib.LibraryLoaderUDT Platform supported.
[INFO ] 2017-07-06 22:55:02 com.barchart.udt.lib.LibraryLoaderUDT Loading release libraries.
[INFO ] 2017-07-06 22:55:02 com.barchart.udt.lib.LibraryLoaderUDT Release libraries loaded.
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0x703f93fe] REGISTERED
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler 17CONNECT218218[id: 0x703f93fe] CONNECT: /192.168.31.153:10010, /192.168.31.153:10011
[INFO ] 2017-07-06 22:55:03 netty.main.udt.peer.ByteEchoPeerBase =========peerTwo is start=========
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0x703f93fe, L:/192.168.31.153:10011 - R:/192.168.31.153:10010] ACTIVE
[INFO ] 2017-07-06 22:55:03 netty.handler.udt.peer.ByteEchoPeerHandler ECHO active
[id: 0x02f505e9]
0) Maximum_Transfer_Unit = 1,500
1) Is_Send_Synchronous = false
2) Is_Receive_Synchronous = false
3) Custom_Congestion_Control = null
4) Flight_Window_Size = 25,600 (25 K)
5) Protocol_Send_Buffer_Size = 10,485,056
6) Protocol_Receive_Buffer_Size = 10,485,056
7) Time_To_Linger_On_Close = 0
8) System_Send_Buffer_Size = 1,048,576
9) System_Receive_Buffer_Size = 1,048,576
12) Is_Randezvous_Connect_Enabled = true
13) Send_Timeout = -1
14) Receive_Timeout = -1
15) Is_Address_Reuse_Enabled = true
16) Maximum_Bandwidth = -1
17) Status_Code = 5
18) Epoll_Event_Mask = 4
19) Send_Buffer_Consumed = 0
20) Receive_Buffer_Available = 0
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0x703f93fe, L:/192.168.31.153:10011 - R:/192.168.31.153:10010] WRITE: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 20 70 65 65 72 2e 2e 2e          |Hello peer...   |
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0x703f93fe, L:/192.168.31.153:10011 - R:/192.168.31.153:10010] FLUSH
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0x703f93fe, L:/192.168.31.153:10011 - R:/192.168.31.153:10010] READ: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 20 70 65 65 72 2e 2e 2e          |Hello peer...   |
+--------+-------------------------------------------------+----------------+
[INFO ] 2017-07-06 22:55:03 netty.handler.udt.peer.ByteEchoPeerHandler ===reciever message from UDT Byte Stream Peer:Hello peer...
[INFO ] 2017-07-06 22:55:03 io.netty.handler.logging.LoggingHandler [id: 0x703f93fe, L:/192.168.31.153:10011 - R:/192.168.31.153:10010] READ COMPLETE


0
0
分享到:
评论

相关推荐

    基于Netty框架的网络通信示例.zip

    基于Netty框架的网络通信示例 内容概要 本项目是一个基于Netty框架的网络通信示例,涵盖了多种网络通信场景,包括TCP、UDP、WebSocket等。项目展示了如何使用Netty进行高性能的异步网络编程,并提供了丰富的示例...

    netty通信完整示例

    这个“netty通信完整示例”提供了一套完整的Netty使用案例,包括了jar包、服务器端、客户端、编码器和解码器,确保了你能够亲身体验Netty的强大功能。 首先,让我们深入理解Netty的核心概念: 1. **NIO(Non-...

    Android-netty和socket通信的demo

    本示例“Android-netty和socket通信的demo”将展示如何在Android平台上利用Netty进行网络通信,与传统的Socket编程相结合,实现更加灵活和高效的通信机制。 首先,理解Netty的基本概念是非常重要的。Netty是一个...

    android netty cli +probuf示例

    总结来说,“android netty cli +protobuf示例”展示了如何在Android客户端利用Netty的异步网络通信能力和Protobuf的数据序列化特性,构建一个高效的即时通讯系统。这个示例涵盖了从消息定义到网络通信的全过程,...

    Android基于Netty框架实现通信

    在Android开发中,为了实现高效的网络通信,开发者常常会选择使用Netty框架。Netty是一个高性能、异步事件驱动的网络应用程序框架,适用于多种协议的服务器和客户端应用。本篇文章将详细探讨如何在Android环境中利用...

    netty示例NIO示例

    myeclipse开发通信示例,框架netty,代码本人写的,而且已测试通过,先运行NettyService,再运行NettyClient即可看到效果。nio示例也有,原理一样,运行先后顺序同netty.

    在使用netty进行网络通信协议传输使用protobuf时protobuf编译.proto文件生成JAVA类.zip

    在使用netty进行网络通信协议传输使用protobuf时protobuf编译.proto文件生成JAVA类.zip 包括测试proto3.proto文件,自动protobuf编译.proto文件生成JAVA类

    netty-transport-udt-4.0.0.CR4.zip

    在Netty中,UDT模块扩展了其网络通信的能力,使得开发者能够在需要高速数据传输和低延迟的应用场景中,如金融交易系统、实时流媒体服务等,充分利用UDT的优势。 Netty的UDT模块提供了高效的双向数据通道,它通过多...

    基于netty和RXTXCom.jar实现的串口通信master端和slave端

    0、maven项目 1、需要手动加入依赖RXTXcomm.jar 2、将rxtxParallel.dll,rxtxSerial.dll放到&lt;JAVA_HOME&gt;\jre\bin下 3、运行App.java主程序

    网络通信 netty_io

    【网络通信:Netty、BIO、NIO与AIO详解】 在计算机科学中,网络通信是实现系统间数据交换的关键技术。Java平台提供了多种I/O模型来支持网络通信,包括传统的阻塞I/O(BIO)、非阻塞I/O(NIO)以及异步I/O(AIO)。...

    netty接收串口数据代码,测试串口工具

    java netty接收串口数据 开启windows串口工具 发送串口数据调试助手

    (源码)基于Netty框架的网络通信系统.zip

    # 基于Netty框架的网络通信系统 ## 项目简介 本项目是一个基于Netty框架的网络通信系统,涵盖了多种网络通信场景和协议,包括TCP、UDP、HTTP、WebSocket等。通过使用Netty的高性能异步事件驱动框架,实现了高效、...

    读书笔记:Netty 权威指南的示例.zip

    读书笔记:Netty 权威指南的示例

    netty-websocket-example 基于netty的websocket实现示例

    Netty 是一个高性能、异步事件驱动的网络应用程序框架,常用于开发服务器和客户端的通信应用,尤其在处理高并发场景时表现出色。WebSocket 协议则是一种在客户端和服务器之间建立长连接的协议,提供了双向通信的能力...

    基于netty的即时通信

    在即时通信领域,Netty作为一个高性能、异步事件驱动的网络应用框架,广泛应用于各种复杂的通信系统。本项目“基于Netty的即时通信”旨在演示如何利用Netty构建一个简单的聊天应用,它包括服务端和客户端,实现了多...

    netty完整代码示例

    Netty 是一个利用 Java 的高级网络的能力,隐藏其背后的复杂性而提供一个易于使用的 API的客户端/服务器框架。Netty 提供高性能和可扩展性,让你可以自由地专注于你真正感兴趣的东西,你的独特的应用!

    基于Netty框架的网络通信项目.zip

    本项目通过多个示例代码,展示了如何使用Netty实现不同类型的网络通信,包括HTTP服务器、WebSocket服务器、Protobuf编解码、Thrift服务等。 项目的主要特性和功能 1. HTTP服务器 实现了一个简单的HTTP服务器,...

    netty-transport-udt-4.1.73.Final-API文档-中文版.zip

    赠送jar包:netty-transport-udt-4.1.73.Final.jar; 赠送原API文档:netty-transport-udt-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-udt-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-...

    netty框架各种通信C/S端

    Netty框架是Java领域中广泛使用的高性能、异步事件驱动的...通过学习和实践这个"Netty框架各种通信C/S端"的资源,你可以深入理解Netty的工作原理,掌握高效网络编程技巧,为你的项目带来更稳定、高效的网络通信能力。

    Android与Netty的结合通信

    本文将深入探讨如何将Android应用程序与Netty框架相结合,实现高效、可靠的网络通信。Netty是一个高性能、异步事件驱动的网络应用程序框架,适用于开发服务器和客户端的Java应用。通过将Netty引入Android开发,我们...

Global site tag (gtag.js) - Google Analytics