File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting data from socket

 
Marco Galea
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, i'm very very new to networking and was wondering if it's possible using this code to "listen" or get some data of what's happening using this code..... when i compile and run nothing is outputted (im using port 80 since it's http so i suppose browsing). Thanks for any info....

 
Tim Moores
Bartender
Pie
Posts: 2488
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess: Don't println to a Socket. It applies to readLine as well.
 
Marco Galea
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Just a guess: Don't println to a Socket. It applies to readLine as well.


Thanks Tim it was a very interesting read.
If i understood properly the article says we should not use println but just print.... but the article also says that's it's a common practice in Mac.

I tried to change every println to print only but still have no output. could it be because i'm running on windows ??

Thanks again and have a good night
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the client and server are the same platform, then that println() issue is probably not the problem. Additionally, when you say you "changed to print()", did you include an explicit "\r\n" as the article indicated?

The real problem, however, if you're connecting to a web server that's using the HTTP protocol, is that you can't just connect and start reading. You have to tell it what you want. For instance, when you point your browser to http://www.coderanch.com/forums, what's happening is that the browser is opening a socket to www.coderanch.com and then writing the request GET /forums\r\n[tt]. An HTTP server doesn't send anything until it receives a request from the client.

You could alter your code to send the appropriate [tt]GET
request, or you could use the HttpXyz classes in the core API, or a third party HTTP library. If all you want to do is just fetch a simple page, then sending the GET yourself is probably fine. For anything more complex though--HTTP headers, other request types, handling redirects, etc., you'll want as much pre-rolled as you can get.
 
Marco Galea
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,
yes i did include the \r\n but still i didn't get anything, but the way you explained it makes sense about sending a request. I don't want to do anything specific, i just want to play with the code to see what's happening and which piece of code is doing what, after all im still a beginner and i suppose it's all part of the fun and learning......

Thanks again and are the classes called HttpXyz so as to read about them ?

btw yes, the client and server are the same platform.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic