Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
znks
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
王夏晖
znks
Commits
44fe55af
Commit
44fe55af
authored
Sep 06, 2018
by
leyboy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.ZnksServer完成,接受客户端解码未完成
parent
e77ed45b
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
294 additions
and
183 deletions
+294
-183
workspace.xml
.idea/workspace.xml
+124
-182
ZnksServer.java
...main/src/main/java/com/adc/da/znks/server/ZnksServer.java
+55
-1
RequestDecoder.java
...n/java/com/adc/da/znks/server/handler/RequestDecoder.java
+21
-0
ZnksServerHandler.java
...ava/com/adc/da/znks/server/handler/ZnksServerHandler.java
+71
-0
ClientUtils.java
...c/main/java/com/adc/da/znks/server/utils/ClientUtils.java
+23
-0
No files found.
.idea/workspace.xml
View file @
44fe55af
This diff is collapsed.
Click to expand it.
adc-da-main/src/main/java/com/adc/da/znks/server/ZnksServer.java
View file @
44fe55af
package
com
.
adc
.
da
.
znks
.
server
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.*
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* znks server
*
* @author ley
*
*
*/
**/
public
class
ZnksServer
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ZnksServer
.
class
);
/**
* init znks server
**/
public
static
void
initZnksServer
(
int
port
)
{
//服务类
ServerBootstrap
bootstrap
=
new
ServerBootstrap
();
//boss和worker
//EventLoopGroup用来管理和调度netty中的各种线程
EventLoopGroup
boss
=
new
NioEventLoopGroup
();
EventLoopGroup
worker
=
new
NioEventLoopGroup
();
try
{
//设置线程池
bootstrap
.
group
(
boss
,
worker
);
//设置socket工厂、
bootstrap
.
channel
(
NioServerSocketChannel
.
class
);
//设置管道工厂
bootstrap
.
childHandler
(
new
ChannelInitializer
<
Channel
>()
{
@Override
protected
void
initChannel
(
Channel
ch
)
throws
Exception
{
}
});
//设置参数,TCP参数
bootstrap
.
option
(
ChannelOption
.
SO_BACKLOG
,
2048
);
//serverSocketchannel的设置,链接缓冲池的大小
bootstrap
.
childOption
(
ChannelOption
.
SO_KEEPALIVE
,
true
);
//socketchannel的设置,维持链接的活跃,清除死链接
bootstrap
.
childOption
(
ChannelOption
.
TCP_NODELAY
,
true
);
//socketchannel的设置,关闭延迟发送
//绑定端口
ChannelFuture
future
=
bootstrap
.
bind
(
port
);
logger
.
info
(
"57所netty监听服务启动成功..."
);
//等待服务端关闭
future
.
channel
().
closeFuture
().
sync
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
//释放资源
boss
.
shutdownGracefully
();
worker
.
shutdownGracefully
();
}
}
}
adc-da-main/src/main/java/com/adc/da/znks/server/handler/RequestDecoder.java
0 → 100644
View file @
44fe55af
package
com
.
adc
.
da
.
znks
.
server
.
handler
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
java.util.List
;
/**
* request decoder
**/
public
class
RequestDecoder
extends
ByteToMessageDecoder
{
/**
*
* **/
@Override
protected
void
decode
(
ChannelHandlerContext
channelHandlerContext
,
ByteBuf
byteBuf
,
List
<
Object
>
list
)
throws
Exception
{
}
}
adc-da-main/src/main/java/com/adc/da/znks/server/handler/ZnksServerHandler.java
0 → 100644
View file @
44fe55af
package
com
.
adc
.
da
.
znks
.
server
.
handler
;
import
com.adc.da.znks.server.utils.ClientUtils
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* znks server handler
*
* @author ley
**/
public
class
ZnksServerHandler
extends
ChannelInboundHandlerAdapter
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ZnksServerHandler
.
class
);
/**
* 存有连接进来的客户端
**/
public
static
final
Map
<
String
,
ChannelHandlerContext
>
clientMap
=
new
ConcurrentHashMap
<>(
64
);
/**
* 客户端数量
**/
private
final
AtomicInteger
clientNumber
=
new
AtomicInteger
(
0
);
/**
* 接受客户端数据
**/
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
}
/**
* 新客户端接入
**/
@Override
public
void
channelActive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
String
key
=
ClientUtils
.
getClientIpAndPort
(
ctx
);
clientMap
.
put
(
key
,
ctx
);
clientNumber
.
incrementAndGet
();
logger
.
info
(
"客户端: {}, 连接成功"
,
key
);
logger
.
info
(
"连接服务端的客户端个数: {}"
,
clientNumber
.
get
());
}
/**
* 客户端断开
**/
@Override
public
void
channelInactive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
super
.
channelInactive
(
ctx
);
}
/**
* 处理异常
**/
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
cause
.
printStackTrace
();
}
}
adc-da-main/src/main/java/com/adc/da/znks/server/utils/ClientUtils.java
0 → 100644
View file @
44fe55af
package
com
.
adc
.
da
.
znks
.
server
.
utils
;
import
io.netty.channel.ChannelHandlerContext
;
import
java.net.InetSocketAddress
;
/**
* client utility class
*
* @author ley
**/
public
class
ClientUtils
{
/**
* get client ip and port
**/
public
static
String
getClientIpAndPort
(
ChannelHandlerContext
context
)
{
InetSocketAddress
clientSocket
=
(
InetSocketAddress
)
context
.
channel
().
remoteAddress
();
String
clientIp
=
clientSocket
.
getAddress
().
getHostAddress
();
int
clientPort
=
clientSocket
.
getPort
();
return
clientIp
+
":"
+
clientPort
;
}
}
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