wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Getting data from socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Getting data from socket " Watch "Getting data from socket " New topic

Getting data from socket

Marco Galea
Ranch Hand

Joined: Mar 30, 2011
Posts: 30
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

Joined: Sep 21, 2011
Posts: 2408
Just a guess: Don't println to a Socket. It applies to readLine as well.
Marco Galea
Ranch Hand

Joined: Mar 30, 2011
Posts: 30
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

Joined: Jan 03, 2004
Posts: 6109

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

Joined: Mar 30, 2011
Posts: 30
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.
Consider Paul's rocket mass heater.
subject: Getting data from socket
Similar Threads
Applet + Application communication
Threaded Application won't run.
No Data - Socket
Multiple sockets cause Threads Hangin