forked from fuzhengwei/CodeGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00f5909
commit fa2189b
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>itstack-demo-netty</artifactId> | ||
<groupId>org.itatack.demo</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>itstack-demo-netty-1-01</artifactId> | ||
|
||
|
||
</project> |
22 changes: 22 additions & 0 deletions
22
...ack-demo-netty-1-01/src/main/java/org/itstack/demo/netty/server/MyChannelInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.itstack.demo.netty.server; | ||
|
||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.socket.SocketChannel; | ||
|
||
/** | ||
* 虫洞栈:https://bugstack.cn | ||
* 公众号:bugstack虫洞栈 {获取学习源码} | ||
* Create by fuzhengwei on 2019 | ||
*/ | ||
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> { | ||
|
||
@Override | ||
protected void initChannel(SocketChannel channel) { | ||
System.out.println("链接报告开始"); | ||
System.out.println("链接报告信息:有一客户端链接到本服务端"); | ||
System.out.println("链接报告IP:" + channel.localAddress().getHostString()); | ||
System.out.println("链接报告Port:" + channel.localAddress().getPort()); | ||
System.out.println("链接报告完毕"); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...etty/itstack-demo-netty-1-01/src/main/java/org/itstack/demo/netty/server/NettyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.itstack.demo.netty.server; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
|
||
/** | ||
* 虫洞栈:https://bugstack.cn | ||
* 公众号:bugstack虫洞栈 {获取学习源码} | ||
* Create by fuzhengwei on 2019 | ||
*/ | ||
public class NettyServer { | ||
|
||
public static void main(String[] args) { | ||
new NettyServer().bing(7397); | ||
} | ||
|
||
private void bing(int port) { | ||
//配置服务端NIO线程组 | ||
EventLoopGroup parentGroup = new NioEventLoopGroup(); //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2)); | ||
EventLoopGroup childGroup = new NioEventLoopGroup(); | ||
try { | ||
ServerBootstrap b = new ServerBootstrap(); | ||
b.group(parentGroup, childGroup) | ||
.channel(NioServerSocketChannel.class) //非阻塞模式 | ||
.option(ChannelOption.SO_BACKLOG, 128) | ||
.childHandler(new MyChannelInitializer()); | ||
ChannelFuture f = b.bind(port).sync(); | ||
System.out.println("itstack-demo-netty server start done. {关注公众号:bugstack虫洞栈,获取源码}"); | ||
f.channel().closeFuture().sync(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
childGroup.shutdownGracefully(); | ||
parentGroup.shutdownGracefully(); | ||
} | ||
|
||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...demo-netty/itstack-demo-netty-1-01/src/test/java/org/itstack/demo/netty/test/ApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.itstack.demo.netty.test; | ||
|
||
/** | ||
* 虫洞栈:https://bugstack.cn | ||
* 公众号:bugstack虫洞栈 {获取学习源码} | ||
* Create by fuzhengwei on 2019 | ||
*/ | ||
public class ApiTest { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("hi! bugstack虫洞栈"); | ||
} | ||
|
||
} |