我写了一个客户端的程序,用out.writeObject(data);上传对象,可是在服务器接收的总是一样的数据。????
例如:
客户端上传数据的代码: while(true){
Object serverMsg=in.readObject(); }else if(((Max)serverMsg).isUpdata==true){
data.data=this.updata;
data.size=this.updataSize;
 for(int i=0;i<data.size;i++){
 System.out.print(data.data[i]+" ");
 }
 System.out.println();

    out.writeObject(data);
    out.flush();
}
}
例如红色的代码:
第一次打出:9917, 9918, 9926, 9943, 9943, 9949, 9954, 9980, 9984, 9997
第二次打出:9727 9736 9753 9770 9777 9791 9802 9806 9810 9813 
while(true){
try{
Object  clientMsg=in.readObject();
if(clientMsg instanceof Data){//发信息获得客户端的数据
System.out.println("输出一下:"+Arrays.toString(((Data)clientMsg).data));
this.insertNum((Data)clientMsg);
Max msg=new Max();
msg.isUpMax=true;
out.writeObject(msg);
}
上面绿色的代码,是打出从客户端传来的data里面的数据:
两次打出来的都是:
9917, 9918, 9926, 9943, 9943, 9949, 9954, 9980, 9984, 9997
9917, 9918, 9926, 9943, 9943, 9949, 9954, 9980, 9984, 9997
再多几次时,打出来的也是
9917, 9918, 9926, 9943, 9943, 9949, 9954, 9980, 9984, 9997
请大家帮忙看看,先写了O(∩_∩)O~

解决方案 »

  1.   

    请问eclipse里怎么调试soket我像调试正常的程序那样设置断点调试,可是每次DEbug就报错???
      

  2.   

    你检查一个in.readObject()这个方法吧。
      

  3.   

    server的一个处理客户端的线程的run方法  
     /**
        * Handle实现runnable接口后的run方法
        */
    public void run() {
    // TODO Auto-generated method stub
    try{
    this.server.getHandlers().addElement(this);
    //监听由此客户端来的输入流

    while(true){
    try{
    Object  clientMsg=in.readObject();
    if(clientMsg instanceof Data){//发信息获得客户端的数据
    System.out.println("输出一下:"+Arrays.toString(((Data)clientMsg).data));
    this.insertNum((Data)clientMsg);
    Max msg=new Max();
    msg.isUpMax=true;
    out.writeObject(msg);
    }else if(clientMsg instanceof Max){//发信息获得客户端数据的最大值
    Max msg=new Max();
    System.out.println("Max"+((Max)clientMsg).max);
    if((((Max)clientMsg).max>this.server.getMin())||(this.server.getSize()!=100)){
    msg.isUpdata=true;
    out.writeObject(msg);
    }else {
    msg.isUpdata=false;
    out.writeObject(msg);
    }
    }
    }catch(Exception e){
    this.server.getWindow().jta.append("ssssssssssdddd");
    break;
    }
    }
    }catch(Exception e){
    System.out.println("出现异常");
    }
    finally{//出现异常后,执行该段代码

    }
    }
      

  4.   

    客户端的run线程的run方法
    public void run() {
    // TODO Auto-generated method stub
    // 接收服务器返回的消息
    try{
    String s="";
    Max max=new Max();
    Data data=new Data();
        this.updata=this.getData();
        max.max=this.updata[this.updataSize-1];
            System.out.println("client——run"+max.max);
        out.writeObject(max);
        out.flush();
    while(true){
    Object serverMsg=in.readObject();
    if(serverMsg instanceof Max){
    if(((Max)serverMsg).isUpMax==true){// 上传最大值
        this.updata=this.getData();
        max.max=this.updata[this.updataSize-1];
        System.out.println(Thread.currentThread().getName()+"输出要上传的最大值:"+max.max);
        out.writeObject(max);
        out.flush();
    }else if(((Max)serverMsg).isUpdata==true){// 上传数据
    data.data=this.updata;
    data.size=this.updataSize;
    System.out.println(Thread.currentThread().getName()+"输出要上传的数据");
     for(int i=0;i<data.size;i++){
     System.out.print(data.data[i]+" ");
     }
     System.out.println();
        out.writeObject(data);
        out.flush();
    }
    }
    jta.append("sever:"+s);
    jta.append("\n");
    }
    }catch(Exception ex){
    System.out.println(ex.getMessage()+"异常 run");
      }
    }
      

  5.   

    学习一下,对了,我有计划学习Socket编程,请问有哪些好建议呢,谢谢
      

  6.   

    我把Client中Data类对象的声明,放在if语句里面后就好用了,请问这是怎么回事呢??