当前位置: 代码迷 >> Java相关 >> 滑动窗口协议实现疑问
  详细解决方案

滑动窗口协议实现疑问

热度:581   发布时间:2007-12-12 06:13:23.0
滑动窗口协议实现疑问
用java实现滑动窗口协议,就是先读一个文件,把他传到一个数据结构里,然后读出来,然后发送acknowledgement,然后在发送一段,比如0 , 7 ,5,6,8,9,1 定义窗口大小如果是3的话,传到第三次就是一个循环,我现在的问题是只能传一次,不知道怎么收到acknowledgement了,用的是java io包的fileinputstream我的代码如下,请高手指点一下改怎么改进一下,想了好久了还是不行,谢谢!
import java.util.*;
import java.io.*;
class Frames{
    String type;
    int sequenceNo;
    int datasize;
    byte data[];
    public Frames(String t,int s,byte[] d){
        type = t;
        sequenceNo = s;
        data = d;
    }
    public String gettype(){
        return type;
    }
    public int getsequenceNo(){
        return sequenceNo;
    }
    public int getdatasize(){
        return datasize;
    }
    public byte[] getdata(){
        return data;
    }
}
class Send implements Runnable{
    Frames inframe;
    Frames ackframe;
    String s;
    Buffer<Frames> buffer;
    Vector<Frames> iframe ;
    Vector<Frames> aframe ;
    int nextStart=0;
    int windowStart=0;
    int windowEnd;
    int sequenceN;
    byte a[] = new byte[10];
    File f=new File("D:\\s\\test.txt");
    FileInputStream in =null;
    public Send(Buffer<Frames> b,Vector<Frames> iframe,Vector<Frames> aframe){
        buffer = b;
        this.iframe = iframe;
        this.aframe = aframe;
    }
    public void run(){
            try{
            in = new FileInputStream(f);
        }catch(FileNotFoundException e1){
            System.out.println("can not find file");
            System.exit(-1);
        }
        try{
            in.read(a);
        }catch(IOException e2){
            System.out.println("fail");
            System.exit(-1);
        }
        int windowEnd=(windowStart+(buffer.windowsize)-1)%(buffer.windowsize);
        while(nextStart>=windowStart&&nextStart<windowEnd){
                sequenceN = nextStart;
                inframe = new Frames("Information",sequenceN,a);
                System.out.println("Creat "+sequenceN+""+inframe.gettype()+" frame");
                nextStart=nextStart+1;
                buffer.put(inframe);
                System.out.println("Send NO."+sequenceN+" frame");
            }
            try{
                 in.close();
             }catch(IOException e3){}
             try{
                 Thread.sleep(2000);
             }catch(InterruptedException ea){}
    }
}


class Receive implements Runnable{
    byte c[];
    Frames inframe;
    Frames ackframe;
    String s;
    Buffer<Frames> buffer;
    Vector<Frames> iframe;
    Vector<Frames> aframe;
    int windowStart=0;
    int windowEnd;
    int nextStart=0;
    int ackN;
    File f = new File("D:\\s\\new.txt");
    FileOutputStream out = null;
    public Receive(Buffer<Frames> b,Vector<Frames> iframe,Vector<Frames> aframe){
        buffer = b;
        this.iframe = iframe;
        this.aframe = aframe;
    }
    public void run(){
        try{
         out = new FileOutputStream(f);    
        }catch(FileNotFoundException e){
        System.out.println("fail");
        System.exit(-1);
        }
         int windowEnd=(windowStart+(buffer.windowsize)-1)%(buffer.windowsize);
         while(nextStart>=windowStart&&nextStart<windowEnd){
            ackN=nextStart;
            ackframe = buffer.get();
            System.out.println("Recevie NO."+ackN+" frame");
            nextStart=nextStart+1;
            c = buffer.get().getdata();
           try{
               out.write(c);    
            }catch(IOException e2){}
            }
          try{
               out.close();
          }catch(IOException ea){}
    }
}
class Buffer<Frames>{
    Vector<Frames> iframe;
    Vector<Frames> aframe;
    public int windowsize;
    private int size = 0;
    public Buffer(Vector<Frames> iframe, Vector<Frames> aframe,int w){
        this.iframe=iframe;
        this.aframe=aframe;
        windowsize = w;
       
    }
    synchronized void put(Frames f){
        while(size == windowsize){
            try{
                wait();
            }
            catch(InterruptedException e){}
        }
        iframe.add(f);
        size++;
        if(size == 0)notify();
    }
    synchronized Frames get(){
        while(size == 0){
            try{
                wait();
            }
            catch(InterruptedException e){}
        }
        Frames temp = iframe.get(0);
            iframe.remove(0);
            size--;
            if(size==windowsize)notify();
        return temp;
    }
}

public class Assignment{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the size of window");
        int t =input.nextInt();
        Vector<Frames> i = new Vector<Frames>();
        Vector<Frames> a = new Vector<Frames>();
        Buffer b=new Buffer(i,a,t);
        new Thread(new Send(b,i,a)).start();
        new Thread(new Receive(b,i,a)).start();
    }
}
搜索更多相关的解决方案: 疑问  协议  窗口  滑动  

----------------解决方案--------------------------------------------------------
不太明白楼主想实现什么
----------------解决方案--------------------------------------------------------
网络第二层,datalink layer,flow control, sliding windows protocol的功能!
----------------解决方案--------------------------------------------------------
我这里有材料
求助了,搞了一个月了,还是不行!
----------------解决方案--------------------------------------------------------
没有看明白

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
求助,我改进了代码,简单了,但是无法运行到底,就是简单的生产者消费者问题,我理解为,但是的发了6个数据只能收到3个,有人帮我看看代码吗?
import java.util.*;
import java.io.*;
class Frames{
    String type;
    int sequenceNo;
    int datasize;
    byte data[];
    public Frames(String t,int s,byte[] d){
        type = t;
        sequenceNo = s;
        data = d;
    }
    public String gettype(){
        return type;
    }
    public int getsequenceNo(){
        return sequenceNo;
    }
    public int getdatasize(){
        return datasize;
    }
    public byte[] getdata(){
        return data;
    }
}
class Send implements Runnable{
    Frames inframe;
    Frames ackframe;
    String s;
    Bufferframe buffer;
    int nextStart=0;
    int windowStart=0;
    int windowEnd;
    int sequenceN;
    byte a[] = new byte[100];
    File f=new File("D:\\s\\new.jpg");
    FileInputStream in =null;
    public Send(Bufferframe b){
        buffer = b;
    }
    public void run(){
            try{
            in = new FileInputStream(f);
        }catch(FileNotFoundException e1){
            System.out.println("can not find file");
            System.exit(-1);
        }
        try{
            in.read(a);
        }catch(IOException e2){
            System.out.println("fail");
            System.exit(-1);
        }
       
           //int    windowEnd=(windowStart+(buffer.windowsize)-1)%(buffer.windowsize);
            //    while(nextStart>=windowStart&&nextStart<windowEnd)
            for(int k =0;k<buffer.windowsize;k++){
                sequenceN = nextStart;
                inframe = new Frames("Information",sequenceN,a);
                System.out.println("Creat "+sequenceN+""+inframe.gettype()+" frame");
                nextStart=nextStart+1;
                buffer.put(inframe);
                System.out.println("Send NO."+sequenceN+" frame");
            }
        

            try{
                 in.close();
             }catch(IOException e3){}
             
    }
}


class Receive implements Runnable{
    byte c[];
    Frames inframe;
    Frames ackframe;
    String s;
    Bufferframe buffer;

    int windowStart=0;
    int windowEnd;
    int nextStart=0;
    int ackN;
    File f = new File("D:\\s\\q.jpg");
    FileOutputStream out = null;
    public Receive(Bufferframe b){
        buffer = b;
    }
    public void run(){
        try{
         out = new FileOutputStream(f);    
        }catch(FileNotFoundException e){
        System.out.println("fail");
        System.exit(-1);
        }
         //int windowEnd=(windowStart+(buffer.windowsize)-1)%(buffer.windowsize);
         //while(nextStart>=windowStart&&nextStart<windowEnd)
         for(int k=0;k<buffer.windowsize;k++){
            ackN=nextStart;
            ackframe = buffer.get();
            System.out.println("Recevie NO."+ackN+" frame");
            nextStart=nextStart+1;
            c = buffer.get().getdata();
           try{
               out.write(c);    
            }catch(IOException e2){}
            }
          try{
               out.close();
          }catch(IOException ea){}
    }
}
class Bufferframe{
    public int windowsize=6;
    public int index = 0;
    Frames[] arr = new Frames[windowsize];
    synchronized void put(Frames f){
        while(index == arr.length){
            try{
                this.wait();
            }
            catch(InterruptedException e){}
        }
        this.notify();
        arr[index]=f;
        index++;
    }
    synchronized Frames get(){
        while(index == 0){
            try{
                this.wait();
            }
            catch(InterruptedException e){}
        }
        this.notify();
        index--;
        return arr[index];
    }
}

public class Assignment{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
    //    System.out.println("Please enter the size of window");
    //    int t =input.nextInt();
  
        Bufferframe b=new Bufferframe();
        new Thread(new Send(b)).start();
        new Thread(new Receive(b)).start();
    }
}
----------------解决方案--------------------------------------------------------
为什么会数据丢失?
----------------解决方案--------------------------------------------------------
还有怎么施行大循环把数据全部传玩,求助了,我在北欧现在是早上2点不到,太累了想不出了,懂的帮我看看把,谢谢了!
----------------解决方案--------------------------------------------------------
每次发送或者读取的时候,应该在明确发送了多少,读入了多少,而并不是一个简单的read就完了,你要知道read的返回值是多少,因为这不一定能全部读满你的缓冲区
----------------解决方案--------------------------------------------------------
  相关解决方案