• Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket vs URL

 
Costa Lamona
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

if I wrote

URL url = new URL(httpRequest); // work fine

but if I wrote

Socket socket = new Socket(httpRequest, 80); // UnknbownHostException is fired

why? giving that



if I replaced ecs.amazonaws.co.uk with an IP in numbers, the same exception is fired, I always thought that Socket can connect to any thing
and at the end every connection is actually handled by a Socket.

So what I have missed

Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Defining a URL doesn't mean that the URL exsits on the internet. The code just checks for valid syntax.
Executing a Socket will check if the URL exists and give an error message if not.
 
Costa Lamona
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the URL exists because I just copy it and past it to a browser.

I think the reason is Socket is expecting something convertible to IP, not something which contains a HTTP Get to a webservice.

so I will alter my question, can Socket constructor takes a GET request, rather than host name?

I think the answer is no, but please verify that

Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the API doc for the Socket Constructor?
What part of it do you NOT understand as there are more than a few?

Is the GET request part of the HTTP protocol? I think you can use Socket for more than one type of protocol. The protocol is specified in the URL.

To connect to a site with a protocol/URL see the openConnection method.
[ August 31, 2008: Message edited by: Norm Radder ]
 
Costa Lamona
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you miss the point because Socket constructor is taking a host not a resource, and that was the problem in first place.

Thanks
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Costa Lamona:
I think you miss the point because Socket constructor is taking a host not a resource, and that was the problem in first place.

Thanks


The socket class is designed to take a hostname, but it has no knowledge of the http protocol, GET parameters, or a bunch of other stuff taken care of by the URL class. The only thing that it knows about is the port, and/or whether the transport is TCP or UDP.

Henry
 
T.A. Nguyen
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work.

Socket socket = new Socket("ecs.amazonaws.co.uk", 80);

As other tried to explained, Socket only take in an IP or a host name as string or as the IPAddress object. Or you can use the URL to decode and get the host and port from there as:

URL url = new URL(httpRequest);
Socket socket = new Socket(url.getHost(), (-1 == url.getPort()) ? 80 : url.getPort());


for more info:

http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html

Have fun,

[ September 02, 2008: Message edited by: T.A. Nguyen ]

[ September 02, 2008: Message edited by: T.A. Nguyen ]
[ September 02, 2008: Message edited by: T.A. Nguyen ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!