Advertisement
bittertea

....

Dec 8th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.35 KB | Source Code | 0 0
  1. ## ----------- Simulation Program 1 (Simple point-to-point)
  2.  
  3. set ns [new Simulator] ;# Letter S is capital
  4. set nf [open 1.nam w] ;# open a nam trace file in write mode
  5. $ns namtrace-all $nf ;# nf nam filename
  6. set tf [open 1.tr w] ;# tf trace filename
  7. $ns trace-all $tf
  8. proc finish { } {
  9. global ns nf tf
  10. $ns flush-trace ;# clears trace file contents
  11. close $nf
  12. close $tf
  13. exec nam 1.nam &
  14. exit 0
  15. }
  16. set n0 [$ns node] ;# creates 3 nodes
  17. set n2 [$ns node]
  18. set n3 [$ns node]
  19.  
  20. $ns duplex-link $n0 $n2 200Mb 10ms DropTail ;# establishing links
  21. $ns duplex-link $n2 $n3 1Mb 1000ms DropTail
  22.  
  23. $ns queue-limit $n0 $n2 10
  24.  
  25.  
  26. set udp0 [new Agent/UDP] ;# attaching transport layer protocols
  27. $ns attach-agent $n0 $udp0
  28.  
  29. set cbr0 [new Application/Traffic/CBR] ;# attaching application layer
  30. $cbr0 set packetSize_ 500
  31. $cbr0 set interval_ 0.005
  32. $cbr0 attach-agent $udp0
  33.  
  34. set null0 [new Agent/Null] ;# creating sink (destination) node
  35.  
  36. $ns attach-agent $n3 $null0
  37. $ns connect $udp0 $null0
  38. $ns at 0.1 "$cbr0 start"
  39. $ns at 1.0 "finish"
  40. $ns run
  41.  
  42.  
  43.  
  44.  
  45.  
  46. ########## --- AWK FILE CODE (file1.awk)
  47.  
  48. BEGIN{ c=0;}
  49. {
  50. if($1=="d")
  51. { c++;
  52. printf("%s\t%s\n",$5,$11);
  53. }
  54. }
  55. END{ printf("The number of packets dropped is %d\n",c); }
  56.  
  57. #### to run in terminal -- awk -f file1.awk 2.tr
  58.  
  59.  
  60. ## --------------- Simulation Program 2 (Transmission of ping messages/traceroute)
  61.    
  62. # Create a new simulator instance
  63. set ns [new Simulator]
  64. set p2 [new Agent/Ping]
  65.  
  66.  
  67. # Open output files for trace and NAM visualization
  68. set nf [open 2.nam w]
  69. $ns namtrace-all $nf
  70. set tf [open 2.tr w]
  71. $ns trace-all $tf
  72.  
  73. # Create network nodes
  74. set n0 [$ns node]
  75. set n1 [$ns node]
  76. set n2 [$ns node]
  77. set n3 [$ns node]
  78. set n4 [$ns node]
  79. set n5 [$ns node]
  80.  
  81. # Establish links between nodes
  82. $ns duplex-link $n0 $n4 1005Mb 1ms DropTail
  83. $ns duplex-link $n1 $n4 50Mb 1ms DropTail
  84. $ns duplex-link $n2 $n4 2000Mb 1ms DropTail
  85. $ns duplex-link $n3 $n4 200Mb 1ms DropTail
  86. $ns duplex-link $n4 $n5 1Mb 1ms DropTail
  87.  
  88. # Create and attach Ping agents
  89. set p1 [new Agent/Ping]
  90. $ns attach-agent $n0 $p1
  91. $p1 set packetSize_ 50000
  92. $p1 set interval_ 0.0001
  93.  
  94. set p2 [new Agent/Ping]
  95.  
  96. $ns attach-agent $n1 $p2
  97.  
  98. set p3 [new Agent/Ping]
  99. $ns attach-agent $n2 $p3
  100. $p3 set packetSize_ 30000
  101. $p3 set interval_ 0.00001
  102.  
  103. set p4 [new Agent/Ping]
  104. $ns attach-agent $n3 $p4
  105.  
  106. set p5 [new Agent/Ping]
  107. $ns attach-agent $n5 $p5
  108.  
  109. # Set queue limits on specific links
  110. $ns queue-limit $n0 $n4 5
  111. $ns queue-limit $n2 $n4 3
  112. $ns queue-limit $n4 $n5 2
  113.  
  114. # Define the 'recv' method for the Ping agent
  115. Agent/Ping instproc recv {from rtt} {
  116.     $self instvar node_
  117.     puts "node [$node_ id] received answer from $from with round trip time $rtt msec"
  118. }
  119.  
  120. # Connect the agents for communication
  121. $ns connect $p1 $p5
  122. $ns connect $p3 $p4
  123.  
  124. # Define a finish procedure to close trace files and launch NAM
  125. proc finish { } {
  126.     global ns nf tf
  127.     $ns flush-trace
  128.     close $nf
  129.     close $tf
  130.     exec nam 2.nam &
  131.     exit 0
  132. }
  133.  
  134. # Schedule packet sending events
  135. for {set i 1} {$i <= 10} {incr i} {
  136.     set time [expr $i * 0.1]
  137.     $ns at $time "$p1 send"
  138.     $ns at $time "$p3 send"
  139. }
  140.  
  141. # Schedule the end of the simulation
  142. $ns at 2.0 "finish"
  143.  
  144. # Run the simulation
  145. $ns run
  146.  
  147.  
  148.  
  149.  
  150. ##--------- Simulation Program 3 (Ethernet LAN using n nodes)
  151.    
  152. set ns [new Simulator]
  153.  
  154. # Open trace and NAM trace files
  155. set tf [open 3.tr w]
  156. $ns trace-all $tf
  157.  
  158. set nf [open 3.nam w]
  159. $ns namtrace-all $nf
  160.  
  161. # Create nodes and set their properties
  162. set n0 [$ns node]
  163. $n0 color "magenta"
  164. $n0 label "src1"
  165.  
  166. set n1 [$ns node]
  167.  
  168. set n2 [$ns node]
  169. $n2 color "magenta"
  170. $n2 label "src2"
  171.  
  172. set n3 [$ns node]
  173. $n3 color "blue"
  174. $n3 label "dest2"
  175.  
  176. set n4 [$ns node]
  177.  
  178. set n5 [$ns node]
  179. $n5 color "blue"
  180. $n5 label "dest1"
  181.  
  182. # Create LAN and duplex links
  183. $ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
  184. $ns duplex-link $n4 $n5 1Mb 1ms DropTail
  185.  
  186. # Create TCP agents and FTP applications
  187. set tcp0 [new Agent/TCP]
  188. $ns attach-agent $n0 $tcp0
  189.  
  190. set sink5 [new Agent/TCPSink]
  191. $ns attach-agent $n5 $sink5
  192. $ns connect $tcp0 $sink5
  193.  
  194. set ftp0 [new Application/FTP]
  195. $ftp0 attach-agent $tcp0
  196. $ftp0 set packetSize_ 500
  197. $ftp0 set interval_ 0.0001
  198.  
  199. set tcp2 [new Agent/TCP]
  200. $ns attach-agent $n2 $tcp2
  201.  
  202. set sink3 [new Agent/TCPSink]
  203. $ns attach-agent $n3 $sink3
  204. $ns connect $tcp2 $sink3
  205.  
  206. set ftp2 [new Application/FTP]
  207. $ftp2 attach-agent $tcp2
  208. $ftp2 set packetSize_ 600
  209. $ftp2 set interval_ 0.001
  210.  
  211. # Create output files for tracing TCP windows
  212. set file1 [open 3_file1.tr w]
  213. $tcp0 attach $file1
  214.  
  215. set file2 [open 3_file2.tr w]
  216. $tcp2 attach $file2
  217.  
  218. # Enable congestion window tracing
  219. $tcp0 trace cwnd_
  220. $tcp2 trace cwnd_
  221.  
  222. # Define the finish procedure
  223. proc finish { } {
  224.     global ns nf tf
  225.     $ns flush-trace
  226.     close $tf
  227.     close $nf
  228.     exec nam 3.nam &
  229.     exit 0
  230. }
  231.  
  232. # Schedule events
  233. $ns at 0.1 "$ftp0 start"
  234. $ns at 5 "$ftp0 stop"
  235. $ns at 7 "$ftp0 start"
  236. $ns at 0.2 "$ftp2 start"
  237. $ns at 8 "$ftp2 stop"
  238. $ns at 14 "$ftp0 stop"
  239. $ns at 10 "$ftp2 start"
  240. $ns at 15 "$ftp2 stop"
  241. $ns at 16 "finish"
  242.  
  243. # Run the simulation
  244. $ns run
  245.  
  246.  
  247. ##---------- Sliding Window
  248.  
  249. class SlidingWindow {
  250.     int windowSize;
  251.     int totalPackets;
  252.     int currentPacket;
  253.  
  254.     SlidingWindow(int windowSize, int totalPackets) {
  255.         this.windowSize = windowSize;
  256.         this.totalPackets = totalPackets;
  257.         this.currentPacket = 0; // Start from the first packet
  258.     }
  259.  
  260.     // Method to send packets
  261.     public void sendPackets() {
  262.         while (currentPacket < totalPackets) {
  263.             // Send packets within the window limit
  264.             for (int i = 0; i < windowSize && currentPacket < totalPackets; i++) {
  265.                 System.out.println("Sending packet " + currentPacket);
  266.                 currentPacket++;
  267.             }
  268.             // Simulate receiving an acknowledgment
  269.             receiveAck();
  270.         }
  271.     }
  272.  
  273.     // Method to simulate receiving acknowledgment for the window
  274.     private void receiveAck() {
  275.         System.out.println("Acknowledgment received for window up to packet " + (currentPacket - 1));
  276.     }
  277. }
  278.  
  279. public class SW {
  280.     public static void main(String[] args) {
  281.         int windowSize = 4; // Size of the sliding window
  282.         int totalPackets = 10; // Total number of packets to send
  283.  
  284.         SlidingWindow slidingWindow = new SlidingWindow(windowSize, totalPackets);
  285.         slidingWindow.sendPackets(); // Start the packet transmission
  286.     }
  287. }
  288.  
  289.  
  290.  
  291.  
  292. ##---------- Send file over TCP/IP
  293.  
  294. SERVER ->
  295.    
  296. import java.io.*;
  297. import java.net.*;
  298. public class server {
  299.    public static void main(String[] args) {
  300.        ServerSocket serverSocket = null;
  301.        Socket clientSocket = null;
  302.        try {
  303.            // Create a server socket listening on port 9876
  304.            serverSocket = new ServerSocket(9876);
  305.            System.out.println("Server started. Waiting for a client...");
  306.            // Accept client connection
  307.            clientSocket = serverSocket.accept();
  308.            System.out.println("Client connected!");
  309.            // Input and output streams for client communication
  310.            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  311.            PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
  312.            // Read the file name requested by the client
  313.            String fileName = inFromClient.readLine();
  314.            System.out.println("Client requested file: " + fileName);
  315.            // Attempt to open the requested file
  316.            File file = new File(fileName);
  317.            if (file.exists() && !file.isDirectory()) {
  318.                // If the file exists, send back the file contents
  319.                BufferedReader fileReader = new BufferedReader(new FileReader(file));
  320.                String line;
  321.                outToClient.println("FILE_FOUND");
  322.                while ((line = fileReader.readLine()) != null) {
  323.                    outToClient.println(line);
  324.                }
  325.                fileReader.close();
  326.                System.out.println("File sent successfully.");
  327.            } else {
  328.                // If the file doesn't exist, inform the client
  329.                outToClient.println("FILE_NOT_FOUND");
  330.                System.out.println("Requested file not found.");
  331.            }
  332.        } catch (IOException e) {
  333.            e.printStackTrace();
  334.        } finally {
  335.            // Close the connections
  336.            try {
  337.                if (clientSocket != null) clientSocket.close();
  338.                if (serverSocket != null) serverSocket.close();
  339.            } catch (IOException e) {
  340.                e.printStackTrace();
  341.            }
  342.        }
  343.    }
  344. }
  345.  
  346. CLIENT ->
  347.  
  348. import java.io.*;
  349. import java.net.*;
  350. public class Client {
  351.    public static void main(String[] args) {
  352.        Socket socket = null;
  353.        try {
  354.            socket = new Socket("localhost", 9876);
  355.            System.out.println("Connected to the server!");
  356.          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  357.            PrintWriter outToServer = new PrintWriter(socket.getOutputStream(), true);
  358.  BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
  359.                     System.out.print("Enter the name of the file to request: ");
  360.            String fileName = userInput.readLine();
  361.            // Send the file name to the server
  362.            outToServer.println(fileName);
  363.            // Read the response from the server
  364.            String serverResponse = inFromServer.readLine();
  365.            if ("FILE_FOUND".equals(serverResponse)) {
  366.                System.out.println("File found! Receiving content:");
  367.                String line;
  368.                while ((line = inFromServer.readLine()) != null) {
  369.                    System.out.println(line);
  370.                }
  371.            } else if ("FILE_NOT_FOUND".equals(serverResponse)) {
  372.                System.out.println("File not found on the server.");
  373.            }
  374.        } catch (IOException e) {
  375.            e.printStackTrace();
  376.        } finally {
  377.            // Close the connection
  378.            try {
  379.                if (socket != null) socket.close();
  380.            } catch (IOException e) {
  381.                e.printStackTrace();
  382.            }
  383.        }
  384.    }
  385. }
  386.  
  387.  
  388.  
  389. ##-------- Client/Server Message Send/Recieve UDP Datagram program
  390.  
  391. SERVER ->
  392.    
  393. import java.net.*;
  394. import java.io.*;
  395. public class UDP_Server {
  396.     public static void main(String[] args) throws IOException {
  397.         DatagramSocket serverSocket = new DatagramSocket(9876); // Server listens on port 9876
  398.         byte[] receiveBuffer = new byte[1024];
  399.         byte[] sendBuffer;
  400.  
  401.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  402.         System.out.println("Server started. Waiting for client messages...");
  403.  
  404.         while (true) {
  405.             // Receiving data from client
  406.       DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
  407.             serverSocket.receive(receivePacket); // Block until data is received
  408.             String clientMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
  409.  
  410.  
  411.             System.out.println("Client: " + clientMessage);
  412.             // Server typing and sending response
  413.             System.out.print("Server: ");
  414.             String serverMessage = reader.readLine();
  415.             sendBuffer = serverMessage.getBytes();
  416.             InetAddress clientAddress = receivePacket.getAddress();
  417.             int clientPort = receivePacket.getPort();
  418.  
  419.             DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, clientAddress, clientPort);
  420.             serverSocket.send(sendPacket); // Send the message to client
  421.  
  422.             // Exit on typing "exit"
  423.             if (serverMessage.equalsIgnoreCase("exit")) {
  424.                 System.out.println("Server exited.");
  425.                 break;
  426.             }
  427.         }
  428.         serverSocket.close();
  429.     }
  430. }
  431.  
  432. CLIENT ->
  433.    
  434. import java.net.*;
  435. import java.io.*;
  436. public class UDP_Receiver {
  437.     public static void main(String[] args) throws IOException {
  438.         DatagramSocket clientSocket = new DatagramSocket();
  439.        InetAddress serverAddress = InetAddress.getByName("localhost");         byte[] sendBuffer;
  440.         byte[] receiveBuffer = new byte[1024];
  441.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  442.  
  443.         while (true) {
  444.             System.out.print("Client: ");
  445.             String clientMessage =  DatagramPacket(receiveBuffer, receiveBuffer.length);
  446.  
  447.             sendBuffer = clientMessage.getBytes();
  448.    DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, serverAddress, 9876);
  449.             clientSocket.send(sendPacket); // Send the message to the server
  450.  
  451.             // Receiving data from server
  452.       DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
  453.             clientSocket.receive(receivePacket); // Block until data is received
  454.          String serverMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
  455.             System.out.println("Server: " + serverMessage);
  456.  
  457.             // Exit on typing "exit"
  458.             if (clientMessage.equalsIgnoreCase("exit")) {
  459.                 System.out.println("Client exited.");
  460.                 break;
  461.             }
  462.         }
  463.         clientSocket.close();
  464.     }
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement