Posted by : Unknown
Friday, 26 June 2015
Sender Side Program
import java.io.*;
import java.net.*;
class slidingSender
{
public void process()
{
try
{
ServerSocket server= new ServerSocket(2500);
Socket client = server.accept();
BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter pr=new PrintWriter(client.getOutputStream(),true);
String buf[]=new String[8];
int index=0,sws=8,rws;
String ch;
do
{
System.out.println("Enter the number of frame send to the receiver:");
int nf=Integer.parseInt(user.readLine());
pr.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter the "+nf+" Message send to the receiver");
for(int i=0;i<nf;i++)
{
buf[i]=user.readLine();
pr.println(buf[i]);
}
sws=sws-nf;
int ackno=Integer.parseInt(br.readLine());
System.out.println("Acknowledge Received For the Frame"+ackno);
sws=sws+nf;
}
else
{
System.out.println("SENDER WINDOW SIZE EXCEEDS");
}
System.out.println("Do You Want To Continue(y/n)");
ch=user.readLine();
pr.println(ch);
}
while(ch.equals("y"));
}
catch(Exception e){}
}
public static void main(String arg[])
{
slidingSender s= new slidingSender();
s.process();
}
}
import java.net.*;
class slidingSender
{
public void process()
{
try
{
ServerSocket server= new ServerSocket(2500);
Socket client = server.accept();
BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter pr=new PrintWriter(client.getOutputStream(),true);
String buf[]=new String[8];
int index=0,sws=8,rws;
String ch;
do
{
System.out.println("Enter the number of frame send to the receiver:");
int nf=Integer.parseInt(user.readLine());
pr.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter the "+nf+" Message send to the receiver");
for(int i=0;i<nf;i++)
{
buf[i]=user.readLine();
pr.println(buf[i]);
}
sws=sws-nf;
int ackno=Integer.parseInt(br.readLine());
System.out.println("Acknowledge Received For the Frame"+ackno);
sws=sws+nf;
}
else
{
System.out.println("SENDER WINDOW SIZE EXCEEDS");
}
System.out.println("Do You Want To Continue(y/n)");
ch=user.readLine();
pr.println(ch);
}
while(ch.equals("y"));
}
catch(Exception e){}
}
public static void main(String arg[])
{
slidingSender s= new slidingSender();
s.process();
}
}
Receiver Side Program
import java.io.*;
import java.net.*;
class slidingReceiver
{
public void process()
{
try
{
Socket client = new Socket(InetAddress.getLocalHost(),2500);
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(client.getOutputStream(),true);
int index=0,rws=8;
String buf[]=new String[8];
String ch;
while(true)
{
do
{
int nf=Integer.parseInt(br.readLine());
if(nf<rws-1)
{
for(int i=0;i<nf;i++)
{
buf[index]=br.readLine();
System.out.println(index);
System.out.println(buf[index]);
index=(index +1)%rws;
System.out.println(index);
}
rws = rws - nf;
System.out.println("Acknowledge Sent To Sender");
pr.println(index);
rws=rws+nf;
}
else
{
break;
}
ch=br.readLine();
}
while(ch.equals("y"));
}
}
catch(Exception e){}
}
public static void main(String arg[])
{
slidingReceiver r= new slidingReceiver();
r.process();
}
}
import java.net.*;
class slidingReceiver
{
public void process()
{
try
{
Socket client = new Socket(InetAddress.getLocalHost(),2500);
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedReader user=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(client.getOutputStream(),true);
int index=0,rws=8;
String buf[]=new String[8];
String ch;
while(true)
{
do
{
int nf=Integer.parseInt(br.readLine());
if(nf<rws-1)
{
for(int i=0;i<nf;i++)
{
buf[index]=br.readLine();
System.out.println(index);
System.out.println(buf[index]);
index=(index +1)%rws;
System.out.println(index);
}
rws = rws - nf;
System.out.println("Acknowledge Sent To Sender");
pr.println(index);
rws=rws+nf;
}
else
{
break;
}
ch=br.readLine();
}
while(ch.equals("y"));
}
}
catch(Exception e){}
}
public static void main(String arg[])
{
slidingReceiver r= new slidingReceiver();
r.process();
}
}
How to run:
- First run Sender program
- Then next run Receiver program
- Give input from Sender side
Sender Side Output:
Enter the number of frame send to the receiver:
3
Enter the 3 Message send to the receiver
hi
good
evening
Acknowledge Received For the Frame3
Do You Want To Continue(y/n)
n
3
Enter the 3 Message send to the receiver
hi
good
evening
Acknowledge Received For the Frame3
Do You Want To Continue(y/n)
n
Receiver Side Output:
0
hi
1
1
good
2
2
evening
3
Acknowledge Sent To Sender
Related Posts :
- Back to Home »
- Client Server Program »
- Client / Server Program With Acknowledgement