| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4223 人关注过本帖
标题:滑动窗口协议实现疑问
只看楼主 加入收藏
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
 问题点数:0 回复次数:11 
滑动窗口协议实现疑问
用java实现滑动窗口协议,就是先读一个文件,把他传到一个数据结构里,然后读出来,然后发送acknowledgement,然后在发送一段,比如0 , 7 ,5,6,8,9,1 定义窗口大小如果是3的话,传到第三次就是一个循环,我现在的问题是只能传一次,不知道怎么收到acknowledgement了,用的是java io包的fileinputstream我的代码如下,请高手指点一下改怎么改进一下,想了好久了还是不行,谢谢!
import java.util.*;
import *;
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();
    }
}
搜索更多相关主题的帖子: 疑问 协议 窗口 滑动 
2007-12-12 06:13
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
不太明白楼主想实现什么

可惜不是你,陪我到最后
2007-12-12 11:09
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
网络第二层,datalink layer,flow control, sliding windows protocol的功能!

2007-12-12 20:01
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
我这里有材料
求助了,搞了一个月了,还是不行!

BSc_4_-_ADN_-_Assignment_2_-_sliding_window_protocol_2007.pdf (44.92 KB)

2007-12-12 20:02
Starlove
Rank: 1
等 级:新手上路
帖 子:169
专家分:0
注 册:2007-4-26
收藏
得分:0 
没有看明白
2007-12-12 21:51
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 

2007-12-12 23:50
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
求助,我改进了代码,简单了,但是无法运行到底,就是简单的生产者消费者问题,我理解为,但是的发了6个数据只能收到3个,有人帮我看看代码吗?
import java.util.*;
import *;
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();
    }
}

2007-12-13 09:07
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
为什么会数据丢失?

2007-12-13 09:28
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
还有怎么施行大循环把数据全部传玩,求助了,我在北欧现在是早上2点不到,太累了想不出了,懂的帮我看看把,谢谢了!

2007-12-13 09:30
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
每次发送或者读取的时候,应该在明确发送了多少,读入了多少,而并不是一个简单的read就完了,你要知道read的返回值是多少,因为这不一定能全部读满你的缓冲区

可惜不是你,陪我到最后
2007-12-13 15:54
快速回复:滑动窗口协议实现疑问
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022700 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved