Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Input in a Client program

Status
Not open for further replies.

JavaDude32

Programmer
Aug 25, 2001
180
US
I have a problem of freezing in my Client program when it attempts to read from a server. I've narrowed the problem down to one line I think, except I don't know how to fix it or why it's freezing. I've tried using a int as to make the code only try so many times, but it freezes in the while statement as the int (c) never prints to the screen. Any help or info on this would be very much appreciated.

while(((theLine = theReader.readLine()) != null) {
System.out.println(c);
theOutput.append("Server: " + theLine);
}
 
Well in more testing of the program, the app is shown to freeze at the end, the server output will be read and displayed until it's last line and then the app freezes. Still looking for a cause. Thanks in advance for any advice.
 
I'm not quite sure what you mean by "freezing" ... you should get some type of exception if the readLine() operation cannot be performed.

Have you tried expanding some of the code:

Code:
    String theLine = theReader.readLine();
    while( theLine != null )
    {
        System.out.println( c );
        theOutput.append( "Server: " + theLine );
        theLine = theReader.readLine();
    }

I'm thinking that maybe the condensed assignment in the while conditional is causing problems.

Best,
Adam Rice
 
It kind of depends on what kind of server you are reading from. Generally, when talking to a java server, the server signals the end of output by sending a -1 which should cause the reader to terminate the read. This may not be occuring if you are talking to a C/C++ daemon which has a different protocol. I've encountered these sorts of issues when talking to Linux daemons such as Telnet.

A little more code and more info about what you are talking to would be helpful.

FWIW, there is nothing wrong with your assignment statement (sorry Adam 8^( )
 
Okay, the purpose of the code there is to continually read input from a server and output to the client screen hence the reading of the input stream in the while statement, I've just thought of a way to possibly set the reading different. What I meant by freezing is that the client would stop reading and refuse to do anything else on the very last line of code. No error messages are reported from the catch part so I don't believe any exceptions are thrown. I don't believe is the server that is to blame as I have tested the client out on other servers and my own server(which works with other clients such as telnet). Thanks for the example AdamRice32, I'm going to try that instead to get the program to work, but I'm still in the dark as to why the program froze on the while statement.

Thanks
JavaDude32
 
Here is a listing of all the code for the connection and reading process:

fConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Integer myInt = new Integer(1);
int aPortNum = myInt.parseInt(thePort.getText());
String theAddress = theIp.getText();
try {
theSocket = new Socket(theAddress, aPortNum);
theReader = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));

theWriter = new BufferedWriter(new OutputStreamWriter(theSocket.getOutputStream()));

fStatus.setText("Connected");
Status.setBackground(java.awt.Color.green);
} catch (IOException a) {
theOutput.append("\nException: " + a.getMessage());
fStatus.setText("Disconnected");
fStatus.setBackground(java.awt.Color.red);
}
try {
String theLine = theReader.readLine();
while(theLine != null) {
theOutput.append(theLine);
theLine = theReader.readLine();
} catch (Exception k) {
System.out.println("message: " + k.getMessage());
System.exit(0);
}
}
});
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top