Search code examples
javasocketsniochannel

UnresolvedAddressException socket channel


I'm trying to create a socket between my computer and a remote server but I'm getting an UnresolvedAddressException error when I do this :

    InetSocketAddress hostAddress = new InetSocketAddress("http://www.google.com", 80);
    SocketChannel serverChannel = SocketChannel.open(hostAddress);

Why is that?


Solution

  • You don't have to use http:// or https://, you just use:

    InetSocketAddress hostAddress = new InetSocketAddress("www.google.com", 80);
    

    Or you can use the IP address instead :

    InetSocketAddress hostAddress = new InetSocketAddress("216.58.210.228", 80);