AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

socket problem

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Siddharam Shingshetty
Guest





PostPosted: Fri May 19, 2006 1:07 pm    Post subject: socket problem Reply with quote



Hello,
Pls help me with following piece of code:


{...
..
ServerSocket ss=new ServerSocket(9000);
Socket s=ss.accept();
System.out.println("Server Ready to Accept");
BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));

String fname1=br.readLine();

....}

This code has uncertain execution.

The issue is br.readline() reads the input stream only sometimes (about
80% of the time).
What could be the problem and workaround.

Any help appreciated.

With regards,
Siddharam
Back to top
Gordon Beaton
Guest





PostPosted: Fri May 19, 2006 1:07 pm    Post subject: Re: socket problem Reply with quote



On 19 May 2006 05:44:52 -0700, Siddharam Shingshetty wrote:
Quote:
ServerSocket ss=new ServerSocket(9000);
Socket s=ss.accept();

Note that your server has *already* accepted the connection at this
point, *before* you display the following line:

Quote:
System.out.println("Server Ready to Accept");

BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));

String fname1=br.readLine();

...}

The issue is br.readline() reads the input stream only sometimes
(about 80% of the time). What could be the problem and workaround.

The methods you are using are reliable. The *way* you are using them
might not be.

What are you connecting to the server with?

What does it do the other 20%?

BufferedReader.readLine() blocks until one of the following occurs:

- end of line is read from the stream
- the stream is closed

Are you terminating your message with newline (or similar)?

Are you using a call like br.ready() before calling br.readLine()? If
you are, then don't.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Back to top
Siddharam Shingshetty
Guest





PostPosted: Fri May 19, 2006 7:07 pm    Post subject: Re: socket problem Reply with quote



Thanks gordon,

Please find my responses to your questions below:

Gordon Beaton wrote:
Quote:
On 19 May 2006 05:44:52 -0700, Siddharam Shingshetty wrote:
ServerSocket ss=new ServerSocket(9000);
Socket s=ss.accept();

Note that your server has *already* accepted the connection at this
point, *before* you display the following line:

System.out.println("Server Ready to Accept");

BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));

String fname1=br.readLine();

...}

The issue is br.readline() reads the input stream only sometimes
(about 80% of the time). What could be the problem and workaround.

The methods you are using are reliable. The *way* you are using them
might not be.


What are you connecting to the server with?

=> a php script client through php's socket api's.

Quote:

What does it do the other 20%?

=> the other 20% of the time br.readLine() returns 'null' but does not
throw any exceptions

Quote:

BufferedReader.readLine() blocks until one of the following occurs:

- end of line is read from the stream
- the stream is closed

Are you terminating your message with newline (or similar)?


=> Yes, I am taking care of that and its not blocking


Quote:

Are you using a call like br.ready() before calling br.readLine()? If
you are, then don't.

=> No, I am not using

Quote:

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Back to top
Gordon Beaton
Guest





PostPosted: Fri May 19, 2006 8:07 pm    Post subject: Re: socket problem Reply with quote

On 19 May 2006 12:00:02 -0700, Siddharam Shingshetty wrote:
Quote:
=> the other 20% of the time br.readLine() returns 'null' but does
not throw any exceptions

That means that your client has disconnected.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.