Code Monkey home page Code Monkey logo

Comments (1)

waylau avatar waylau commented on May 18, 2024

@Duanzq-GIT
起多个Bootstrap 就可以了,示例如下:

EventLoopGroup workerGroup = new NioEventLoopGroup();

		try {
			Bootstrap b = new Bootstrap();
			b.group(workerGroup);
			b.channel(NioSocketChannel.class);
			b.option(ChannelOption.SO_KEEPALIVE, true);
			b.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				public void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast("decoder", new MyDecoder());
					ch.pipeline().addLast("encoder", new MyEncoder());
					ch.pipeline().addLast(new MyClientHandler());

				}
			});

			Bootstrap b2 = new Bootstrap();
			b2.group(workerGroup);
			b2.channel(NioSocketChannel.class);
			b2.option(ChannelOption.SO_KEEPALIVE, true);
			b2.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				public void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast("decoder", new MyDecoder());
					ch.pipeline().addLast("encoder", new MyEncoder());
					ch.pipeline().addLast(new MyClientHandler());

				}
			});
			// 启动客户端
			ChannelFuture f = b2.connect(host, port).sync();
			ChannelFuture f2 = b2.connect(host, port).sync();

			while (true) {

				// 发送消息给服务器
				Msg msg = new Msg();
				MsgHeader msgHeader = new MsgHeader();
				msgHeader.setMsgType(MsgType.EMGW_LOGIN_REQ.getValue());
				String body = "床前明月光,疑是地上霜。举头望明月,低头思故乡。";

				byte[] bodyBytes = body.getBytes(Charset.forName("utf-8"));
				int bodySize = bodyBytes.length;
				msgHeader.setLen(bodySize);
				msg.setMsgHeader(msgHeader);
				msg.setBody(body);

				f.channel().writeAndFlush(msg);
				f2.channel().writeAndFlush(msg);
				Thread.sleep(2000);
			}
		} finally {
			workerGroup.shutdownGracefully();
		}

from netty-4-user-guide-demos.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.