Develop
2015.01.02 18:32
[java] netty (비동기 이벤트 방식 네트워크 프레임워크) 사용법 #2 (client)
조회 수 3639 댓글 0
바로 이어서 client 로직을 올려 봅니다.
여기도 2개의 클래스 입니다.
첫 번째로 이벤트 핸들러 클래스 입니다.
package com.incross.netty;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
public class SimpleClientHandler extends SimpleChannelHandler
{
@Override
public void messageReceived(ChannelHandlerContext ctx,MessageEvent e)
{
ChannelBuffer response = (ChannelBuffer)e.getMessage();
byte[] message = response.array();
System.out.println("message:"+new String(message));
//response 메시지 찍어보기
if(new String(message).equals("server write test"))
{
//어떤 조건이 들어왔을 때 종료 되는 로직
Channel ch = e.getChannel();
ch.close();
System.out.println("closed");
}
}
//connection 연결 하면 바로 데이터 전송 하도록 하는 메소드
@Override
public void channelConnected(ChannelHandlerContext ctx,ChannelStateEvent e)
{
Channel ch = e.getChannel();
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
buf.writeBytes("1234a".getBytes());
ChannelFuture future = ch.write(buf);
future.addListener(new ChannelFutureListener()
{
public void operationComplete(ChannelFuture future)
{
Channel ch = future.getChannel();
//ch.close();
//보내고 응답 안받고 끝내려면 close 해주면 됨
}
});
}
public void exceptionCaught(ChannelHandlerContext ctx,ExceptionEvent e)
{
e.getCause().printStackTrace();
Channel ch = e.getChannel();
ch.close();
}
}두 번째로 클라이언트 쪽의 main 클래스 입니다.
package com.incross.netty;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
public class SimpleClient
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int port = 8000;
ChannelFactory factory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()
);
ClientBootstrap bootstrap = new ClientBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception
{
// TODO Auto-generated method stub
return Channels.pipeline(new SimpleClientHandler());
}
});
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost",port));
// 아래 부터는 connection 끊어 졌을 때를 위한 처리
future.awaitUninterruptibly();
if(!future.isSuccess())
{
future.getCause().printStackTrace();
}
future.getChannel().getCloseFuture().awaitUninterruptibly();
factory.releaseExternalResources();
//connection 끊어졌을 때 자원 회수
}
}[출처] http://shonm.tistory.com/398
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|---|
| 925 | Develop |
[ios] UIWebView를 이용한 로컬 HTML 파일 표시
|
hooni | 2015.01.02 | 2396 |
| 924 | Develop | [android] 안드로이드 동영상 스트리밍 예제 2 | hooni | 2015.01.02 | 6352 |
| 923 | System/OS | CentOS 에서 Cacti 설치하기 | hooni | 2015.01.02 | 3293 |
| » | Develop | [java] netty (비동기 이벤트 방식 네트워크 프레임워크) 사용법 #2 (client) | hooni | 2015.01.02 | 3639 |
| 921 | Develop | [java] netty (비동기 이벤트 방식 네트워크 프레임워크) 사용법 #1 (server) 1 | hooni | 2015.01.02 | 4400 |
| 920 | System/OS | [svn] 하나의 SVN에서 멀티 저장소 (One svnserve, multiple repositories) | hooni | 2015.01.02 | 2752 |
| 919 | Develop |
ZBar 라이브러리를 이용한 바코드 스캔 앱 개발하기
|
hooni | 2015.01.01 | 2818 |
| 918 | System/OS |
iptime 공유기 해킹 기술문서
4 |
hooni | 2015.01.01 | 3738 |
| 917 | System/OS | [svn] Can't convert string from native encoding to 'UTF-8' 메시지가 나오는 경우 | hooni | 2014.12.18 | 2279 |
| 916 | System/OS | [svn] SVN trunk 변경사항 되돌리기 (SVN Rollback) | hooni | 2014.11.27 | 2712 |
| 915 | Develop | [ios] 비동기 블럭 코드 예제 | hooni | 2014.11.21 | 1676 |
| 914 | Develop | [ios] 스크린 캡쳐 (전원버튼 + 홈버튼) 호출 알아내기 | hooni | 2014.11.19 | 2705 |