0

サーバーとの非同期ハンドシェイクを取得するために次のコードがありますが、Webサイトに接続できない理由がわかりません。

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 に答える