netty 4.1.52 final 버전, 전문 통신 관련해서 질문입니다.!

171 views
Skip to first unread message

백두산

unread,
Jan 14, 2022, 1:42:57 AM1/14/22
to Netty Korean User Group
안녕하세요. 최근 네티로 고도화를 하는 과제 중에 있습니다.
그런데 이 부분에서 제가 코드를 잘못짠 것인지 문제점이 생기더라구요

문제점 : 전문 중에 로그인 인증을 하는 전문이 있습니다.
해당 전문에서 SESSION_TYPE이란 것을 바디에 담아 넘겨주게 되는데
이 SESSION_TYPE가 MT_SEND_LINE 이렇게 세션 라인을 연결시켜주는 전문이거든요.
그런데 이것을 네티로 고도화하며 고쳐보려 하였는데 세션 유지가 안되는 건지..
아니면 세션 유지하는 걸 체크하기 위해 요청을 보내는 세션 체크 전문이 잘못된 건지..
해당 코드 올려두겠습니다.. 너무 어렵네요 ㅠㅠ

NettyClient

public class NettyClient2 {

private Bootstrap bs = new Bootstrap();

private SocketAddress socketAddress;

private ByteBuf[] msgArr;

public NettyClient2(SocketAddress addr, ByteBuf[] msgArr) {
this.socketAddress = addr;
this.msgArr = msgArr;
}

public NettyClient2(String host, int port, ByteBuf[] msgArr) {
this(new InetSocketAddress(host, port), msgArr);
}


public void run() {

bs.group(new NioEventLoopGroup(3))
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
//소켓 핸들러에 ChannelInitializer를 등록한다.
// ChannelInitializer 말 그대로 채널 초기함수이다.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("clientHandler", new NettyClientHandler(msgArr));
}

})
.option(ChannelOption.SO_KEEPALIVE, true)
;

ChannelFuture f = bs.connect(socketAddress);
f.channel();
}

public static void main(String[] args) {

String host = "127.0.0.1";
int port = 8000;

String CLIENT_ID = "api 서버에서 발급 받은 아이디";
String CLIENT_PASSWORD_LENGTH = "20\0\0";
String CLIENT_PASSWORD = "api 서버에서 발급 받은 패스워드";
String SESSION_TYPE = "1001";
String REPORT_RECEIVE_YN = "1012";
String YES = "Y";
String PDU_TYPE = "1002";
String PDU_VERSION = "101\0";
String BODY_LENGTH = "49\0\0\0\0\0\0\0\0\0\0";

String body = CLIENT_ID + CLIENT_PASSWORD_LENGTH + CLIENT_PASSWORD + SESSION_TYPE + REPORT_RECEIVE_YN + YES;
String header = PDU_TYPE + PDU_VERSION + BODY_LENGTH;

ByteBuf headBuf = Unpooled.wrappedBuffer(header.getBytes());

ByteBuf bodyBuf = Unpooled.wrappedBuffer(body.getBytes());

ByteBuf messageBuffer = Unpooled.buffer();
messageBuffer.writeBytes(header.getBytes());
messageBuffer.writeBytes(body.getBytes());

ByteBuf[] bufArray = new ByteBuf[1];

bufArray[0] = messageBuffer;

new NettyClient(host, port, bufArray).run();

}
}


NettyClientHandler

public class NettyClientHandler2 extends ChannelInboundHandlerAdapter {

private ByteBuf[] msgArr;

int idx = 0;

public NettyClientHandler2(ByteBuf[] msgArr) {
this.msgArr = msgArr;
}

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelRegistered");
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {

System.out.println("채널 연결이 종료됨.");
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {

System.out.println("channelActive");

sendMsg(ctx, msgArr[idx++]);
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {

System.out.println("channelRead");

ByteBuf buf = (ByteBuf)msg;
int readMessage = buf.readableBytes();
if( readMessage > 0 ) {
byte[] count = new byte[readMessage];

buf.readBytes(count);

String receiveMsg = new String( count );
System.out.println("receiveMsg : " + receiveMsg);

if ( msgArr.length == idx ) {
System.out.println("모든 메시지 전송 및 수신 완료");

}
else {
//메시지를 읽고도 남은 메시지가 있다면 이후의 메시지를 보낸다..
//sendMsg(ctx, msgArr[idx++]);
}
}
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
System.out.println("channelReadComplete");

}

private void sendMsg(ChannelHandlerContext ctx, ByteBuf buffer) {
System.out.println("sendMsg");
ctx.writeAndFlush( buffer );
}

private void channelClose(ChannelHandlerContext ctx) {
System.out.println("channelClose");
ctx.close();
}
}

이렇게 핸들러, 클라이언트 측 코드를 짜두었고
해당 코드를 실행하게 되면
로그인 인증에 대한 응답 코드는 오며 채널은 끊어지지 않고 유지가 됩니다..
그러면 세션이 유지가 되는 부분인가요? 
그리고 나선 로그인 인증할 때 사용한 채널을 통해서 
세션 체크 전문을 날려야 로그인 인증이 된 채널에서 전문이 날라가서
응답코드가 날라올 것이기에 그 부분을 어떻게 짜면 될까요?
네티에 대한 이해도가 부족하여 같은 채널 내에서 
다른 전문을 전송하는 법을 감을 못찾겠네요.. ㅠㅠ 

taekuk.song

unread,
Jan 26, 2022, 4:47:25 AM1/26/22
to Netty Korean User Group
class NettyClient   {
        private SocketChannel socketChannel;

        run () {
                ChannelFuture f = bs.connect(socketAddress);
                ChannelFuture f = bs.connect(socketAddress).awaitUninterruptibly();
                socketChannel = (SocketChannel) f.channel();
        }

        public send(msg) {
           socketChannel.writeAndFlush(msg);
        }

}

NettyClient client = new NettyClient();
client.run();
client.send("전문");

2022년 1월 14일 금요일 오후 3시 42분 57초 UTC+9에 백두산님이 작성:
Reply all
Reply to author
Forward
0 new messages