Ex2a Ex2b
import java.net.InetAddress; import java.io.IOException;
public class Ex2a { import java.net.*;
public static void public class Ex2bnew
main(String[]args)throws
{
Exception
public static void main(String[] args) {
{
try { InetAddress
InetAddress addr=InetAddress.getLocalHost();
addr=InetAddress.getLocalHost();
Socket thesoc=new Socket(addr,139);
System.out.println("Host
System.out.println("Connected to
Name:"+addr.getHostName());
"+thesoc.getInetAddress()+"on the port
System.out.println("IP "+thesoc.getPort()+"from port
Address:"+addr.getHostAddress()); "+thesoc.getLocalPort()+"of
"+thesoc.getLocalAddress()); }
}
catch(UnknownHostException e) {
}
System.err.println("Can't find "+e); }
catch(SocketException e) {
OUTPUT
System.err.println("Could not connect
Host Name:MSC01
"+e); }
IP Address:172.10.8.25
catch(IOException e) {
System.out.println(e); } } }
OUTPUT
Connected to MSC01/172.10.8.25 on the
port 139 from port 51101 of /172.10.8.25
Ex3 a[len++]=0; }
import java.util.Scanner; k=len-16;
public class Ex3 { for(int i=0;i<len;i++) {
void div (int a[],int k) { b[i]=a[i]; }
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1}; ob.div(a, k);
int count=0; for(int i=0;i<len;i++) {
for(int i=0;i<k;i++) { a[i]=a[i]^b[i];}
if(a[i]==gp[0]) { System.out.println("Data to be
transmitted");
for(int j=i;j<17;j++) {
for(int i=0;i<len;i++) {
a[j]=a[j]^gp[count++]; }
System.out.println(a[i]+""); }
count=0; } } }
System.out.println();
public static void main(String[] args) {
System.out.println("Enter received data");
int a[]=new int[100];
for(int i=0;i<len;i++) {
int b[]=new int[100];
a[i]=sc.nextInt(); }
int len,k;
ob.div(a, k);
Ex3 ob=new Ex3();
for(int i=0;i<len;i++) {
System.out.println("Enter the length of
the data frame"); if(a[i]!=0) {
Scanner sc=new Scanner(System.in); flag=1;
len=sc.nextInt(); break; } }
int flag=0; if(flag==1) {
System.out.println("Enter the message"); System.out.println("Error in data"); }
for(int i=0;i<len;i++) { else {
a[i]=sc.nextInt(); } System.out.println("No error"); } } }
for(int i=0;i<16;i++) {
OUTPUT
Enter the length of the data frame
2
Enter the message
1
0
Data to be transmitted
100010000001000010
Enter received data
100010000001000010
No error
OUTPUT
Enter the length of the data frame
2
Enter the message
1
0
Data to be transmitted
100010000001000010
Enter received data
100010000001000010
No error
Sliding window
Sender:
import java.net.*; sws=-nf;
import java.io.*; System.out.println("Acknowledgement
received");
public class slidsend {
ano=Integer.parseInt(in1.readLine());
public static void main(String args[])
throws Exception { System.out.println("for "+ano+" frames");
ServerSocket ser=new ServerSocket(10); sws+=nf; }
Socket s=ser.accept(); else {
DataInputStream in=new System.out.println("The number of frames
DataInputStream(System.in); exceeds window size");
DataInputStream in1=new break; }
DataInputStream(s.getInputStream());
System.out.println("\nDo you want to
String sbuff[]=new String[8]; send some more frames?\n");
PrintStream p; ch=in.readLine();
int sptr=0,sws=8,nf,ano,i; p.println(ch); }
String ch; while(ch.equals("yes"));
do { s.close(); } }
p=new PrintStream(s.getOutputStream());
System.out.println("Enter the number of
frames: ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1) {
System.out.println("Enter the "+nf+"
messages to be send\n");
for(i=1;i<=nf;i++){
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8; }
import java.net.*; OUTPUT
import java.io.*; //SENDER
class slidreceive { Enter the number of frames:
public static void main(String a[])throws 2
Exception {
Enter the 2 messages to be send
Socket s=new
HI
Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new HELLO
DataInputStream(s.getInputStream()); Acknowledgement received
PrintStream p=new for 2 frames
PrintStream(s.getOutputStream());
Do you want to send some more frames?
int i=0,rptr=-1,nf,rws=8; String rbuf[]=new
String[8]; String ch; NO
System.out.println(); do {
nf=Integer.parseInt(in.readLine()); //RECEIVER
if(nf<=rws-1) { The received Frame 0 is : HI
for(i=1;i<=nf;i++) { The received Frame 1 is : HELLO
rptr=++rptr%8; Acknowledgment sent
rbuf[rptr]=in.readLine();
System.out.println("The received Frame "
+rptr+" is : "+rbuf[rptr]); }
rws-=nf;
System.out.println("\nAcknowledgment
sent\n"); p.println(rptr+1); rws+=nf; }
else
break;
ch=in.readLine(); }
while(ch.equals("yes")); } }
STOP AND WAIT : seq = 1 – seq;
Sender: i++; }
import java.io.*; else {
import java.net.*; System.out.println("ACK mismatch!
Resending frame...");} }
import java.util.Scanner;
out.writeUTF("exit");
public class SwSender {
sc.close();
public static void main(String[] args)
throws Exception { socket.close();} }
Socket socket = new Socket("localhost",
5000);
DataOutputStream out = new
DataOutputStream(socket.getOutputStrea
m());
DataInputStream
in=newDataInputStream(socket.getInputS
tream());
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of frames
to send: ");
int n = sc.nextInt();
sc.nextLine();
int seq = 0;
for (int i = 0; i < n; ) {
System.out.print("Enter data for frame " +
(i + 1) + ": ");
String data = sc.nextLine();
out.writeUTF(seq + ":" + data);
System.out.println("Sent frame with Seq="
+ seq + ". Waiting for ACK...");
String ack = in.readUTF();
System.out.println("Received: " + ack);
if (ack.equals("ACK" + seq)) {
Receiver: out.writeUTF("ACK"+(1-expectedSeq));}}
import java.io.*; socket.close();
import java.net.*; server.close(); } }
public class SWReceiver {
public static void main(String[] args)
throws Exception {
OUTPUT :
ServerSocket server = new
//sender
ServerSocket(5000);
C:\>java Sender.java
System.out.println("Receiver ready...");
Enter number of frames to send: 2
Socket socket = server.accept();
Enter data for frame 1: John
DataInputStream in = new
DataInputStream(socket.getInputStream() Sent frame with Seq=0. Waiting for ACK...
); Received: ACK0
DataOutputStream out = new Enter data for frame 2: PSGCAS
DataOutputStream(socket.getOutputStrea
m()); Sent frame with Seq=1. Waiting for ACK...
int expectedSeq = 0; Received: ACK1
while (true) {
String frame = in.readUTF(); //receiver
if (frame.equalsIgnoreCase("exit")) break; C:\>java Receiver.java
String[] parts = frame.split(":", 2); Receiver ready...
int seq = Integer.parseInt(parts[0]); Received frame with Seq=0, Data: John
String data = parts[1]; Received frame with Seq=1, Data: PSGCAS
if (seq == expectedSeq) {
System.out.println("Received frame with
Seq=" + seq + ", Data: " + data);
out.writeUTF("ACK" + seq);
expectedSeq = 1 - expectedSeq; }
else {
System.out.println("Duplicate frame
received with Seq=" + seq + ".
Discarding.");