Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.client.network;
- import java.net.InetSocketAddress;
- import java.util.concurrent.Executors;
- import org.client.utility.Config;
- import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
- import org.jboss.netty.bootstrap.ClientBootstrap;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelFuture;
- import org.jboss.netty.channel.ChannelPipeline;
- import org.jboss.netty.channel.ChannelPipelineFactory;
- import org.jboss.netty.channel.Channels;
- public class Connection {
- private static Channel channel;
- public static boolean connect() {
- try {
- ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
- bootstrap.setPipelineFactory(new PipelineFactory());
- bootstrap.setOption("child.tcpNoDelay", true);
- ChannelFuture future = bootstrap.connect(new InetSocketAddress(
- Config.SERVER_IP, Config.PORT));
- Channel channel = future.awaitUninterruptibly().getChannel();
- if (!future.isSuccess()) {
- bootstrap.releaseExternalResources();
- return false;
- }
- setChannel(channel);
- } catch (Exception ex) {
- ex.printStackTrace();
- return false;
- }
- return true;
- }
- public static Channel getChannel() {
- return channel;
- }
- public static void setChannel(Channel channel) {
- Connection.channel = channel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement