473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

applet 'hangs' while reading from stream

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
2 3532
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Paul | last post by:
Hello, I've been reading up on security in Java Applets and whilst I understand the concept, I can't successfully get my applet to read a file on my local machine. I discovered from...
2
by: Michael | last post by:
Hello Currently I'm migrating an applet from Java 1.1 to 1.3. Targetplattform: Microsoft Internet Explorer 5.x with Sun's Java VM 1.3.1 Problem: The attached applet works fine with the...
1
by: Nick Nygaard | last post by:
Hi everyone, I'm using O'Reilly's servlet API to make an object based HTTP communication between an Applet and a servlet. Following their own tutorial 10.2.3. Object-based HTTP Communication...
0
by: Nathan | last post by:
This is a copy of a message at microsoft.public.dotnet.framework.clr: THE CODE: I'm using an HttpWebResponse object to send an HTTP POST to a Java server I have written and are running on the...
2
by: MarkMurphy | last post by:
Is there a limitation in ASP.NET in this regard? From the aspx code below, I can successfully call a Java applet. If I try the identical thing in a user control ascx however, the control loads...
2
by: Pierre Rouleau | last post by:
Hi all, I have a consistent test case where os.popen3() hangs in Windows. The system hangs when retrieving the lines from the child process stdout. I know there were several reports related to...
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
1
by: Reg | last post by:
I have a following code in HttpRequestListener's ProcessRequest method which is a Windows Console Application. I'm trying to read html page with browser (Opera) and html file contains tags to...
0
by: Reg | last post by:
I have a following code in HttpRequestListener's ProcessRequest method which is a Windows Console Application. I'm trying to read html page with browser (Opera) and html file contains tags to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.