DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page Doubt in java socket programming
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jun 2008
Default Doubt in java socket programming - 06-12-2008, 12:57 PM

I am trying to run this simple codepiece

Socket mySock = new Socket("www.javalobby.org",80);
InputStream ins = mySock.getInputStream();
BufferedReader bufr = new BufferedReader(new InputStreamReader(ins));
String aLine = bufr.readLine();

but the program while running, gets hanged at the readLine and never ends. What is the reason?
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: May 2008
Default 06-13-2008, 05:59 AM

hi,

you have to send a request for it to send the response, otherwise it would not give you a reply.
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Jun 2008
Default 06-16-2008, 06:10 PM

Hi ya

as micro_lecnuj says, the server is waiting for you to send something to it. Because its HTTP, you need to send a GET message to get the page.

Two things you should do are:
1. set the timeout on the socket (setsoTimeout) so that the socket will timeout if the server doesn't send anything (so that the code doesn't hang if you have a comms prombem)
2. get hold of the the output stream from the socket and send a string like "GET /X" remember to add a blank line at the end of the response.

Hope that this helps

Andy
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java socket performance xcolwell Java 1 01-25-2009 08:44 AM
a small doubt in java ragskvin Java 1 09-26-2008 07:03 AM
Eclipse RCP doubt. Ayusman Eclipse 0 05-15-2008 08:56 AM
Java Programming Help ogre23 Java 0 04-13-2008 04:13 PM
Java Concurrent Programming Floetic Java 2 02-27-2008 02:07 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com