Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

socket's http connection

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was wondering if a socket can have http connections, like Socket socket = new Socket("http://www.javaranch.com");
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Peter and William!
 
Your buns are mine! But you can have this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic