| View previous topic :: View next topic |
| Author |
Message |
Siddharam Shingshetty Guest
|
Posted: Fri May 19, 2006 1:07 pm Post subject: socket problem |
|
|
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
|
Posted: Fri May 19, 2006 1:07 pm Post subject: Re: socket problem |
|
|
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
|
Posted: Fri May 19, 2006 7:07 pm Post subject: Re: socket problem |
|
|
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
|
Posted: Fri May 19, 2006 8:07 pm Post subject: Re: socket problem |
|
|
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 |
|
 |
|