Usuful links:
1. https://en.wikipedia.org/wiki/Netty_(software)
2. https://habrahabr.ru/post/277695/
1. https://en.wikipedia.org/wiki/Netty_(software)
2. https://habrahabr.ru/post/277695/
NettyClient.java:
package socketclient.netty; import com.thetransactioncompany.jsonrpc2.JSONRPC2Request; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import socketclient.common.ClientRequest; /** * Created by Mansurjon on 2/9/2017. */public class NettyClient<T> { protected final int SOCKET_WAIT_TIME = 100; protected final int SOCKET_LOOP_MAX_COUNT = 5; private String host; private int port; private Channel channel; private Boolean isOpen = false; private EventLoopGroup group; public Channel getChannel() { return channel; } public NettyClient(String host, int port) { this.host = host; this.port = port; } public boolean start() { group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .handler(new NettyClientInitializer()); try { channel = bootstrap.connect(host, port).sync().channel(); } catch (InterruptedException e) { return false; //e.printStackTrace(); } isOpen = channel.isActive(); return isOpen; } public String ececute(ClientRequest clientRequest){ if (isOpen){ JSONRPC2Request request=newJSONRPC2Request(clientRequest.getMethodName(),clientRequest.getReqParams(),clientRequest.getRequestID()); this.channel.write(request.toString() + "\r\n"); int loopTimeout = 0; while (NettyClientHandler.jsonResponse.equals("")&&loopTimeout<SOCKET_LOOP_MAX_COUNT){ try { loopTimeout++; Thread.sleep(SOCKET_WAIT_TIME); } catch (InterruptedException e) { e.printStackTrace(); } } this.channel.flush(); group.shutdownGracefully(); return NettyClientHandler.jsonResponse; } return ""; } public EventLoopGroup getGroup() { return group; } public void close(){ if (!isOpen){ this.channel.flush(); this.channel.close(); this.channel.close(); group.shutdownGracefully(); isOpen = false;group = null; } } }NettyClientHandler.java:NettyClientInitializer.java:Server.java:ServerHandler.java:ServerInitializer.java:setRequest(clientRequest).execute(); System.out.println("this is server resp.:"+client1.getResponse()); } }
 
