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;
}}

解决方案 »

  1.   

    这么说吧/我想实现一个接收message 客户端。上面代码可以接收server发过来的消息.服务端发一条客户端接收一条消息。其实这是一个即时更新的应用:用户修改了一条记录--》发送一条message到server->server把消息发给打开某个web页面的所有用户(client)。client端的web接收到消息后立即刷新页面(我用SAP的前台无法用脚本啥的)。如果修改频繁的话。web刷新过多。所以我想隔一段时间去刷新一次。所以我就想定时去查看str设置bl。
    然在我在客户端判断
    if(cc.getBl()){
       刷新数据
    }