0% found this document useful (0 votes)
30 views4 pages

Goback

This document contains code for a client-server program that transmits frames of data from the server to the client. The server code creates a socket, listens for a connection from the client, and then either resends any frames that are not acknowledged by the client or simply sends each frame once depending on the transmission type selected by the client. The client code connects to the server, requests a specified number of frames, selects the transmission type, receives frames from the server and sends acknowledgments back after each valid frame is received.

Uploaded by

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

Goback

This document contains code for a client-server program that transmits frames of data from the server to the client. The server code creates a socket, listens for a connection from the client, and then either resends any frames that are not acknowledged by the client or simply sends each frame once depending on the transmission type selected by the client. The client code connects to the server, requests a specified number of frames, selects the transmission type, receives frames from the server and sends acknowledgments back after each valid frame is received.

Uploaded by

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

Server

import java.io.*;
import java.net.*;
import java.util.*;
class gbserver
{
public static void main(String args[]) throws Exception
{
System.out.println("Server");
System.out.println("Waiting for the connection");
InetAddress addr=InetAddress.getByName("localhost");
ServerSocket s=new ServerSocket(5000);
Socket s1=new Socket();
s1=s.accept();
BufferedInputStream in=new BufferedInputStream(s1.getInputStream());
DataOutputStream out=new DataOutputStream(s1.getOutputStream());
System.out.println("Recieved request for sending frame");
int p=in.read();
boolean f[]=new boolean[p];
int pc=in.read();
System.out.println("Sending...");
if(pc==0)
{
for(int i=0;i<p;i++)
{
System.out.println("Sending frame number"+i);
out.write(i);
out.flush();
System.out.println("Waiting for Acknowlegement");
try
{
Thread.sleep(1000);
}

catch(Exception e){}
int a=in.read();
System.out.println("Received aknowledgement for frame "+i +"as
"+a);
}
out.flush();
}
else
{
for(int i=0;i<p;i++)
{
if(i==2)
{
System.out.println("SEnding frame no"+i);
}
else
{
System.out.println("Sending frame no"+i);
out.write(i);
out.flush();
System.out.println("Waiting for Acknowlgement");
try
{
Thread.sleep(1000);
}
catch(Exception e){}
int a=in.read();
if(a!=255)
{
System.out.println("Recived Acknowlgement for frame
"+i+" as "+a);
f[i]=true;
}
}

}
for(int a=0;a<p;++a)
{
if(f[a]==false)
{
System.out.println("Resending the frame "+a);
out.write(a);
out.flush();
System.out.println("Waiting for ack");
try
{
Thread.sleep(2000);
}
catch(Exception e){}
int b=in.read();
System.out.println("Recieved ack for frame no"+ a +"as"+b);
f[a]=true;
}
}
out.flush();
}
in.close();
out.close();
}
}
Client
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;

class gbclient
{
public static void main(String[] args) throws Exception
{
InetAddress addr=InetAddress.getByName("localhost");
System.out.println(addr);
Socket s=new Socket(addr,5000);
BufferedInputStream in=new BufferedInputStream(s.getInputStream());
DataOutputStream out =new DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
System.out.println("Client.. ");
System.out.println("Connect");
System.out.println("Enter the number of frame to requested to the server");
int c=sc.nextInt();
out.write(c);
out.flush();
System.out.println("Enter type of transmission.Error-1 : No-Error-0");
int choice=sc.nextInt();
out.write(choice);
int check=0;
int i=0;
int j=0;
if(choice==0)
{
for(j=0;j<c;++j)
{
i=in.read();
System.out.println("Recieved Frame no is:"+i);
System.out.println("Sending Acknowledgment for frame no :"+i);
out.write(i);
out.flush();
}
}
else
{
for(j=0;j<c;++j)
{
i=in.read();
if(i==check)
{
System.out.println("i:"+i +"check: "+check);
System.out.println("Received frame no:"+i);
System.out.println("Sending ack for frame no"+i);
out.write(i);
++check;
}
else
{
--j;
System.out.println("discarded frame no:"+i);
System.out.println("sending neg ack");
out.write(-1);
}
out.flush();
}
}
in.close();
out.close();
System.out.println("Quiting");
}
}

You might also like