⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dllayerdemo.java

📁 实现两台主机通信
💻 JAVA
字号:
import java.net.*;

class LFrameLengthException extends Exception {
  public String toString() {
    return "Length of Frame is illegal";
  }
}

class LFrame extends Object {
  final int MAX_INFO_LEN = 2048;
  private byte buffer[] = new byte[3+MAX_INFO_LEN+2];
  private int framelength;
  
  public LFrame() {
    framelength = 0;
  }
  
  public LFrame(byte kind, byte seq, byte ack, byte infobuffer[],
    int infolength) throws LFrameLengthException {
    if ((infolength>MAX_INFO_LEN+2)||(infolength<2))
      throw new LFrameLengthException(); 
          
    framelength = infolength+3;
    int pos = 0;
    buffer[0] = kind;
    buffer[1] = seq;
    buffer[2] = ack;
    pos = 3;
    for (int i=0; i<infolength; i++){
      buffer[pos++] = infobuffer[i];
    }   
  }
  
  public byte[] getData(){
    return buffer;
  }
  public void setData(byte[] buffer){
    this.buffer = buffer;
  }
  public int getLength() {
    return framelength;
  }
  //
  public void setLength (int len) throws Exception {
    if (len >2053)
      throw new LFrameLengthException();
    this.framelength = len;
  }
  public byte getKind() {
    return buffer[0];
  }
  public byte getSeq() {
    return buffer[1];
  }
  public byte getAck() {
    return buffer[2];
  }
  public String toString() {
    return "kind: "+getKind()+"\n"
           +"Seq: "+getSeq()+"\n"
           +"Ack: "+getAck()+"\n"
           +"information: "+new String(buffer,3,framelength-3);
  }
}

class DLLayerConnectException extends Exception {
  String info;
  public DLLayerConnectException(String info) {
    this.info = info;
  }
  public String toString() {
    return "DLLayerConnectException: "+info;
  }
}
class DLLayer{
  final static int localPort = 1025;
  final static int remotePort = 1025;
  private static volatile double noise = 0;
  private static InetAddress remotehost;
  private DatagramSocket ds;
  private static boolean connected = false;
  
  //No Exception expected
  public DLLayer() {
    try{
      ds = new DatagramSocket(localPort);
    } catch (Exception e) {
    }  
  }
  
  //if rhost.length!=4, throws Exception: UnknownHostException
  public static void connect(byte[] rhost) throws Exception {
    remotehost = InetAddress.getByAddress(rhost);
    connected = true;
  }
  public static void disconnect() {
    connected = false;
  }
  public static void setNoise(double aNoise) {
    noise = aNoise;
  }
  public static int getLocalPort() {
    return localPort;
  }
  public boolean isConnected() {
    return connected;
  }
  
  private byte[] addNoise(byte[] buffer, int framelength) {
    int a,b;
    for(int i = 0; i<framelength; i++)
      for(int j =0; j<8; j++){
      	if (Math.random()<noise) {
      	  a = (int) Math.pow(2,j);
      	  b = buffer[i];
      	  b = a^b;
      	  buffer[i] = (byte) b;
      	}
      }
    return buffer;
  }
  //if connected is false , throw a DLLayerConnectException
  synchronized public void send(LFrame aFrame) throws Exception {
    if (connected) {
      DatagramPacket dp = new DatagramPacket(addNoise(aFrame.getData(),
        aFrame.getLength()),aFrame.getLength(),remotehost,remotePort);
      ds.send(dp);
      
    } else {
      throw new DLLayerConnectException("Host hasn't connected to physical line");
    }
  }
  
  //if connected is false, throw a DLLayerConnectException
  synchronized public void receive(LFrame aFrame) throws Exception {
    if (connected) {
      int buffer_size = 2053;
      byte buffer[] = new byte[buffer_size];
      DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
      ds.receive(dp);
      aFrame.setData(dp.getData());
      aFrame.setLength(dp.getLength());

    } else {
      throw new DLLayerConnectException("Host hasn't connected to physical line");
    }
  }  
}

public class DLLayerDemo {
  public static void main(String args[]) {
      try{
        DLLayer.setNoise(0.01);
        DLLayer d = new DLLayer();
      
        byte[] addr = new byte[4];
        addr[0]=(byte) 222; addr[1]=(byte) 28; addr[2]=(byte) 194; addr[3]=(byte) 179;             
        d.connect(addr);        
        //d.disconnect();
        byte kind = (byte) 234;
        byte seq = (byte) 0;
        byte ack = (byte) 2;
        byte buffer[] = new byte[2500];
        int len=10;
        for (int i=0; i<len; i++)
          buffer[i]= (byte)(i+'a');
          
        LFrame aFrame = new LFrame(kind,seq,ack,buffer,len);
        System.out.println("the length of the sendframe is: "+aFrame.getLength());
        
        //d.send(aFrame);
        System.out.println("Frame has been sent");
        LFrame bFrame = new LFrame();
        d.receive(bFrame);
        System.out.println("the length of the receiveframe is: "+bFrame.getLength());
        System.out.println(bFrame);
      } catch (Exception e){
      	System.out.println(e);
      }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -