令牌环协议 java

Token ring protocol java

我正在尝试使此源代码正常工作以了解网络编程。它最初通过三个 classes 发送数据包,但即使使用相同的代码也不再发送数据包。当在 Tokenclient1 class 中输入 no 并且数据包没有被发送到第二个客户端时,就会出现问题。

知道为什么不再有效吗?

    import java.net.*; 

    public class TokenServer {     
        public static void main(String agrs[])throws Exception         {                         

            while(true)             {             
                Server sr=new Server();             
                sr.recPort(8000);             
                sr.recData();             
                }         
            } 
        } 
    class Server {         
        boolean hasToken=false;     
        boolean sendData=false;     
        int recport;         

        void recPort(int recport)     {         
            this.recport=recport;     
            }     

        void recData()throws Exception     {         
            byte buff[]=new byte[256];         
            DatagramSocket ds;         
            DatagramPacket dp;         
            String str;                 
            ds=new DatagramSocket(recport);         
            dp=new DatagramPacket(buff,buff.length);         
            ds.receive(dp);         
            ds.close();                 
            str=new String(dp.getData(),0,dp.getLength());         
            System.out.println("The message is "+str);     
            } 
    }







    import java.io.*; 
    import java.net.*; 
    public class TokenClient1 {     
        public static void main(String arg[]) throws Exception         {             
            InetAddress lclhost;             
            BufferedReader br;             
            String str="";             
            TokenClient12 tkcl,tkser;             
            //boolean hasToken;             
            //boolean setSendData;                         

            while(true)             {             
                lclhost=InetAddress.getLocalHost();             
                tkcl = new TokenClient12(lclhost);             
                tkser = new TokenClient12(lclhost);                    
                //tkcl.setSendPort(9001);             
                tkcl.setSendPort(9004);             
                tkcl.setRecPort(8002);             
                lclhost=InetAddress.getLocalHost();             
                tkser.setSendPort(9000);             

                if(tkcl.hasToken == true)             {                 
                    System.out.println("Do you want to enter the Data --> YES/NO");                 
                    br=new BufferedReader(new InputStreamReader(System.in));                 
                    str=br.readLine();                 
                    if(str.equalsIgnoreCase("yes"))                 {                       
                        System.out.println("ready to send");                     
                        tkser.setSendData = true;                     
                        tkser.sendData();                     
                        tkser.setSendData = false;                 
                        }                 
                    else if(str.equalsIgnoreCase("no"))                 {                     
                        System.out.println("i m in else");                     
                        //tkcl.hasToken=false;                     
                        tkcl.sendData();                     
                        tkcl.recData();                 
                        System.out.println("i m leaving else");                 
                        }             
                    }             
                else             {             
                    System.out.println("ENTERING RECEIVING MODE...");                 
                    tkcl.recData();             
                    }     
                } 
            } 
        }                    
    class TokenClient12 {     
        InetAddress lclhost;     
        int sendport,recport;     
        boolean hasToken = true;     
        boolean setSendData = false;     
        TokenClient12 tkcl,tkser;     
        TokenClient12(InetAddress lclhost)     {                 
            this.lclhost = lclhost;     
            }         
        void setSendPort(int sendport)     {         
            this.sendport = sendport;     
            }     void setRecPort(int recport)        {         
                this.recport = recport;     
                }     
            void sendData() throws Exception         {         
                BufferedReader br;         
                String str="Token";         
                DatagramSocket ds;         
                DatagramPacket dp;                     

                if(setSendData == true)         {             
                    System.out.println("sending ");             
                    System.out.println("Enter the Data");             
                    br=new BufferedReader(new InputStreamReader(System.in));             
                    str = "ClientOne....." + br.readLine();             
                    System.out.println("now sending");                             
                    }             
                ds = new DatagramSocket(sendport);             
                dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);             
                ds.send(dp);             
                ds.close();             
                setSendData = false;             
                hasToken = false;     
                }         

            void recData()throws Exception     {         
                String msgstr;         
                byte buffer[] = new byte[256];         
                DatagramSocket ds;         
                DatagramPacket dp;         
                ds = new DatagramSocket(recport);         
                dp = new DatagramPacket(buffer,buffer.length);         
                ds.receive(dp);         
                ds.close();         
                msgstr = new String(dp.getData(),0,dp.getLength());         
                System.out.println("The data is "+msgstr);                 

                if(msgstr.equals("Token"))             {                 
                    hasToken = true;             
                    }     
                }     
    }








        import java.io.*; 
        import java.net.*; 



        public class TokenClient2 {     
            static boolean setSendData ;     
            static boolean hasToken ;     

            public static void main(String arg[]) throws Exception         {             

                InetAddress lclhost;             
                BufferedReader br;             
                String str1;             
                TokenClient21 tkcl;             
                TokenClient21 ser;             

                while(true)             {             
                    lclhost=InetAddress.getLocalHost();             
                    tkcl = new TokenClient21(lclhost);             
                    tkcl.setRecPort(8004);                    
                    tkcl.setSendPort(9002);             
                    lclhost=InetAddress.getLocalHost();             
                    ser = new TokenClient21(lclhost);             
                    ser.setSendPort(9000);             
                    System.out.println("entering if");                

                    if(hasToken == true)             {                 
                        System.out.println("Do you want to enter the Data --> YES/NO");                 
                        br=new BufferedReader(new InputStreamReader(System.in));                 
                        str1=br.readLine();                 
                        if(str1.equalsIgnoreCase("yes"))                 {                        
                            System.out.println("ignorecase");                     
                            ser.setSendData = true;                     
                            ser.sendData();                     }                 
                        else if(str1.equalsIgnoreCase("no"))                 {                     
                            tkcl.sendData();                     
                            hasToken=false;                 
                            }             
                        }             
                    else             {             
                        System.out.println("entering recieving mode");                    
                        tkcl.recData();                 
                        hasToken=true;            
                        }         
                    }     
                } 
            } 
        class TokenClient21 {     
            InetAddress lclhost;     
            int sendport,recport;     
            boolean setSendData = false;     
            boolean hasToken = false;     
            TokenClient21 tkcl;     
            TokenClient21 ser;         
            TokenClient21(InetAddress lclhost)     {                 
                this.lclhost = lclhost;     }        
            void setSendPort(int sendport)     {         
                this.sendport = sendport;     
                }     
            void setRecPort(int recport)        {         
                this.recport = recport;     
                }     
            void sendData() throws Exception     {         
                System.out.println("case");         
                BufferedReader br;         
                String str="Token";         
                DatagramSocket ds;         
                DatagramPacket dp;                 

                if(setSendData == true)         {             
                    System.out.println("Enter the Data");             
                    br=new BufferedReader(new InputStreamReader(System.in));             
                    str = "ClientTwo....." + br.readLine();         }             
                ds = new DatagramSocket(sendport);             
                dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);             
                ds.send(dp);             
                ds.close();             
                System.out.println("Data Sent");             
                setSendData = false;             
                hasToken = false;             
                }         
            void recData()throws Exception     {         
                String msgstr;         
                byte buffer[] = new byte[256];         
                DatagramSocket ds;         
                DatagramPacket dp; 
                ds = new DatagramSocket(recport);        
                //ds = new DatagramSocket(4000);         
                dp = new DatagramPacket(buffer,buffer.length);         
                ds.receive(dp);         

                ds.close(); 
                msgstr = new String(dp.getData(),0,dp.getLength());         
                System.out.println("The data is "+msgstr);         

                if(msgstr.equals("Token"))             {                 
                    hasToken = true;             
                    }     
                }     

        } 

您的程序运行正常。请确保您 运行 同时运行所有三个程序,这意味着分别打开 3 个不同的命令提示符和 运行 以下命令。

java 令牌服务器

java TokenClient1

java TokenClient2