public class ClientConn implements Runnable {
public static boolean bl=false;public void run(){
String host ="endor";
int port = 4444;
int receiveInternal =100;
int stillaliveFactor =10;
Socket cilent = null;
InputStreamReader reader=null;
OutputStreamWriter writer=null;
BufferedReader input=null;
PrintWriter output=null;
Socket client=null;
String str=null;
try {
client=new Socket(host,port);
input=new BufferedReader(new InputStreamReader(client.getInputStream()));
output=new PrintWriter(new OutputStreamWriter(client.getOutputStream()),true);
System.out.println("receive from server:");
while (true) {
Thread.sleep(receiveInternal);
str=input.readLine();
//******************************************
//我想在这里做一些操作。每隔一定时间如一分钟去查看str的数据(是不是也要开个线程?)。
//if(str!=null){
// setBl(true);
//}else{
// setBl(false);
//}
//这样做的目的是如果我开启了这样一个线程。
//ClientConn cc=new ClientConn();
//Thread t=new Thread(cc);
//t.start()后。任时候时间如果我可以能过cc.getBl()==true/false去做我需要的处理。
//***********************************************
} } catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
} finally{
try {
client.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static boolean getBl() {
return bl;
}
public static void setBl(boolean b) {
bl = b;
}}
public static boolean bl=false;public void run(){
String host ="endor";
int port = 4444;
int receiveInternal =100;
int stillaliveFactor =10;
Socket cilent = null;
InputStreamReader reader=null;
OutputStreamWriter writer=null;
BufferedReader input=null;
PrintWriter output=null;
Socket client=null;
String str=null;
try {
client=new Socket(host,port);
input=new BufferedReader(new InputStreamReader(client.getInputStream()));
output=new PrintWriter(new OutputStreamWriter(client.getOutputStream()),true);
System.out.println("receive from server:");
while (true) {
Thread.sleep(receiveInternal);
str=input.readLine();
//******************************************
//我想在这里做一些操作。每隔一定时间如一分钟去查看str的数据(是不是也要开个线程?)。
//if(str!=null){
// setBl(true);
//}else{
// setBl(false);
//}
//这样做的目的是如果我开启了这样一个线程。
//ClientConn cc=new ClientConn();
//Thread t=new Thread(cc);
//t.start()后。任时候时间如果我可以能过cc.getBl()==true/false去做我需要的处理。
//***********************************************
} } catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
} finally{
try {
client.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static boolean getBl() {
return bl;
}
public static void setBl(boolean b) {
bl = b;
}}
然在我在客户端判断
if(cc.getBl()){
刷新数据
}