By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

applet 'hangs' while reading from stream

P: n/a
Hi I'm doing a project for school and wrote an applet that makes a socket
connection to a server (smae host as webserver) that was setup for this
project. In the applet there are 3 buttons and by pressing one of them it
triggers a specific query, like getting the server uptime, date or who (is
online). The problem is that when I press one of the buttons it send the
query to the server, reads from the BufferedReader. This goes all well but
then it 'hangs'. I think the problem is in the line:

while ((c = in.read())!=-1)

If I replace the while by a single read() then it works just fine, except
that I need to output the whole BufferedReader not just 1 char. The docs
specify that when the method read() reaches the end of the buffer it
returns -1. But it seems that c never gets to be -1. I've been googling for
several hours now and according to the docs on java.sun.com it whould be
working. What's wrong here?

Thanks for help!
Jonathan

--------------------------

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class test extends Applet implements ActionListener
{
private static int port = 2000;
private static String server = "xxxxxxxxx";
int c;
Socket s;
InputStreamReader isr = null;
BufferedReader in = null;
PrintWriter out = null;
Button uptimeButton,whoButton,dateButton;
TextArea ta;

public void init()
{
setBackground(Color.white);

uptimeButton = new Button("uptime");
uptimeButton.addActionListener(this); add(uptimeButton);
whoButton = new Button("who"); whoButton.addActionListener(this);
add(whoButton);
dateButton = new Button("date"); dateButton.addActionListener(this);
add(dateButton);
ta = new TextArea("",20,80);
ta.setEditable(false);
add(ta);
}

public void start()
{
try
{
s = new Socket(server,port);
isr = new InputStreamReader(s.getInputStream());
in = new BufferedReader(isr);
out = new PrintWriter(s.getOutputStream(),true);

catch(Exception e)
{
ta.append("applet error: "+e+"\n");
}
}

public void stop()
{
try
{
s.close();
}
catch(Exception e)
{
ta.append("applet error: "+e+"\n");
}
}

public void actionPerformed(ActionEvent evt)
{
String query;
Object b = evt.getSource();
boolean newLine = true;

if (b==uptimeButton) { query = "<query>uptime</query>"; }
else if (b==whoButton) { query = "<query>who</query>"; }
else { query = "<query>date</query>"; }

try
{
out.println(query);
out.flush();
try
{
while ((c = in.read())!=-1)
{
ta.append(String.valueOf((char) c));
}
}
catch (IOException ioe)
{
ta.append("I/O error: "+ioe+"\n");
}
repaint();
}
catch(Exception e)
{
ta.append("applet error: "+e+"\n");
}
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 14 May 2004 03:16:22 +0200, Jonathan wrote:
The problem is that when I press one of the buttons it send the
query to the server, reads from the BufferedReader. This goes all
well but then it 'hangs'. I think the problem is in the line:

while ((c = in.read())!=-1)

If I replace the while by a single read() then it works just fine,
except that I need to output the whole BufferedReader not just 1
char. The docs specify that when the method read() reaches the end
of the buffer it returns -1. But it seems that c never gets to be
-1.


I'm not sure what you mean by "the end of the buffer", but read() will
return -1 when the end of the *stream* (aka EOF) is reached, which
won't happen until the remote end of the connection is *closed*.
What's going on at the other end?

The code you've posted seems to assume that the same connection will
be reused more than once, which implies that the server doesn't close
it after responding. There's absolutely nothing wrong with that, but
it means that you can't use EOF as a delimiter. There must be some
other way to detect the end of the server response, i.e. a newline, an
empty line, a special token, a certain length or something like that.

/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
Jul 17 '05 #2

P: n/a
Hi Gordon,
I'm not sure what you mean by "the end of the buffer", but read() will
return -1 when the end of the *stream* (aka EOF) is reached, which
won't happen until the remote end of the connection is *closed*.
What's going on at the other end?

The code you've posted seems to assume that the same connection will
be reused more than once, which implies that the server doesn't close
it after responding. There's absolutely nothing wrong with that, but
it means that you can't use EOF as a delimiter. There must be some
other way to detect the end of the server response, i.e. a newline, an
empty line, a special token, a certain length or something like that.
I wrongly assumed that read would return -1 if there was nothing more to
read in the buffer. Like you said the server sends a 'token' consisting of a followed by a space. Now the loop executes until the last 2 characters are

'> '.

Thanks!
Jonathan
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.