Re: How can I detect a carriage return using java.net
EJP <esmond.not.pitt@not.bigpond.com> wrote or quoted in
Message-ID: <exF_g.51086$rP1.23887@news-server.bigpond.net.au>:
Red Orchid wrote:
As I know as,
'null' do not necessarily indicate that server has finished
sending data. 'null' may be returned when unexpected
network error occurs (ex: unexpected connection close).
This is 100% incorrect.
Let's assume that you have an external modem for networking.
Execute the class 'Test'.
If you turn off the modem in the middle of receiving data,
this code returns 'null', not an exception.
The 'null' do not indicate that a server has finished
sending data because the modem was turned off.
<code>
public class Test {
public static void main(String[] args) throws Exception {
process();
}
static void process() {
String url = "http://Your Server/Your Data File";
BufferedReader br = null;
try {
HttpURLConnection uc;
URL u = new URL(url);
uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
if (code != 200) {
System.out.println("Err Code: " + code);
return;
}
InputStreamReader ir;
InputStream in;
in = uc.getInputStream();
ir = new InputStreamReader(in);
br = new BufferedReader(ir);
while(true) {
if (br.readLine() == null) {
System.out.println("-< null >-");
break;
}
System.out.println("Receiving ...");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (br != null) {
try {
br.close();
}
catch (Exception e) {
}
}
}
}
}
</code>