SOLVED Problem - BufferedReader call in thread handler

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

SOLVED Problem - BufferedReader call in thread handler

Postby zogieosagie » Tue Dec 01, 2009 3:23 pm

Hello everyone,

please I'd like any help on this problem I have been trying to fix for hours.
When I make a call to the readLine() method of BufferedReader within a thread handler my application hangs up indefinitely. The problem occurs in the line " fromServer = in.readLine();" in bold within the handleMessage() method in the code snippet below. When I remove calls to the Bufferedreader methods the thread executes properly without problems. I have also attached the eclipse project file to this post.

Any help will be appreciated. Thanks very much in advance.


public class TCPClient implements Runnable {

static String fromServer = null;
static String fromUser;

private Socket kkSocket = null;
private PrintWriter out = null;
private BufferedReader in = null;

Handler myHandler2 = new Handler(){
public void handleMessage (Message m)
{
switch (m.what)
{
case 2:
try {
fromServer = in.readLine();
if (fromServer.equals("Bye."))
{
try
{
closeAllConnections();
}catch (IOException e){

}
}

if (fromUser != null)
{
out.println(fromUser);
fromUser = null;
}
} catch (IOException e) {
fromServer = "Okokoko";
}

break;
default:
break;
}
super.handleMessage(m);

}

private void closeAllConnections() throws IOException {
out.close();
in.close();
kkSocket.close();
}

};

public void run() {

try {
kkSocket = new Socket("111.111.1.111", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: 111.111.1.111.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: 111.111.1.111.");
System.exit(1);
}

while (!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(150);

}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
Message m2 = new Message();
m2.what = 2;
myHandler2.sendMessage(m2);
}
}

public static String obtainNewText() {
if (fromServer != null)
{
return fromServer;
}
else return "No Data!";
}

public static void sendNewText(String s) {
fromUser = s;
}


}
Attachments
TrialTCP.zip
(37.04 KiB) Downloaded 31 times
Last edited by zogieosagie on Mon Dec 07, 2009 2:54 pm, edited 1 time in total.
zogieosagie
Freshman
Freshman
 
Posts: 2
Joined: Fri Sep 25, 2009 5:07 pm

Top

Postby grzegorz.zajac » Tue Dec 01, 2009 5:30 pm

Here are my suggestions for your code:

1) use BufferedInputStream instead of BufferedReader.
2) consider using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Message m2 = Message.obtain();
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
instead of
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Message m2 = new Message();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

3) put some debugs to check if the message was consumed by the handler (or maybe an exeception was thrown?)

Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby zogieosagie » Fri Dec 04, 2009 2:57 am

Hi Grzesiek,
thanks a lot for your response. I finally found the reason for the problem. the reason is that the readLine() (in.readline() ) method blocks until a new line is received. All I needed to do was use in.ready() to check if a new line was available before calling readLine().

Regards,
Osagie
zogieosagie
Freshman
Freshman
 
Posts: 2
Joined: Fri Sep 25, 2009 5:07 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Exabot [Bot] and 10 guests