0

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

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();
    }
4

1 回答 1

4

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

ChannelFuture future = bootstrap.connect(new InetSocketAddress("google.com", 80));

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

InetSocketAddress i = new InetSocketAddress("http://google.com", 80);

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

此外,future.getCause()可能对这个问题有更多的了解;我必须对其进行测试以验证。

于 2012-12-30T00:39:04.767 回答