/****** GO BACK Server ******/
import java.io.*;
import java.net.*;
import java.util.LinkedList;
public class GoBackNserver {
public static void main(String args[]) {
try {
ServerSocket s = new ServerSocket(7079);
Socket con = s.accept();
BufferedReader fromclient = new BufferedReader(new InputStreamReader(con.getInputStream()));
DataOutputStream toclient = new DataOutputStream(con.getOutputStream());
OutputStream output = con.getOutputStream();
LinkedList < String > list = new LinkedList < String > ();
String f = fromclient.readLine();
int nf = Integer.parseInt(f);
System.out.println("Number of packets = " + nf);
String flow = fromclient.readLine();
int ack = 0;
System.out.println("Flow is "+flow);
if(flow.contains("n") || flow.contains("N")){
for (int i = 0; i < nf; i++) {
if (i == 2) {
list.add(i, "killed");
ack = 2;
continue;
}
String n = fromclient.readLine();
int num = Integer.parseInt(n);
String packet = fromclient.readLine();
list.add(num, packet);
if (ack >= 2) {
System.out.println("Not received packet" + ack + "..... Hence Discardingpacket" + num);
num=num-1;
}else
{
System.out.println("received packet" + num + " = " + packet);
}
if (list.contains("killed")) {
toclient.writeBytes(ack + "\n");
num = ack-1;
}
System.out.println("sending ack" + num);
toclient.flush();
toclient.writeBytes(num + "\n");
toclient.flush();
}
System.out.println(list);
for (int r = ack; r < nf; r++) {
String n = fromclient.readLine();
int num = Integer.parseInt(n);
String packet = fromclient.readLine();
System.out.println("received packet" + num + " = " + packet);
list.add(num, packet);
}
}else{
int k=0;
for (int i = 0; i < nf; i++) {
String n = fromclient.readLine();
int num = Integer.parseInt(n);
String packet = fromclient.readLine();
System.out.println("received packet" + num + " = " + packet);
list.add(num, packet);
k=i;
}
if(k==(nf-1)){
for(int j=0;j int num=j;
System.out.println("sending ack" + num);
toclient.flush();
toclient.writeBytes(num + "\n");
toclient.flush();
}
}
}
fromclient.close();
output.close();
con.close();
s.close();
} catch (Exception e) {
System.out.println(" " );
}
}
}
/************ Go Back Client *************/
import java.io.*;
import java.net.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList;
public class GoBackNclient {
public static void main(String args[]) {
long start = System.currentTimeMillis();
try {
Socket con = new Socket("localhost", 7079);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream toserver = new DataOutputStream(con.getOutputStream());
BufferedReader fromserver = new BufferedReader(new InputStreamReader(con.getInputStream()));
LinkedList < String > list = new LinkedList < String > ();
System.out.println("Enter number of packets to be transmitted");
String n = in .readLine();
int nf = Integer.parseInt(n);
toserver.writeBytes(nf + "\n");
for (int i = 0; i < nf; i++) {
System.out.println("Enter packet" + i + "to be transmitted");
String packet = in .readLine();
list.add(packet);
}
System.out.println("\nYou have entered following data "+list+"\n");
System.out.println("Please enter Y for successful sending or N for packet loss");
String flow = in.readLine();
System.out.println("input read is "+flow);
toserver.writeBytes(flow+"\n");
int discard = 0;
int ack = 0,w=4,wind=0,p=0,q=0;
if(flow.contains("n") || flow.contains("N")){
for (int i = 0; i < nf; i++) {
p=i;
wind = windowSize(p,q);
if (i == 2) {
discard = 2;
System.out.println("Packet lost is"+i);
q=w;
wind = windowSize(p,q);
wind++;
q=wind;
continue;
} else {
String str = list.get(i);
if (discard == 2) str = "Discarded";
toserver.writeBytes(i + "\n");
toserver.writeBytes(str + "\n");
}
wind++;
q=wind;
String ak = fromserver.readLine();
ack = Integer.parseInt(ak);
if(ack==2)
System.out.println("Ack received for "+(ack-1));
else
System.out.println("Ack received for " + ack);
}
wind=0;q=4;
for (int r = ack; r < nf; r++) {
p=r;
wind = windowSize(p,q);
q=0;
System.out.println("Resending packet" + r);
String str = list.get(r);
toserver.writeBytes(r + "\n");
toserver.writeBytes(str + "\n");
wind++;
q=wind;
}
}else
{ q=0;
for (int i = 0; i < nf; i++) {
p=i;
wind = windowSize(p,q);
System.out.println("Sending packet" + i);
String str = list.get(i);
toserver.writeBytes(i + "\n");
toserver.writeBytes(str + "\n");
wind++;
q=wind;
if(i==(nf-1)){
for(int j=0;j String ak = fromserver.readLine();
ack = Integer.parseInt(ak);
ack=j;
System.out.println("Ack received for " + ack);
}
}
}
}
fromserver.close();
toserver.close();
con.close();
} catch (Exception e) {
System.out.println("\n\n ERROR" + e);
}
long end = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.println("\nExecution time is " + formatter.format((end - start) / 1000d) + " seconds");
}
public static int windowSize(int p,int wind)throws InterruptedException
{
int i=p,w=i+4,windw=wind;
LinkedList < Integer > list1 = new LinkedList < Integer > ();
if(i==0 || windw==4){
System.out.println("\nBased on the window size, packets to be sent ");
for(int q=i;q windw=0;
list1.add(q);
}
System.out.println(list1);
if(windw==0){
Thread.currentThread();
Thread.sleep(5000);
}
}
return windw;
}
}
import java.io.*;
import java.net.*;
import java.util.LinkedList;
public class GoBackNserver {
public static void main(String args[]) {
try {
ServerSocket s = new ServerSocket(7079);
Socket con = s.accept();
BufferedReader fromclient = new BufferedReader(new InputStreamReader(con.getInputStream()));
DataOutputStream toclient = new DataOutputStream(con.getOutputStream());
OutputStream output = con.getOutputStream();
LinkedList < String > list = new LinkedList < String > ();
String f = fromclient.readLine();
int nf = Integer.parseInt(f);
System.out.println("Number of packets = " + nf);
String flow = fromclient.readLine();
int ack = 0;
System.out.println("Flow is "+flow);
if(flow.contains("n") || flow.contains("N")){
for (int i = 0; i < nf; i++) {
if (i == 2) {
list.add(i, "killed");
ack = 2;
continue;
}
String n = fromclient.readLine();
int num = Integer.parseInt(n);
String packet = fromclient.readLine();
list.add(num, packet);
if (ack >= 2) {
System.out.println("Not received packet" + ack + "..... Hence Discardingpacket" + num);
num=num-1;
}else
{
System.out.println("received packet" + num + " = " + packet);
}
if (list.contains("killed")) {
toclient.writeBytes(ack + "\n");
num = ack-1;
}
System.out.println("sending ack" + num);
toclient.flush();
toclient.writeBytes(num + "\n");
toclient.flush();
}
System.out.println(list);
for (int r = ack; r < nf; r++) {
String n = fromclient.readLine();
int num = Integer.parseInt(n);
String packet = fromclient.readLine();
System.out.println("received packet" + num + " = " + packet);
list.add(num, packet);
}
}else{
int k=0;
for (int i = 0; i < nf; i++) {
String n = fromclient.readLine();
int num = Integer.parseInt(n);
String packet = fromclient.readLine();
System.out.println("received packet" + num + " = " + packet);
list.add(num, packet);
k=i;
}
if(k==(nf-1)){
for(int j=0;j
System.out.println("sending ack" + num);
toclient.flush();
toclient.writeBytes(num + "\n");
toclient.flush();
}
}
}
fromclient.close();
output.close();
con.close();
s.close();
} catch (Exception e) {
System.out.println(" " );
}
}
}
/************ Go Back Client *************/
import java.io.*;
import java.net.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList;
public class GoBackNclient {
public static void main(String args[]) {
long start = System.currentTimeMillis();
try {
Socket con = new Socket("localhost", 7079);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream toserver = new DataOutputStream(con.getOutputStream());
BufferedReader fromserver = new BufferedReader(new InputStreamReader(con.getInputStream()));
LinkedList < String > list = new LinkedList < String > ();
System.out.println("Enter number of packets to be transmitted");
String n = in .readLine();
int nf = Integer.parseInt(n);
toserver.writeBytes(nf + "\n");
for (int i = 0; i < nf; i++) {
System.out.println("Enter packet" + i + "to be transmitted");
String packet = in .readLine();
list.add(packet);
}
System.out.println("\nYou have entered following data "+list+"\n");
System.out.println("Please enter Y for successful sending or N for packet loss");
String flow = in.readLine();
System.out.println("input read is "+flow);
toserver.writeBytes(flow+"\n");
int discard = 0;
int ack = 0,w=4,wind=0,p=0,q=0;
if(flow.contains("n") || flow.contains("N")){
for (int i = 0; i < nf; i++) {
p=i;
wind = windowSize(p,q);
if (i == 2) {
discard = 2;
System.out.println("Packet lost is"+i);
q=w;
wind = windowSize(p,q);
wind++;
q=wind;
continue;
} else {
String str = list.get(i);
if (discard == 2) str = "Discarded";
toserver.writeBytes(i + "\n");
toserver.writeBytes(str + "\n");
}
wind++;
q=wind;
String ak = fromserver.readLine();
ack = Integer.parseInt(ak);
if(ack==2)
System.out.println("Ack received for "+(ack-1));
else
System.out.println("Ack received for " + ack);
}
wind=0;q=4;
for (int r = ack; r < nf; r++) {
p=r;
wind = windowSize(p,q);
q=0;
System.out.println("Resending packet" + r);
String str = list.get(r);
toserver.writeBytes(r + "\n");
toserver.writeBytes(str + "\n");
wind++;
q=wind;
}
}else
{ q=0;
for (int i = 0; i < nf; i++) {
p=i;
wind = windowSize(p,q);
System.out.println("Sending packet" + i);
String str = list.get(i);
toserver.writeBytes(i + "\n");
toserver.writeBytes(str + "\n");
wind++;
q=wind;
if(i==(nf-1)){
for(int j=0;j
ack = Integer.parseInt(ak);
ack=j;
System.out.println("Ack received for " + ack);
}
}
}
}
fromserver.close();
toserver.close();
con.close();
} catch (Exception e) {
System.out.println("\n\n ERROR" + e);
}
long end = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.println("\nExecution time is " + formatter.format((end - start) / 1000d) + " seconds");
}
public static int windowSize(int p,int wind)throws InterruptedException
{
int i=p,w=i+4,windw=wind;
LinkedList < Integer > list1 = new LinkedList < Integer > ();
if(i==0 || windw==4){
System.out.println("\nBased on the window size, packets to be sent ");
for(int q=i;q
list1.add(q);
}
System.out.println(list1);
if(windw==0){
Thread.currentThread();
Thread.sleep(5000);
}
}
return windw;
}
}
0 comments :
Post a Comment