首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Netty握手问题

Netty握手问题
EN

Stack Overflow用户
提问于 2012-12-30 08:16:56
回答 1查看 1.1K关注 0票数 0

我有以下代码来获得与服务器的异步握手,但我似乎不能弄清楚为什么我无法连接到网站。

代码语言:javascript
运行
复制
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandler;
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;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
import org.jboss.netty.handler.logging.LoggingHandler;

public class Handshake
{
    public static String remoteHost = "http://google.com";
    public static int remotePort = 80;
    private static ClientBootstrap bootstrap;
    private static Channel channel;
    private static final ChannelHandler HTTP_REQUEST_ENCODER = new HttpRequestEncoder();
    private static final ChannelHandler HTTP_REQUEST_DECODER = new HttpRequestDecoder();
    private static final ChannelHandler HTTP_RESONPSE_DECODER = new HttpResponseDecoder();
    private static final ChannelHandler LOG_HANDLER = new LoggingHandler();

    public static void main(String[] args)
    {
        attemptConnection();
    }

    public static void attemptConnection()
    {
        Thread thread = new Thread()
        {
            public void run()
            {
                Executor bossPool = Executors.newCachedThreadPool();
                Executor workerPool = Executors.newCachedThreadPool();
                ChannelFactory channelFactory = new NioClientSocketChannelFactory(bossPool, workerPool);
                ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory()
                {
                    public ChannelPipeline getPipeline() throws Exception
                    {
                        ChannelPipeline pipeline = Channels.pipeline();
                        pipeline.addLast("requestencoder", HTTP_REQUEST_ENCODER);
                        pipeline.addLast("reqeustdecoder", HTTP_REQUEST_DECODER);
                        pipeline.addLast("responsedecoder", HTTP_RESONPSE_DECODER);
                        pipeline.addLast("logger", LOG_HANDLER);
                        return pipeline;
                    }
                };
                bootstrap = new ClientBootstrap(channelFactory);
                bootstrap.setPipelineFactory(pipelineFactory);
                SocketAddress address = new InetSocketAddress(remoteHost, remotePort);

                ChannelFuture cf = bootstrap.connect(address);
                cf.addListener(new ChannelFutureListener()
                {
                    public void operationComplete(ChannelFuture future)
                    {
                        // check to see if we succeeded
                        if (!future.awaitUninterruptibly().isSuccess())
                        {
                            System.out.println("Failed to connect to server.");
                            bootstrap.releaseExternalResources();
                        }
                        else
                        {
                            System.out.println("Connected.");
                            channel = future.getChannel();
                            channel.write(new Date());
                        }
                    }
                });
            }
        };
        thread.start();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-30 08:39:05

因为http://google.com不是主机,所以它是一个网址。

代码语言:javascript
运行
复制
ChannelFuture future = bootstrap.connect(new InetSocketAddress("google.com", 80));

编辑添加:如果主机名无法解析, InetSocketAddress不会抛出异常。你必须检查isUnresolved() -

代码语言:javascript
运行
复制
InetSocketAddress i = new InetSocketAddress("http://google.com", 80);

if (i.isUnresolved())
    System.out.println("Yup");

此外,future.getCause()可能已经阐明了更多的问题;我必须对其进行测试以进行验证。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14087158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档