0% found this document useful (0 votes)
21 views11 pages

Sliding Window Protocol

This document contains code for a sliding window protocol sender and receiver in Java. The sender code accepts a number of frames from the user, sends them to the receiver within the window size, and waits for acknowledgment. The receiver code receives frames, prints them, and sends acknowledgments back to the sender. There is also sample output showing a test run with 1 frame sent and received.

Uploaded by

mskumar_me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Sliding Window Protocol

This document contains code for a sliding window protocol sender and receiver in Java. The sender code accepts a number of frames from the user, sends them to the receiver within the window size, and waits for acknowledgment. The receiver code receives frames, prints them, and sends acknowledgments back to the sender. There is also sample output showing a test run with 1 frame sent and received.

Uploaded by

mskumar_me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SLIDING WINDOW

PROTOCOL
SENDER:
import java.net.*;
import java.io.*;
public class slidesender
{
public static void main(String a[]) throws Exception
{
ServerSocket ser=new ServerSocket(10);
try (Socket s = ser.accept())
{
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new
DataInputStream(s.getInputStream());
String sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream(s.getOutputStream());
System.out.print("enter the of frames");
1

nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("enter"+nf+"msg to be send\n");
for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.print("ACKNOWLEDGEMENT REC");
ano=Integer.parseInt(in1.readLine());
System.out.println("for"+ano+"frames");
sws+=nf;
}
else
{

System.out.println("the no of frames exceeds


windowsize");
break;
2

}
System.out.print("\n do you want sent");
ch=in.readLine();
p.println(ch);
}
while(ch.equals("yes"));
s.close();
}
}

RECEIVER
import java.net.*;
import java.io.*;
class slidreceiver
{
public static void main(String a[]) throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new
DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch;
System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=(rws-1))
{
for(i=1;i<=nf;i++)
{
4

rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("the received
frame"+rptr+"is:"+rbuf[rptr]);

System.out.println("\nacksent\n");
p.println(rptr+1);
rws+=nf;
}
}
else
break;
ch=in.readLine();
}
while(ch.equals("yes"));
}
}

OUTPUT

SENDER:
enter the of frames1
1msg to be send
WW
Ackreceive
RECEIVER:

The received frame1is:WW


Acksent

DATE CLIENT

import java.net.*;
import java.io.*;
class dateclient
{
public static void main(String args[])
{
Socket soc;
DataInputStream dis;
String sdate;
PrintStream ps;
try
{
InetAddress ia=InetAddress.getLocalHost();
soc=new Socket(ia,8020);
dis=new DataInputStream(soc.getInputStream());
sdate=dis.readLine();
System.out.println("the date in the server is:"+sdate);
ps=new PrintStream(soc.getOutputStream());
7

ps.println(ia);
}
catch(IOException e)
{
System.out.println("the expection is:"+e);

}
}
}

DATESERVER

import java.net.*;
import java.io.*;
class dateserver
{
public static void main(String a[]) throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new
DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch;
System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=(rws-1))
{
for(i=1;i<=nf;i++)
9

{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("the received
frame"+rptr+"is:"+rbuf[rptr]);

System.out.println("\nacksent\n");
p.println(rptr+1);
rws+=nf;
}
}
else
break;
ch=in.readLine();
}
while(ch.equals("yes"));
}
}

10

11

You might also like