Last active
April 12, 2017 04:55
-
-
Save aurghyadip/2269f4f2e6c8e85cb6767262737d1e22 to your computer and use it in GitHub Desktop.
Stop and Wait Protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| import java.net.*; | |
| public class Sender{ | |
| Socket sender; | |
| ObjectOutputStream out; | |
| ObjectInputStream in; | |
| String packet,ack,str, msg; | |
| int n,i=0,sequence=0; | |
| Sender(){} | |
| public void run(){ | |
| try{ | |
| BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); | |
| System.out.println("Waiting for Connection...."); | |
| sender = new Socket("localhost",2005); | |
| sequence=0; | |
| out=new ObjectOutputStream(sender.getOutputStream()); | |
| out.flush(); | |
| in=new ObjectInputStream(sender.getInputStream()); | |
| str=(String)in.readObject(); | |
| System.out.println("reciver > "+str); | |
| System.out.println("Enter the data to send...."); | |
| packet=br.readLine(); | |
| n=packet.length(); | |
| do{ | |
| try{ | |
| if(i<n){ | |
| msg=String.valueOf(sequence); | |
| msg=msg.concat(packet.substring(i,i+1)); | |
| }else if(i==n){ | |
| msg="end";out.writeObject(msg);break; | |
| }out.writeObject(msg); | |
| sequence=(sequence==0)?1:0; | |
| out.flush(); | |
| System.out.println("data sent>"+msg); | |
| ack=(String)in.readObject(); | |
| System.out.println("waiting for ack.....\n\n"); | |
| if(ack.equals(String.valueOf(sequence))){ | |
| i++; | |
| System.out.println("receiver > "+" packet recieved\n\n"); | |
| }else{ | |
| System.out.println("Time out resending data....\n\n"); | |
| sequence=(sequence==0)?1:0; | |
| }}catch(Exception e){} | |
| }while(i<n+1); | |
| System.out.println("All data sent. exiting."); | |
| }catch(Exception e){} | |
| finally{ | |
| try{ | |
| in.close(); | |
| out.close(); | |
| sender.close(); | |
| } | |
| catch(Exception e){} | |
| } | |
| } | |
| public static void main(String args[]){ | |
| Sender s=new Sender(); | |
| s.run(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment