Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## ----------- Simulation Program 1 (Simple point-to-point)
- set ns [new Simulator] ;# Letter S is capital
- set nf [open 1.nam w] ;# open a nam trace file in write mode
- $ns namtrace-all $nf ;# nf nam filename
- set tf [open 1.tr w] ;# tf trace filename
- $ns trace-all $tf
- proc finish { } {
- global ns nf tf
- $ns flush-trace ;# clears trace file contents
- close $nf
- close $tf
- exec nam 1.nam &
- exit 0
- }
- set n0 [$ns node] ;# creates 3 nodes
- set n2 [$ns node]
- set n3 [$ns node]
- $ns duplex-link $n0 $n2 200Mb 10ms DropTail ;# establishing links
- $ns duplex-link $n2 $n3 1Mb 1000ms DropTail
- $ns queue-limit $n0 $n2 10
- set udp0 [new Agent/UDP] ;# attaching transport layer protocols
- $ns attach-agent $n0 $udp0
- set cbr0 [new Application/Traffic/CBR] ;# attaching application layer
- $cbr0 set packetSize_ 500
- $cbr0 set interval_ 0.005
- $cbr0 attach-agent $udp0
- set null0 [new Agent/Null] ;# creating sink (destination) node
- $ns attach-agent $n3 $null0
- $ns connect $udp0 $null0
- $ns at 0.1 "$cbr0 start"
- $ns at 1.0 "finish"
- $ns run
- ########## --- AWK FILE CODE (file1.awk)
- BEGIN{ c=0;}
- {
- if($1=="d")
- { c++;
- printf("%s\t%s\n",$5,$11);
- }
- }
- END{ printf("The number of packets dropped is %d\n",c); }
- #### to run in terminal -- awk -f file1.awk 2.tr
- ## --------------- Simulation Program 2 (Transmission of ping messages/traceroute)
- # Create a new simulator instance
- set ns [new Simulator]
- set p2 [new Agent/Ping]
- # Open output files for trace and NAM visualization
- set nf [open 2.nam w]
- $ns namtrace-all $nf
- set tf [open 2.tr w]
- $ns trace-all $tf
- # Create network nodes
- set n0 [$ns node]
- set n1 [$ns node]
- set n2 [$ns node]
- set n3 [$ns node]
- set n4 [$ns node]
- set n5 [$ns node]
- # Establish links between nodes
- $ns duplex-link $n0 $n4 1005Mb 1ms DropTail
- $ns duplex-link $n1 $n4 50Mb 1ms DropTail
- $ns duplex-link $n2 $n4 2000Mb 1ms DropTail
- $ns duplex-link $n3 $n4 200Mb 1ms DropTail
- $ns duplex-link $n4 $n5 1Mb 1ms DropTail
- # Create and attach Ping agents
- set p1 [new Agent/Ping]
- $ns attach-agent $n0 $p1
- $p1 set packetSize_ 50000
- $p1 set interval_ 0.0001
- set p2 [new Agent/Ping]
- $ns attach-agent $n1 $p2
- set p3 [new Agent/Ping]
- $ns attach-agent $n2 $p3
- $p3 set packetSize_ 30000
- $p3 set interval_ 0.00001
- set p4 [new Agent/Ping]
- $ns attach-agent $n3 $p4
- set p5 [new Agent/Ping]
- $ns attach-agent $n5 $p5
- # Set queue limits on specific links
- $ns queue-limit $n0 $n4 5
- $ns queue-limit $n2 $n4 3
- $ns queue-limit $n4 $n5 2
- # Define the 'recv' method for the Ping agent
- Agent/Ping instproc recv {from rtt} {
- $self instvar node_
- puts "node [$node_ id] received answer from $from with round trip time $rtt msec"
- }
- # Connect the agents for communication
- $ns connect $p1 $p5
- $ns connect $p3 $p4
- # Define a finish procedure to close trace files and launch NAM
- proc finish { } {
- global ns nf tf
- $ns flush-trace
- close $nf
- close $tf
- exec nam 2.nam &
- exit 0
- }
- # Schedule packet sending events
- for {set i 1} {$i <= 10} {incr i} {
- set time [expr $i * 0.1]
- $ns at $time "$p1 send"
- $ns at $time "$p3 send"
- }
- # Schedule the end of the simulation
- $ns at 2.0 "finish"
- # Run the simulation
- $ns run
- ##--------- Simulation Program 3 (Ethernet LAN using n nodes)
- set ns [new Simulator]
- # Open trace and NAM trace files
- set tf [open 3.tr w]
- $ns trace-all $tf
- set nf [open 3.nam w]
- $ns namtrace-all $nf
- # Create nodes and set their properties
- set n0 [$ns node]
- $n0 color "magenta"
- $n0 label "src1"
- set n1 [$ns node]
- set n2 [$ns node]
- $n2 color "magenta"
- $n2 label "src2"
- set n3 [$ns node]
- $n3 color "blue"
- $n3 label "dest2"
- set n4 [$ns node]
- set n5 [$ns node]
- $n5 color "blue"
- $n5 label "dest1"
- # Create LAN and duplex links
- $ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
- $ns duplex-link $n4 $n5 1Mb 1ms DropTail
- # Create TCP agents and FTP applications
- set tcp0 [new Agent/TCP]
- $ns attach-agent $n0 $tcp0
- set sink5 [new Agent/TCPSink]
- $ns attach-agent $n5 $sink5
- $ns connect $tcp0 $sink5
- set ftp0 [new Application/FTP]
- $ftp0 attach-agent $tcp0
- $ftp0 set packetSize_ 500
- $ftp0 set interval_ 0.0001
- set tcp2 [new Agent/TCP]
- $ns attach-agent $n2 $tcp2
- set sink3 [new Agent/TCPSink]
- $ns attach-agent $n3 $sink3
- $ns connect $tcp2 $sink3
- set ftp2 [new Application/FTP]
- $ftp2 attach-agent $tcp2
- $ftp2 set packetSize_ 600
- $ftp2 set interval_ 0.001
- # Create output files for tracing TCP windows
- set file1 [open 3_file1.tr w]
- $tcp0 attach $file1
- set file2 [open 3_file2.tr w]
- $tcp2 attach $file2
- # Enable congestion window tracing
- $tcp0 trace cwnd_
- $tcp2 trace cwnd_
- # Define the finish procedure
- proc finish { } {
- global ns nf tf
- $ns flush-trace
- close $tf
- close $nf
- exec nam 3.nam &
- exit 0
- }
- # Schedule events
- $ns at 0.1 "$ftp0 start"
- $ns at 5 "$ftp0 stop"
- $ns at 7 "$ftp0 start"
- $ns at 0.2 "$ftp2 start"
- $ns at 8 "$ftp2 stop"
- $ns at 14 "$ftp0 stop"
- $ns at 10 "$ftp2 start"
- $ns at 15 "$ftp2 stop"
- $ns at 16 "finish"
- # Run the simulation
- $ns run
- ##---------- Sliding Window
- class SlidingWindow {
- int windowSize;
- int totalPackets;
- int currentPacket;
- SlidingWindow(int windowSize, int totalPackets) {
- this.windowSize = windowSize;
- this.totalPackets = totalPackets;
- this.currentPacket = 0; // Start from the first packet
- }
- // Method to send packets
- public void sendPackets() {
- while (currentPacket < totalPackets) {
- // Send packets within the window limit
- for (int i = 0; i < windowSize && currentPacket < totalPackets; i++) {
- System.out.println("Sending packet " + currentPacket);
- currentPacket++;
- }
- // Simulate receiving an acknowledgment
- receiveAck();
- }
- }
- // Method to simulate receiving acknowledgment for the window
- private void receiveAck() {
- System.out.println("Acknowledgment received for window up to packet " + (currentPacket - 1));
- }
- }
- public class SW {
- public static void main(String[] args) {
- int windowSize = 4; // Size of the sliding window
- int totalPackets = 10; // Total number of packets to send
- SlidingWindow slidingWindow = new SlidingWindow(windowSize, totalPackets);
- slidingWindow.sendPackets(); // Start the packet transmission
- }
- }
- ##---------- Send file over TCP/IP
- SERVER ->
- import java.io.*;
- import java.net.*;
- public class server {
- public static void main(String[] args) {
- ServerSocket serverSocket = null;
- Socket clientSocket = null;
- try {
- // Create a server socket listening on port 9876
- serverSocket = new ServerSocket(9876);
- System.out.println("Server started. Waiting for a client...");
- // Accept client connection
- clientSocket = serverSocket.accept();
- System.out.println("Client connected!");
- // Input and output streams for client communication
- BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
- // Read the file name requested by the client
- String fileName = inFromClient.readLine();
- System.out.println("Client requested file: " + fileName);
- // Attempt to open the requested file
- File file = new File(fileName);
- if (file.exists() && !file.isDirectory()) {
- // If the file exists, send back the file contents
- BufferedReader fileReader = new BufferedReader(new FileReader(file));
- String line;
- outToClient.println("FILE_FOUND");
- while ((line = fileReader.readLine()) != null) {
- outToClient.println(line);
- }
- fileReader.close();
- System.out.println("File sent successfully.");
- } else {
- // If the file doesn't exist, inform the client
- outToClient.println("FILE_NOT_FOUND");
- System.out.println("Requested file not found.");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- // Close the connections
- try {
- if (clientSocket != null) clientSocket.close();
- if (serverSocket != null) serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- CLIENT ->
- import java.io.*;
- import java.net.*;
- public class Client {
- public static void main(String[] args) {
- Socket socket = null;
- try {
- socket = new Socket("localhost", 9876);
- System.out.println("Connected to the server!");
- BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintWriter outToServer = new PrintWriter(socket.getOutputStream(), true);
- BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Enter the name of the file to request: ");
- String fileName = userInput.readLine();
- // Send the file name to the server
- outToServer.println(fileName);
- // Read the response from the server
- String serverResponse = inFromServer.readLine();
- if ("FILE_FOUND".equals(serverResponse)) {
- System.out.println("File found! Receiving content:");
- String line;
- while ((line = inFromServer.readLine()) != null) {
- System.out.println(line);
- }
- } else if ("FILE_NOT_FOUND".equals(serverResponse)) {
- System.out.println("File not found on the server.");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- // Close the connection
- try {
- if (socket != null) socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- ##-------- Client/Server Message Send/Recieve UDP Datagram program
- SERVER ->
- import java.net.*;
- import java.io.*;
- public class UDP_Server {
- public static void main(String[] args) throws IOException {
- DatagramSocket serverSocket = new DatagramSocket(9876); // Server listens on port 9876
- byte[] receiveBuffer = new byte[1024];
- byte[] sendBuffer;
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Server started. Waiting for client messages...");
- while (true) {
- // Receiving data from client
- DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
- serverSocket.receive(receivePacket); // Block until data is received
- String clientMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
- System.out.println("Client: " + clientMessage);
- // Server typing and sending response
- System.out.print("Server: ");
- String serverMessage = reader.readLine();
- sendBuffer = serverMessage.getBytes();
- InetAddress clientAddress = receivePacket.getAddress();
- int clientPort = receivePacket.getPort();
- DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, clientAddress, clientPort);
- serverSocket.send(sendPacket); // Send the message to client
- // Exit on typing "exit"
- if (serverMessage.equalsIgnoreCase("exit")) {
- System.out.println("Server exited.");
- break;
- }
- }
- serverSocket.close();
- }
- }
- CLIENT ->
- import java.net.*;
- import java.io.*;
- public class UDP_Receiver {
- public static void main(String[] args) throws IOException {
- DatagramSocket clientSocket = new DatagramSocket();
- InetAddress serverAddress = InetAddress.getByName("localhost"); byte[] sendBuffer;
- byte[] receiveBuffer = new byte[1024];
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- System.out.print("Client: ");
- String clientMessage = DatagramPacket(receiveBuffer, receiveBuffer.length);
- sendBuffer = clientMessage.getBytes();
- DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, serverAddress, 9876);
- clientSocket.send(sendPacket); // Send the message to the server
- // Receiving data from server
- DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
- clientSocket.receive(receivePacket); // Block until data is received
- String serverMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
- System.out.println("Server: " + serverMessage);
- // Exit on typing "exit"
- if (clientMessage.equalsIgnoreCase("exit")) {
- System.out.println("Client exited.");
- break;
- }
- }
- clientSocket.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement