This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Socket vs URL  RSS feed

 
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
 
Master Rancher
Posts: 3276
33
  • 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
Master Rancher
Posts: 3276
33
  • 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
 
author
Sheriff
Posts: 23586
138
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
 
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!