aspose file tools*
The moose likes Servlets and the fly likes socket's http connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "socket Watch "socket New topic
Author

socket's http connection

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I was wondering if a socket can have http connections, like Socket socket = new Socket("http://www.javaranch.com");
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cameron Park:
Hi, I was wondering if a socket can have http connections, like Socket socket = new Socket("http://www.javaranch.com");

Yes to the first half of the sentence, and no to the second half
A socket is a raw Internet connection (more precisely, a TCP/IP connection). It allows you to reliably send bytes to another computer and receive bytes from it.
HTTP is a server protocol built on top of a connection protocol like TCP/IP. The TCP/IP connection is used to send a message like "get page foo" whereupon the server responds with "yep, I have page foo, here it is" or "nope, no such thing" (the famous 404 status).
So yes, you can use sockets to establish a HTTP connection. The catch is, you'll have to implement the HTTP protocol yourself. Fine if you want to understand HTTP, but a bad case of reinventing the wheel otherwise. By now it's probably clear that the Socket class knows nothing about protocols like HTTP, SMTP, FTP, etc, and will not take a "http://www.foo.com" URL. It can use only the hostname and port.
- Peter

[This message has been edited by Peter den Haan (edited May 20, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
For connections where you specify the a protocol, such as HTTP, use URL. This handles the Socket stuff for you because it has to provide the protocol. For connections where you want to work directly with a socket, you have to specify host and port, etc. but you have to provide your own protocol handling.
Bill

------------------
author of:
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thanks, Peter and William!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: socket's http connection