hhrzc
hhrzc

Reputation: 2050

java.net.UnknownHostException Cannot create socket. Java

I read some manuals and tried to create Socket, how it looks like in examples.

Socket socket = new Socket(InetAddress.getByName("http://google.com"), 80);

or

Socket socket = new Socket("http://google.com", 80);

In any case, I get the UnknownHostException:

java.net.UnknownHostException: http://google.com: Name or service not known

How will it work?

Upvotes: 1

Views: 1055

Answers (2)

Martin
Martin

Reputation: 2235

try www.google.com instead. I just tried telnet http://google.com and it doesn't connect. telnet www.google.com 80 does connect however.

Upvotes: 1

Sledro
Sledro

Reputation: 137

import java.io.*;
import java.net.*;

public class socket_client
{
    public static void main(String[] args) throws IOException 
    {
        Socket s = new Socket();
    String host = "www.google.com";

        try
        {
        s.connect(new InetSocketAddress(host , 80));
        }

        //Host not found
        catch (UnknownHostException e) 
        {
            System.err.println("Don't know about host : " + host);
            System.exit(1);
        }

        System.out.println("Connected");
    }
}

Upvotes: 1

Related Questions