Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartBooks committed Oct 28, 2017
1 parent dd25662 commit ef77b3b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 14 deletions.
1 change: 1 addition & 0 deletions bin/client.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -cp ./netty-example.jar com.ljja.client.ClientApp
1 change: 1 addition & 0 deletions bin/server.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -cp ./netty-example.jar com.ljja.server.ServerApp
5 changes: 2 additions & 3 deletions src/main/java/com/ljja/client/ClientApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ public void initChannel(SocketChannel ch) throws Exception {
//异步连接到服务
ChannelFuture future = b.connect(HOST, PORT).sync();


Channel clientChannel = future.channel();

int currentCount = 10;

while (currentCount > 0) {

Thread.sleep(1000);
//Thread.sleep(1000);

clientChannel.writeAndFlush("Hello Netty Server");

Expand All @@ -58,7 +57,7 @@ public void initChannel(SocketChannel ch) throws Exception {

clientChannel.writeAndFlush(msgEntity);

currentCount -= 1;
//currentCount -= 1;
}

System.out.println("Close Channel");
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/com/ljja/encoder/CustomDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;


public class CustomDecoder extends LengthFieldBasedFrameDecoder {

//判断传送客户端传送过来的数据是否按照协议传输,头部信息的大小应该是 byte+byte+int = 1+1+4 = 6
private static final int HEADER_SIZE = 6;

private byte type;

private byte flag;

private int length;

private String body;
private String encoding = "UTF-8";

/**
* @param maxFrameLength 解码时,处理每个帧数据的最大长度
Expand Down Expand Up @@ -65,10 +61,8 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception

buf.readBytes(req);

body = new String(req, "UTF-8");

CustomMsg customMsg = new CustomMsg(type, flag, length, body);
body = new String(req, encoding);

return customMsg;
return new CustomMsg(type, flag, length, body);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/ljja/protocol/CustomMsg.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ljja.protocol;

/**
* Created by Administrator on 2017/10/28.
* 消息协议数据包
*/
public class CustomMsg {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ljja/server/ServerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class ServerApp {

private static final int MAX_FRAME_LENGTH = 1024 * 1024;
private static final int MAX_FRAME_LENGTH = 1024 * 1024 * 10;
private static final int LENGTH_FIELD_LENGTH = 4;
private static final int LENGTH_FIELD_OFFSET = 2;
private static final int LENGTH_ADJUSTMENT = 0;
Expand Down

0 comments on commit ef77b3b

Please sign in to comment.