Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Tuesday, 27 September 2011

Stop and Wait in Java


Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientstop
{              public static void main(String args[])
                {try{while(true)
                                {String str1=null;
                                BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                                Socket clsct=new Socket("127.0.0.1",139);
                                DataInputStream din=new DataInputStream(clsct.getInputStream());
                                DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());

                                System.out.println("Enter the frame:");
                                String str=in.readLine();
                                dout.writeBytes(str+'\n');
                                clsct.setSoTimeout(5000);
                                str1=din.readLine();
                                System.out.println(str1);
                                clsct.close();          }}
                catch (Exception e)
                {System.out.println(e);}}}
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverstop
{public static void main(String args[])
                {try{       String s="0";
                                int count=0;
                                ServerSocket obj=new ServerSocket(139);
                                while(true)
                                {              Socket obj1=obj.accept();
                                                DataInputStream din=new DataInputStream(obj1.getInputStream());
                                                DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                                String str=din.readLine();
                                                if(count==0)
                                                {              count+=1;
                                                                if(str.equals("0"))
                                                                {              s="1";
                                                                                dout.writeBytes("send frame no "+s+'\n');                      }
                                                                else if(str.equals("1"))
                                                                {              s="0";                                                    
                                                                                dout.writeBytes("send frame no "+s+'\n');                      }
                                                                else if(str.equals("-1"))
                                                                                break;
                                                }
                                                else
                                                {              count+=1;
                                                                if(str.equals(s))
                                                                {              if(str.equals("0"))
                                                                                {              s="1";
                                                                                                dout.writeBytes("send frame no "+s+'\n');      }
                                                                                else if(str.equals("1"))
                                                                                {              s="0";                                                    
                                                                                                dout.writeBytes("send frame no "+s+'\n');      }}
                                                                else if(str.equals("-1"))
                                                                                break;                    }                              }
                                obj.close();            }
                catch(Exception e)
                {              System.out.println(e);         }
                }
}
Output
Client
Enter the frame:
1
send frame no 0
Enter the frame:
0
send frame no 1
Enter the frame:
1
send frame no 0
Enter the frame:
0
send frame no 1
Enter the frame:
1
send frame no 0
Enter the frame:
-1

No comments:

Post a Comment

Followers