Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengtianlai3
ioc_sixiang_license
Commits
f55efe5a
Commit
f55efe5a
authored
Nov 08, 2022
by
AfirSraftGarrier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
格式
parent
fb1e7076
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
42 deletions
+40
-42
TcpServer.java
license/src/main/java/iot/sixiang/license/net/TcpServer.java
+40
-42
No files found.
license/src/main/java/iot/sixiang/license/net/TcpServer.java
View file @
f55efe5a
...
@@ -10,48 +10,46 @@ import lombok.extern.slf4j.Slf4j;
...
@@ -10,48 +10,46 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@Slf4j
public
class
TcpServer
{
public
class
TcpServer
{
private
int
port
;
private
BaseChannelInitializer
channelInitializer
;
private
int
port
;
public
TcpServer
(
int
port
,
BaseChannelInitializer
channelInitializer
)
{
private
BaseChannelInitializer
channelInitializer
;
this
.
port
=
port
;
this
.
channelInitializer
=
channelInitializer
;
}
public
TcpServer
(
int
port
,
BaseChannelInitializer
channelInitializer
)
{
public
void
start
()
{
this
.
port
=
port
;
//创建两个线程组 bossGroup、workerGroup
this
.
channelInitializer
=
channelInitializer
;
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
(
4
);
}
EventLoopGroup
workerGroup
=
new
NioEventLoopGroup
(
4
);
log
.
debug
(
"Tcp服务,开始监听端口:{}"
,
port
);
public
void
start
()
{
//创建服务端的启动对象,设置参数
ServerBootstrap
b
=
new
ServerBootstrap
();
//创建两个线程组 bossGroup、workerGroup
//设置两个线程组boosGroup和workerGroup
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
(
4
);
b
.
group
(
bossGroup
,
workerGroup
)
EventLoopGroup
workerGroup
=
new
NioEventLoopGroup
(
4
);
//设置服务端通道实现类型
log
.
debug
(
"Tcp服务,开始监听端口:{}"
,
port
);
.
channel
(
NioServerSocketChannel
.
class
)
//创建服务端的启动对象,设置参数
// .handler(new LoggingHandler(LogLevel.INFO))
ServerBootstrap
b
=
new
ServerBootstrap
();
.
childHandler
(
channelInitializer
)
//设置两个线程组boosGroup和workerGroup
// 设置tcp缓冲区
b
.
group
(
bossGroup
,
workerGroup
)
.
option
(
ChannelOption
.
SO_BACKLOG
,
1024
)
//设置服务端通道实现类型
//设置保持活动连接状态
.
channel
(
NioServerSocketChannel
.
class
)
.
childOption
(
ChannelOption
.
SO_KEEPALIVE
,
true
);
// .handler(new LoggingHandler(LogLevel.INFO))
ChannelFuture
f
;
.
childHandler
(
channelInitializer
)
try
{
// 设置tcp缓冲区
f
=
b
.
bind
(
port
).
sync
();
.
option
(
ChannelOption
.
SO_BACKLOG
,
1024
)
f
.
channel
().
closeFuture
().
sync
();
//设置保持活动连接状态
}
catch
(
InterruptedException
e
)
{
.
childOption
(
ChannelOption
.
SO_KEEPALIVE
,
true
);
// TODO Auto-generated catch block
ChannelFuture
f
;
log
.
error
(
"Tcp服务异常,端口:{}"
,
port
);
try
{
}
finally
{
f
=
b
.
bind
(
port
).
sync
();
log
.
debug
(
"Tcp服务,停止退出"
);
f
.
channel
().
closeFuture
().
sync
();
if
(
workerGroup
!=
null
)
{
}
catch
(
InterruptedException
e
)
{
workerGroup
.
shutdownGracefully
();
// TODO Auto-generated catch block
}
log
.
error
(
"Tcp服务异常,端口:{}"
,
port
);
if
(
bossGroup
!=
null
)
{
}
finally
{
bossGroup
.
shutdownGracefully
();
log
.
debug
(
"Tcp服务,停止退出"
);
}
if
(
workerGroup
!=
null
)
{
}
workerGroup
.
shutdownGracefully
();
}
}
if
(
bossGroup
!=
null
)
{
bossGroup
.
shutdownGracefully
();
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment