473,811 Members | 3,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.App let;
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;
InputStreamRead er isr = null;
BufferedReader in = null;
PrintWriter out = null;
Button uptimeButton,wh oButton,dateBut ton;
TextArea ta;

public void init()
{
setBackground(C olor.white);

uptimeButton = new Button("uptime" );
uptimeButton.ad dActionListener (this); add(uptimeButto n);
whoButton = new Button("who"); whoButton.addAc tionListener(th is);
add(whoButton);
dateButton = new Button("date"); dateButton.addA ctionListener(t his);
add(dateButton) ;
ta = new TextArea("",20, 80);
ta.setEditable( false);
add(ta);
}

public void start()
{
try
{
s = new Socket(server,p ort);
isr = new InputStreamRead er(s.getInputSt ream());
in = new BufferedReader( isr);
out = new PrintWriter(s.g etOutputStream( ),true);

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

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

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

if (b==uptimeButto n) { query = "<query>upt ime</query>"; }
else if (b==whoButton) { query = "<query>who </query>"; }
else { query = "<query>dat e</query>"; }

try
{
out.println(que ry);
out.flush();
try
{
while ((c = in.read())!=-1)
{
ta.append(Strin g.valueOf((char ) c));
}
}
catch (IOException ioe)
{
ta.append("I/O error: "+ioe+"\n") ;
}
repaint();
}
catch(Exception e)
{
ta.append("appl et error: "+e+"\n");
}
}
}
Jul 17 '05 #1
2 3563
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
11469
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 http://java.sun.com/sfaq/#read: ----- Sun's appletviewer allows applets to read files that are named on the access control list for reading. The access control list for reading is null by default, in the JDK. You can allow applets to read directories or files...
2
3484
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 Microsoft VM. But when I use Suns' VM the applet hangs after opening the dialog. And you have to
1
7093
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 http://www.unix.org.ua/orelly/java-ent/servlet/ch10_02.htm of how to construct e.g. a day time server, gives me: java.io.StreamCorruptedException: invalid stream header
0
2912
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 same machine (for dev and testing). Here is the C# code snippet: 1 string clientAddr = "http://127.0.0.1:22225/"; 2 try 3 { 4 webreq = (HttpWebRequest)WebRequest.Create( clientAddr );
2
7376
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 and hangs. The Java console offers no clues. The server serves the aspx page that contains the control, view source shows the expected html. However, the applet does nothing. I'm integrating Quadbase ExpressReport into a site. TIA. <%@ Page...
2
2694
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 os.popen3() hanging under Windows in this group before. I stumbled on a case where a piece of code works in some occasions and hangs consistently given different data. It performs exactly the same on 3 different computers running Windows:
37
4981
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 times but it hangs in different places with the same data. The offending code follows. ReadFile(char *csFileName) { float fFloat1, fFloat2;
1
2824
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 include Java Applet jar too, for (int i = 0; i < numRequestsToBeHandled; i++) { HttpListenerResponse response = null;
0
1273
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 include Java Applet jar too, for (int i = 0; i < numRequestsToBeHandled; i++) { HttpListenerResponse response = null;
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.