0

I'm making a program that reads html from a website and pulls some things from the html. When I try to start my socket I get the UnknownHostException error. It happens when I call the InetAdress method as well. I've tried multiple ways of doing this including not using the inetaddress method and it doesn't help anything. Here's what the code looks like

try {
    InetAddress add = InetAddress.getByName(text);
} catch (UnknownHostException e1) { 

}

try {
    Socket socket = new Socket(text,80); 
    PrintWriter out = new PrintWriter(socket.getOutputStream()); 
}
7
  • 2
    Post what is text... Also, why do you need the InetAddress.getByName() call? Commented Mar 11, 2013 at 13:52
  • What is the value of text ?? Commented Mar 11, 2013 at 13:52
  • from a command line, can you ping the host you're trying to connect to? Commented Mar 11, 2013 at 13:52
  • text is the url from the user and I used inetaddress to try to see if it changed the error Commented Mar 11, 2013 at 13:56
  • @RileyBurris are you printing out the user input before attempting to make a socket connection, cause UnknownHostException seems clear enough (I means the hostname cannot be resolved) Commented Mar 11, 2013 at 13:59

1 Answer 1

2

Make sure you're passing just the hostname, and not the complete path to the resource you're trying to fetch. For instance, in your comment you wrote en.wikipedia.org/wiki/The_Elder_Scrolls_IV:_Oblivion ; to determine the host your Socket should connect to, pass just en.wikipedia.org to the InetAddress.getByName() method.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't find the answer to this question i have... Where do you then specify the path for the socket to connect to? I read that is done in the handshake part of the connection, but I can't find how to do it.
Sockets are for connecting to a specific TCP or UDP host and port. that's all. Sockets know nothing about paths, because TCP/UDP have no concept of paths. Higher-level protocols like HTTP, FTP, etc. deal with paths.
I see, so the "sockets" that are aware of paths are WebSockets which are built on sockets with understanding of their own standard for communication right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.