Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading from Socket  RSS feed

 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to read the HTTP response from google.com (or any other site it seems). This is the code I am using (largely copied from an onjava.com example):



which produces the output:
false
test

i assumed the address.isReachable returned false because i couldn't ping google but it seems that the code never makes it past in.read. after that call the code just hangs. i turned my firewall off... am i doing something wrong?
[ July 04, 2007: Message edited by: ra carter ]
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An empty line is needed at end of HTTP query.

Another problem is that

does not work. The conversion from int to char will change -1 to 65535 (and it happens before the test for -1).
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any particular reason you're not using the HttpUrlConnection class (which knows how to do HTTP) for this?
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, the particular reason is because i did not know about that class. by the way i stole most of this code from an onjava.com example.

i tried using readLine but it still wasn't working and i then read somewhere else that there was a bug with this method when reading from sockets. What is the proper way to read from the InputStream?

I will try java.net.HttpURLConnection. Thanks. But should this still work with Sockets?

by the way, isn't the capitalization of that class off? shouldn't it be: HttpUrlConnection? Why didn't they capitalize HTTP but they did capitalize URL?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried using readLine but it still wasn't working

Ah, I missed that part in the original post. Don't println to a socket explains well the problems arising when using readers/writers with sockets.
I will try java.net.HttpURLConnection. But should this still work with Sockets?

The class uses sockets internally, so you no longer need to worry about them.
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am kind of confused about HttpURLConnection since it is an abstract class and i am not sure how to instantiate it. but assuming that i wanted to use sockets anyway... is there any way to correct that code? i replaced the use of out.println with out.print("...\r\n") without much luck as well....
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am kind of confused about HttpURLConnection since it is an abstract class and i am not sure how to instantiate it.

Something like

without much luck as well....

What does or does not happen in your code? In which lines do things start to deviate from what you would expect to happen?
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what is unexpected is that it seems that i never enter this loop or go beyond the in.read() method invocation. the application hangs at that line of code and i must forcibly stop the program.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you haven't acted on Vlado's comment.

Also, note that read will block until there is input available. And, since the server will not respond until you send two CR/LF combinations in succession, that call to read will block. That's how HTTP works. HttpUrlConnection takes care of all this, but if you want to use sockets, you have to implement this yourself.
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as using a socket... sending two newlines as the output and using readLine (instead of the (char)in.read()) seems to have solved all problems
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!