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

📄 phylayer.java

📁 total是最后的版本。包括的全是最新的物理层和数据链路层。另外还有pro1和pro2的打包程序和调用它们形成的各自的界面程序
💻 JAVA
字号:
/*Definition Physical-layer.The Physical_Layer provide a no-error 
 *channel cheng for quan the translation of the frame between two 
 *contigeous points in the net
*/
package chq;
import java.io.*;
import java.net.*;

public class PhyLayer// extends JFrame implements ActionListener
{
   	public DatagramPacket sentPacket,receivedPacket;
   	public DatagramSocket socket;
   	public int localsocketnum;
   	public byte sdata[] = new byte[100];
   	public byte rdata[] = new byte[100];
   	
   	// set up GUI and DatagramSocket
   	public PhyLayer()
   	{
    
    try {socket = new DatagramSocket();}
    catch (SocketException se) {
          se.printStackTrace();
          System.exit(1);
    	}
	} // end Physical-layer constructor
	
	public void sendPackets(String aimaddress,int aimport,String s)
   	{
	try {
		sdata = s.getBytes();
		sentPacket=new DatagramPacket(sdata,sdata.length,
    				InetAddress.getByName(aimaddress),aimport);
    	socket.send( sentPacket );
    	}
    catch ( IOException Exception ) 
    	{Exception.printStackTrace();}		
   	}
   	
   // wait for packets to arrive from Server, display packet contents
   public String waitForPackets()
   {
      String receiveddata="";
      //while ( true ) { // loop forever
         try {
            // set up packet
            //byte rdata[] = new byte[100];
            receivedPacket = 
            	new DatagramPacket(rdata, rdata.length );

            socket.receive( receivedPacket ); // wait for packet
            
            receiveddata=new String(receivedPacket.getData(),
            						0,receivedPacket.getLength());
         }//end of try
 
         // process problems receiving or displaying packet
         catch( IOException exception ) {
            exception.printStackTrace();}
      //} // end while 
      return receiveddata;
   } // end method waitForPackets
   
   public String waitForTime()
   	{
      	String receiveddata="";
      	     	
		try {
			socket.setSoTimeout(5*1000);
            // set up packet
            //byte rdata[] = new byte[100];
            receivedPacket = 
            	new DatagramPacket(rdata, rdata.length );

            socket.receive( receivedPacket ); // wait for packet
            
            receiveddata=new String(receivedPacket.getData(),
            						0,receivedPacket.getLength());
            return receiveddata;
         }//end of try
 
		catch(IOException exception){
            return "timeout";}
            
	} // end method waitForPackets
	
	public void from_DatalinkLayer(String aimaddress,int aimport,String s)
   	{
		sendPackets(aimaddress,aimport,s);}
	
	public String to_DatalinkLayer()
	{
		return waitForPackets();}
	
	public String getLocalAddress(){
		String localhost="";
		try {
			localhost=InetAddress.getLocalHost().toString();
		}
    	catch ( IOException Exception ) 
    		{Exception.printStackTrace();}
    	
		return localhost;
	}
	
	public int getLocalPort(){
		return socket.getLocalPort();}
		
	public String getRPAddress(){
		String rPAddress="";
		StringBuffer address=new StringBuffer(
			receivedPacket.getAddress().toString());
		rPAddress=address.substring(1);
		return rPAddress;}
		
	public int getRPPort(){
		return receivedPacket.getPort();}
	
	public int getRPLength(){
		return receivedPacket.getLength();}
	
}  // end class Physical

⌨️ 快捷键说明

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