This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a new Socket and parameters to the Socket Constructor

 
John McDowell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Socket socket = new Socket("127.0.0.1",5000);

HeadFirst Java says that the ip address that I have put in double quotes can be replaced by the domain name.Could somebody tell me how that would work.With a real life example. Please don't redirect me to some site with tons of api descriptions. Just give me a nice example, like the examples in HFJ. Can I connect to the Google server and send a search query as a String to it using printWriter object
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can, but not on port 5000, and not quite as easily as you seem to assume. HTTP generally uses port 80 (or port 443 for HTTPS), and you would need to send and receive HTTP over the socket. So it's not like a web browser where you can simply enter the URL and get back a web page.

But you don't have to implement all that - the java.net package has classes that do all this for you, like URL, URLConnection and HttpURLConnection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic