-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNettyClient.java
More file actions
55 lines (41 loc) · 1.6 KB
/
NettyClient.java
File metadata and controls
55 lines (41 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.zs.system.io;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* @author: 马士兵教育
* @create: 2020-04-26 15:59
*/
public class NettyClient {
public static void main(String[] args) {
try {
NioEventLoopGroup worker = new NioEventLoopGroup();
Bootstrap boot = new Bootstrap();
boot.group(worker)
.channel(NioSocketChannel.class)
.remoteAddress("192.168.150.11", 9090)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
System.out.println("初始化client");
ChannelPipeline p = sc.pipeline();
p.addLast(new MyInbound());
}
});
ChannelFuture conn = boot.connect().sync();
Channel client = conn.channel();
System.out.println(client);
ByteBuf byteBuf = Unpooled.copiedBuffer("hello world".getBytes());
client.writeAndFlush(byteBuf).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}