10th
import java.util.*;
class crc{
 static String div(String d,String g){
  int m=g.length();char[] r=d.toCharArray();
  for(int i=0;i<=d.length()-m;i++)
   if(r[i]=='1')
    for(int j=0;j<m;j++)
     r[i+j]=r[i+j]==g.charAt(j)?'0':'1';
  return new String(r).substring(d.length()-(m-1));
 }
 public static void main(String[]a){
  Scanner sc=new Scanner(System.in);
  System.out.print("Enter data: ");String data=sc.next();
  System.out.print("Enter generator: ");String gen=sc.next();
  String zeros = new String(new char[gen.length()-1]).replace('\0','0');
  String rem=div(data+zeros,gen);
  System.out.println("Codeword: "+data+rem);
  System.out.println(div(data+rem,gen).contains("1")?"Error!":"No Error");
 }}
Arp server
import java.io.*;
import java.net.*;
public class arps {
  public static void main(String[] args) throws Exception {
     ServerSocket ss = new ServerSocket(5000);
     String[] ip = {"165.165.80.80", "165.165.79.1"};
     String[] mac = {"6A:08:AA:C2", "8A:BC:E3:FA"};
     System.out.println("ARP Server running...");
       while (true) {
         Socket s = ss.accept();
       BufferedReader in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
       BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()));
       String req = in.readLine(), res = "MAC not found";
            for (int i = 0; i < ip.length; i++)
               if (req.equals(ip[i])) res = mac[i];
            out.write(res + "\n");
            out.flush();
            System.out.println("Request: " + req + " → " + res);
            s.close();
        }
    }
}
Arp client
import java.io.*;
import java.net.*;
public class arpc {
  public static void main(String[] args) throws Exception {
     Socket s = new Socket("127.0.0.1", 5000);
     BufferedReader in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
     BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()));
     BufferedReader user = new BufferedReader(new
InputStreamReader(System.in));
        System.out.print("Enter IP Address: ");
        String ip = user.readLine();
        out.write(ip + "\n");
        out.flush();
        System.out.println("MAC Address: " + in.readLine());
        s.close();
    }
}
Rarp s
import java.net.*;
public class rarps {
  public static void main(String[] args) throws Exception {
     DatagramSocket server = new DatagramSocket(1309);
     String[] mac = {"6A:08:AA:C2", "8A:BC:E3:FA"};
     String[] ip = {"165.165.80.80", "165.165.79.1"};
     System.out.println("RARP Server running...");
        byte[] buf = new byte[100];
        while (true) {
          DatagramPacket p = new DatagramPacket(buf, buf.length);
          server.receive(p);
          String macAddr = new String(p.getData()).trim();
          String res = "IP not found";
            for (int i = 0; i < mac.length; i++)
               if (macAddr.equals(mac[i])) res = ip[i];
            server.send(new DatagramPacket(res.getBytes(), res.length(),
                 p.getAddress(), p.getPort()));
            System.out.println("Received MAC: " + macAddr + " → " + res);
        }
    }
}
Rarp c
import java.io.*;
import java.net.*;
public class rarpc {
  public static void main(String[] args) throws Exception {
     DatagramSocket client = new DatagramSocket();
     InetAddress addr = InetAddress.getLocalHost();
     BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
        System.out.print("Enter MAC Address: ");
        String mac = br.readLine();
        byte[] sendData = mac.getBytes();
        client.send(new DatagramPacket(sendData, sendData.length, addr, 1309));
        byte[] recv = new byte[100];
        client.receive(new DatagramPacket(recv, recv.length));
        System.out.println("IP Address: " + new String(recv).trim());
        client.close();
    }
}
set ns [new Simulator]
# Trace & NAM
set nf [open exp9a.tr w]
$ns trace-all $nf
set namf [open exp9a.nam w]
$ns namtrace-all $namf
# Nodes
set n0 [$ns node]; set n1 [$ns node]
set n2 [$ns node]; set n3 [$ns node]
# Links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 50ms DropTail
# NAM orientations
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient down
$ns duplex-link-op $n2 $n3 orient left
$ns duplex-link-op $n0 $n3 orient down
# Routing & TCP setup
$ns rtproto DV
set tcp [new Agent/TCP]; $ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]; $ns attach-agent $n3 $sink
$ns connect $tcp $sink
# FTP traffic
set ftp [new Application/FTP]; $ftp attach-agent $tcp
$ns at 1.0 "$ftp start"
$ns at 9.0 "$ftp stop"
# Finish
proc finish {} {
  global ns nf namf
  $ns flush-trace
    close $nf; close $namf
    exec nam exp9a.nam &
    exit 0
}
$ns at 10.0 "finish"
$ns run
1. Download Webpage/Image via TCP Socket
Client.java
import javax.imageio.*; import java.awt.image.*; import java.io.*; import
java.net.*;
public class Client {
  public static void main(String[] args) throws Exception {
     Socket soc = new Socket("localhost", 4000);
     BufferedImage img = ImageIO.read(new File("a.jpeg"));
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ImageIO.write(img, "jpg", baos);
     byte[] bytes = baos.toByteArray();
     DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
     dos.writeInt(bytes.length);
     dos.write(bytes);
     dos.close(); soc.close();
     System.out.println("Image sent to server.");
  }
}
Server.java
import javax.imageio.*; import javax.swing.*; import java.awt.image.*; import
java.io.*; import java.net.*;
public class Server {
  public static void main(String[] args) throws Exception {
     ServerSocket server = new ServerSocket(4000);
     Socket socket = server.accept();
     DataInputStream dis = new DataInputStream(socket.getInputStream());
     int len = dis.readInt();
     byte[] data = new byte[len];
     dis.readFully(data);
     BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
     JFrame f = new JFrame("Server");
     f.add(new JLabel(new ImageIcon(img)));
     f.pack(); f.setVisible(true);
     dis.close(); socket.close(); server.close();
  }
}
2. TCP Echo Client & Server
EClient.java
import java.io.*; import java.net.*;
public class EClient {
  public static void main(String[] args) throws Exception {
     Socket c = new Socket(InetAddress.getLocalHost(), 9000);
     BufferedReader user = new BufferedReader(new
InputStreamReader(System.in));
     BufferedReader in = new BufferedReader(new
InputStreamReader(c.getInputStream()));
     PrintStream out = new PrintStream(c.getOutputStream());
     String line;
     while(true) {
        System.out.print("Client: "); line = user.readLine();
        out.println(line);
          System.out.println("Server: " + in.readLine());
          if(line.equalsIgnoreCase("exit")) break;
        }
        c.close();
    }
}
EServer.java
import java.io.*; import java.net.*;
public class EServer {
  public static void main(String[] args) throws Exception {
     ServerSocket s = new ServerSocket(9000);
     Socket c = s.accept();
     BufferedReader in = new BufferedReader(new
InputStreamReader(c.getInputStream()));
     PrintStream out = new PrintStream(c.getOutputStream());
     String line;
     while((line = in.readLine()) != null) out.println(line);
     c.close(); s.close();
  }
}
3. Chat via UDP Sockets
UDPClient.java
import java.net.*; import java.io.*;
public class UDPClient {
  public static void main(String[] args) throws Exception {
     DatagramSocket ds = new DatagramSocket();
     InetAddress ia = InetAddress.getLocalHost();
     BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
     byte[] buffer = new byte[1024];
     while(true) {
       System.out.print("Client: "); String msg = br.readLine();
       if(msg.equals("end")) break;
       ds.send(new DatagramPacket(msg.getBytes(), msg.length(), ia, 789));
       DatagramPacket p = new DatagramPacket(buffer, buffer.length);
       ds.receive(p);
       System.out.println("Server: " + new
String(p.getData(),0,p.getLength()));
     }
     ds.close();
   }
}
UDPServer.java
import java.net.*; import java.io.*;
public class UDPServer {
  public static void main(String[] args) throws Exception {
     DatagramSocket ds = new DatagramSocket(789);
     byte[] buffer = new byte[1024];
     BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
     InetAddress ia = InetAddress.getLocalHost();
     while(true) {
        DatagramPacket p = new DatagramPacket(buffer, buffer.length);
        ds.receive(p);
        System.out.println("Client: " + new String(p.getData(),0,p.getLength()));
        System.out.print("Server: "); String msg = br.readLine();
        if(msg.equals("end")) break;
        ds.send(new DatagramPacket(msg.getBytes(), msg.length(), ia, 790));
     }
     ds.close();
  }
}
4. File Transfer via TCP Sockets
FileClient.java
import java.io.*; import java.net.*;
public class FileClient {
  public static void main(String[] args) throws Exception {
     Socket socket = new Socket("localhost", 4002);
     InputStream is = socket.getInputStream();
     FileOutputStream fos = new FileOutputStream("received_text.txt");
     byte[] contents = new byte[10000];
     int bytesRead;
     while((bytesRead = is.read(contents)) != -1)
fos.write(contents,0,bytesRead);
     fos.close(); socket.close();
     System.out.println("File saved successfully!");
  }
}
FileServer.java
import java.io.*; import java.net.*;
public class FileServer {
   public static void main(String[] args) throws Exception {
     ServerSocket ss = new ServerSocket(4002);
     Socket socket = ss.accept();
     File file = new File("text.txt");
     BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(file));
     OutputStream os = socket.getOutputStream();
     byte[] contents = new byte[10000];
     int bytesRead;
     while((bytesRead = bis.read(contents)) != -1)
os.write(contents,0,bytesRead);
     os.flush(); bis.close(); socket.close(); ss.close();
     System.out.println("File sent successfully!");
   }
}
5. DNS via UDP Sockets
UDPDNSClient.java
import java.io.*; import java.net.*;
public class UDPDNSClient {
   public static void main(String[] args) throws IOException {
     DatagramSocket ds = new DatagramSocket();
     BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
     InetAddress server = InetAddress.getLocalHost();
     byte[] sendData = new byte[1024], receiveData = new byte[1024];
     System.out.print("Enter hostname: "); String host = br.readLine();
     ds.send(new DatagramPacket(host.getBytes(), host.length(), server, 1362));
     DatagramPacket p = new DatagramPacket(receiveData,
receiveData.length);
     ds.receive(p);
     System.out.println("IP Address: " + new
String(p.getData(),0,p.getLength()));
     ds.close();
   }
}
UDPDNSServer.java
import java.io.*; import java.net.*;
public class UDPDNSServer {
  public static void main(String[] args) throws IOException {
     String[] hosts =
{"yahoo.com","gmail.com","cricinfo.com","facebook.com"};
     String[] ips =
{"68.180.206.184","209.85.148.19","80.168.92.140","69.63.189.16"};
     DatagramSocket ds = new DatagramSocket(1362);
     byte[] buffer = new byte[1024];
     System.out.println("UDP DNS Server started");
     while(true){
       DatagramPacket p = new DatagramPacket(buffer, buffer.length);
       ds.receive(p);
       String host = new String(p.getData(),0,p.getLength()).trim();
       InetAddress clientAddr = p.getAddress(); int clientPort = p.getPort();
       String resp = "Host Not Found";
       for(int i=0;i<hosts.length;i++) if(hosts[i].equalsIgnoreCase(host))
resp=ips[i];
       ds.send(new DatagramPacket(resp.getBytes(), resp.length(), clientAddr,
clientPort));
     }
   }
}
# exp9a.tcl - Distance Vector Routing Simulation
set ns [new Simulator]
# Trace + NAM
set nf [open exp9a.tr w]
$ns trace-all $nf
set namf [open exp9a.nam w]
$ns namtrace-all $namf
# Nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 50ms DropTail
# Orient for NAM
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient down
$ns duplex-link-op $n2 $n3 orient left
$ns duplex-link-op $n0 $n3 orient down
# Routing protocol
$ns rtproto DV
# TCP Agent and Sink (FIXED ORDER)
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
# Must connect *after* both agents are attached
$ns connect $tcp $sink
# FTP over TCP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
# Start traffic
$ns at 1.0 "$ftp start"
$ns at 9.0 "$ftp stop"
# Finish
proc finish {} {
    global ns nf namf
    $ns flush-trace
    close $nf
    close $namf
    exec nam exp9a.nam &
    exit 0
}
$ns at 10.0 "finish"
$ns run
# Step-1: Initialize simulator
set ns [new Simulator]
$ns rtproto LS
# Step-2: Create 7 nodes
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
set node5 [$ns node]
set node6 [$ns node]
set node7 [$ns node]
# Step-3: Trace and NAM files
set tf [open out.tr w]
$ns trace-all $tf
set nf [open out.nam w]
$ns namtrace-all $nf
$node1 label-color blue
$node2 label-color red
$node3 label-color red
$node4 label-color blue
$node5 label-color blue
$node6 label-color blue
$node7 label-color blue
# Step-5: Duplex links (forming ring)
$ns duplex-link $node1 $node2 1.5Mb 10ms DropTail
$ns duplex-link $node2 $node3 1.5Mb 10ms DropTail
$ns duplex-link $node3 $node4 1.5Mb 10ms DropTail
$ns duplex-link $node4 $node5 1.5Mb 10ms DropTail
$ns duplex-link $node5 $node6 1.5Mb 10ms DropTail
$ns duplex-link $node6 $node7 1.5Mb 10ms DropTail
$ns duplex-link $node7 $node1 1.5Mb 10ms DropTail
# Step-6: Orient links
$ns duplex-link-op $node1 $node2 orient left-down
$ns duplex-link-op $node2 $node3 orient left-down
$ns duplex-link-op $node3 $node4 orient right-down
$ns duplex-link-op $node4 $node5 orient right
$ns duplex-link-op $node5 $node6 orient right-up
$ns duplex-link-op $node6 $node7 orient left-up
$ns duplex-link-op $node7 $node1 orient left-up
# Step-7: TCP agent setup
set tcp2 [new Agent/TCP]
$ns attach-agent $node1 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $node4 $sink2
$ns connect $tcp2 $sink2
# Step-8: FTP traffic
set traffic_ftp2 [new Application/FTP]
$traffic_ftp2 attach-agent $tcp2
# Step-9: Finish procedure
proc finish {} {
  global ns nf
    $ns flush-trace
    close $nf
    exec nam out.nam &
    exit 0
}
# Step-10: Scheduling (fixed with $traffic_ftp2)
$ns at 0.5 "$traffic_ftp2 start"
$ns rtmodel-at 1.0 down $node2 $node3
$ns rtmodel-at 2.0 up $node2 $node3
$ns at 3.0 "$traffic_ftp2 start"
$ns at 4.0 "$traffic_ftp2 stop"
$ns at 5.0 "finish"
# Run the simulation
$ns run