Java 令牌环协议

Java 令牌环协议,java,tcp,token,Java,Tcp,Token,为了学习网络编程,我正在尝试让这些源代码正常工作。它最初是通过三个类发送数据包的,但即使使用相同的代码也不再是了。当在Tokenclient1类中输入no之后,数据包没有被发送到第二个客户端时,就会出现问题 你知道为什么它不再工作了吗 import java.net.*; public class TokenServer { public static void main(String agrs[])throws Exception

为了学习网络编程,我正在尝试让这些源代码正常工作。它最初是通过三个类发送数据包的,但即使使用相同的代码也不再是了。当在Tokenclient1类中输入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

感谢您的帮助。我在线程“main”Java.net.BindException:Address已在使用中时收到错误,例如,我没有在后台运行的程序的旧实例正在使用端口(netstat-aon | find“9090”)。如果程序中使用的端口正被某人使用,您可以更改为不同的端口。它应该可以工作。将我的端口更改为正在侦听的端口,但我仍然收到错误!非常困惑我肯定你做错了什么。请重新启动系统并再次运行程序。出现什么问题?什么错误?什么?