服务器编程消息接收与处理,Netty socket客户端与服务器端编程示例|Hello World

本文介绍了如何使用Netty框架编写一个非阻塞的socket客户端与服务器端,通过Maven导入相关jar包,利用jboss的marshalling进行数据编码解码,展示了服务器启动、消息处理及客户端发送接收的详细步骤,揭示了Netty在提高效率和节省资源方面的优势。
摘要由CSDN通过智能技术生成

Netty是一个非租塞的异步通信框架,它是机遇NIO开发的,NIO的全称是NoneBlocking IO,非阻塞IO,区别于BIO,BIO的全称是Blocking IO,阻塞IO,非阻塞比阻塞式的编程更加节省服务器资源,下面使用netty编写一个socket客户端与服务器端,算是对Netty的Hello World入门吧!

第一步:使用Maven导入netty相关的jar包,代码如下:

org.jboss.marshalling

jboss-marshalling

2.0.5.Final

org.jboss.marshalling

jboss-marshalling-serial

1.3.0.CR9

test

io.netty

netty-all

4.1.23.Final

注意:没有第二个jboss-marshalling-serial包客户端可能无法接收到消息,所以最好导入进来。

第二步:使用jboss的marshalling来对发送和接收的socket数据进行编码解码操作,代码如下:public class MarshallingCodeCFactory {

//解码

public static MarshallingDecoder buildMarshallingDecoder() {

final MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");

final MarshallingConfiguration configuration = new MarshallingConfiguration();

configuration.setVersion(5);

UnmarshallerProvider provider = new DefaultUnmarshallerProvider(factory, configuration);

MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024*1024);

return decoder;

}

//编码

public static MarshallingEncoder buildMarshallingEncoder() {

final MarshallerFactory factory = Marshalling.getProv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值