Computer Network Laboratory Computer Network Laboratory INDEX EXP NO TITLE OF EXPERIMENT PAGE NO 1 PART-A Write a program for error detecting code using CRC-CCITT (16- bits). 2 2 Write a program for simple RSA algorithm to encrypt and decrypt the data. 5 3 Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present. 8 4 Write a program for Congestion Control using Leaky Bucket algorithm. 11 1 PART-B Implement three nodes point – to – point network with duplex links between them. Set the queue size, vary the bandwidth and find the number of packets dropped. 15 2 Implement a four node point-to-point network with duplex links connected as follows: n0-n2,n1-n2 and n2-n3.Apply TCP agent between n0-n3 and UDP between n1-n3.Apply relevant applications over TCP and UDP agents changing the parameter and determine the number of packets sent by TCP/UDP. 18 3 Implement transmission of ping messages/trace route over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. 21 4 Implement an Ethernet LAN using n-nodes and set multiple traffic nodes and determine collision across different nodes. 23 5 Implement an Ethernet LAN using n-nodes and set multiple traffic nodes and plot congestion window for different source / destination. 25 Department of Computer science and Engineering Faculty of Engineering and Technology Page 1 Computer Network Laboratory 1. Write a program for error detecting code using CRC-CCITT (16- bits). import java.io.*; import java.util.Scanner; public class CRC { public static void main(String args[]){ Scanner sc = new Scanner(System.in); //Input Data Stream System.out.println("Enter data stream:"); String datastream = sc.nextLine(); System.out.println("Enter generator: "); String generator = sc.nextLine(); int l1 = datastream.length() ; int l2 = generator.length() ; int l3=l1+l2-1; int data[] = new int[l3]; int divisor[] = new int[generator.length()]; for(int i=0;i<datastream.length();i++) data[i] = Integer.parseInt(datastream.charAt(i)+""); for(int i=0;i<generator.length();i++) divisor[i] = Integer.parseInt(generator.charAt(i)+""); //Calculation of CRC for(int i=0;i<datastream.length();i++){ if(data[i]==1) for(int j=0;j<divisor.length;j++) data[i+j] ^= divisor[j]; } //Display CRC System.out.println("The CRC code is:" ); for(int i=0;i<datastream.length();i++) data[i] = Integer.parseInt(datastream.charAt(i)+""); for(int i=0;i<data.length;i++) System.out.print(data[i]); System.out.println(); //Check for input CRC code System.out.print("Enter CRC code: "); datastream = sc.nextLine(); System.out.print("Enter generator: "); generator = sc.nextLine(); int l4 = datastream.length() ; int l5= generator.length(); int l6= l4 + l5 - 1; data = new int[l6]; divisor = new int[generator.length()]; for(int i=0;i<datastream.length();i++) data[i] = Integer.parseInt(datastream.charAt(i)+""); Department of Computer science and Engineering Faculty of Engineering and Technology Page 2 Computer Network Laboratory for(int i=0;i<generator.length();i++) divisor[i] = Integer.parseInt(generator.charAt(i)+""); //Calculation of remainder for(int i=0;i<datastream.length();i++){ if(data[i]==1) for(int j=0;j<divisor.length;j++) data[i+j] ^= divisor[j]; } //Display validity of data boolean valid = true; for(int i=0;i<data.length;i++) if(data[i]==1){ valid = false; break; } if(valid==true) System.out.println("Data stream is valid"); else System.out.println("Data stream is invalid. CRC error occured."); } } Department of Computer science and Engineering Faculty of Engineering and Technology Page 3 Computer Network Laboratory Output: Enter Data Stream 11011 Enter generator 100 The CRC Code is 1101100 Enter CRC Code 1101100 Enter Generator 100 Data Stream is valid Output: 2 Enter Data Stream 11011 Enter generator 100 The CRC Code is 1101100 Enter CRC Code 11011001 Enter Generator 101 Data Stream is invalid Department of Computer science and Engineering Faculty of Engineering and Technology Page 4 Computer Network Laboratory 2. Write a program for simple RSA algorithm to encrypt and decrypt the data. import java.io.DataInputStream; import java.io.IOException; import java.math.BigInteger; import java.util.Random; public class RSA { private BigInteger p; private BigInteger q; private BigInteger N; private BigInteger phi; private BigInteger e; private BigInteger d; private int bitlength = 1024; private Random r; public RSA() { r = new Random(); p = BigInteger.probablePrime(bitlength, r); q = BigInteger.probablePrime(bitlength, r); N = p.multiply(q); phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); e = BigInteger.probablePrime(bitlength / 2, r); while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0) { e.add(BigInteger.ONE); } d = e.modInverse(phi); } public RSA(BigInteger e, BigInteger d, BigInteger N) { this.e = e; this.d = d; this.N = N; } @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { RSA rsa = new RSA(); DataInputStream in = new DataInputStream(System.in); String teststring; Department of Computer science and Engineering Faculty of Engineering and Technology Page 5 Computer Network Laboratory System.out.println("Enter the plain text:"); teststring = in.readLine(); System.out.println("Encrypting String: " + teststring); System.out.println("String in Bytes: " + bytesToString(teststring.getBytes())); // encrypt byte[] encrypted = rsa.encrypt(teststring.getBytes()); // decrypt byte[] decrypted = rsa.decrypt(encrypted); System.out.println("Decrypting Bytes: " + bytesToString(decrypted)); System.out.println("Decrypted String: " + new String(decrypted)); } private static String bytesToString(byte[] encrypted) { String test = ""; for (byte b : encrypted) { test += Byte.toString(b); } return test; } // Encrypt message public byte[] encrypt(byte[] message) { return (new BigInteger(message)).modPow(e, N).toByteArray(); } // Decrypt message public byte[] decrypt(byte[] message) { return (new BigInteger(message)).modPow(d, N).toByteArray(); } } Department of Computer science and Engineering Faculty of Engineering and Technology Page 6 Computer Network Laboratory Output: Enter Plain text: HELLO WORLD Encrypting String: HELLO WORLD String in bytes: 10410 110810811132119111114108100 Decrypting bytes: 10410 110810811132119111114108100 Decrypting String: HELLO WORLD Department of Computer science and Engineering Faculty of Engineering and Technology Page 7 Computer Network Laboratory 3. Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present. import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws Exception { Socket sock = new Socket("127.0.0.1", 3000); // reading from keyboard (keyRead object) BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); // sending to client (pwrite object) OutputStream ostream = sock.getOutputStream(); PrintWriter pwrite = new PrintWriter(ostream, true); // receiving from server ( receiveRead object) InputStream istream = sock.getInputStream(); BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); System.out.println("Start the chitchat, type and press Enter key"); String receiveMessage, sendMessage; while(true) { sendMessage = keyRead.readLine(); // keyboard reading pwrite.println(sendMessage); // sending to server pwrite.flush(); // flush the data if((receiveMessage = receiveRead.readLine()) != null) //receive from server { System.out.println(receiveMessage); // displaying at DOS prompt } } } } Department of Computer science and Engineering Faculty of Engineering and Technology Page 8 Computer Network Laboratory import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket sersock = new ServerSocket(3000); System.out.println("Server ready for chatting"); Socket sock = sersock.accept( ); // reading from keyboard (keyRead object) BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); // sending to client (pwrite object) OutputStream ostream = sock.getOutputStream(); PrintWriter pwrite = new PrintWriter(ostream, true); // receiving from server ( receiveRead object) InputStream istream = sock.getInputStream(); BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); String receiveMessage, sendMessage; while(true) { if((receiveMessage = receiveRead.readLine()) != null) { System.out.println(receiveMessage); } sendMessage = keyRead.readLine(); pwrite.println(sendMessage); pwrite.flush(); } } } Department of Computer science and Engineering Faculty of Engineering and Technology Page 9 Computer Network Laboratory Output: Client C:\Documents and settings\cnlab>cd\ C:\cd program files C:\Program Files>cd java C:\Program Files\java>cd pgm3 C:\Program Files\java\pgm3>javac C:\Program Files\java\pgm3>javac Client.java C:\Program Files\java\pgm3>java Client WELCOME TO NETWORKS LAB Server C:\Documents and settings\cnlab>cd\ C:\cd program files C:\Program Files>cd java C:\Program Files\java>cd pgm3 C:\Program Files\java\pgm3>javac C:\Program Files\java\pgm3>javac Server.java C:\Program Files\java\pgm3>java Server Ready to type now WELCOME TO NETWORKS LAB Department of Computer science and Engineering Faculty of Engineering and Technology Page 10 Computer Network Laboratory 4. Write a program for congestion control using leaky bucket algorithm. Leaky Bucket Implementation import java.util.*; public class leaky { public static void main(String[] args) { Scanner my = new Scanner(System.in); int no_groups,bucket_size; System.out.print("\n Enter the bucket size : \t"); bucket_size = my.nextInt(); System.out.print("\n Enter the no of groups : \t"); no_groups = my.nextInt(); int no_packets[] = new int[no_groups]; int in_bw[] = new int[no_groups]; int out_bw,reqd_bw=0,tot_packets=0; for(int i=0;i<no_groups;i++) { System.out.print("\n Enter the no of packets for group " + (i+1) + "\t"); no_packets[i] = my.nextInt(); System.out.print("\n Enter the input bandwidth for the group " + (i+1) + "\t"); in_bw[i] = my.nextInt(); if((tot_packets+no_packets[i])<=bucket_size) { tot_packets += no_packets[i]; } else { do { System.out.println(" Bucket Overflow "); System.out.println(" Enter value less than " + (bucket_size-tot_packets)); no_packets[i] = my.nextInt(); }while((tot_packets+no_packets[i])>bucket_size); tot_packets += no_packets[i]; } reqd_bw += (no_packets[i]*in_bw[i]); } System.out.println("\nThe total required bandwidth is " + reqd_bw); System.out.println("Enter the output bandwidth "); Department of Computer science and Engineering Faculty of Engineering and Technology Page 11 Computer Network Laboratory out_bw = my.nextInt(); int temp=reqd_bw; int rem_pkts = tot_packets; while((out_bw<=temp)&&(rem_pkts>0)) { System.out.println("Data Sent \n" + (--rem_pkts) + " packets remaining"); System.out.println("Remaining Bandwidth " + (temp -= out_bw)); if((out_bw>temp)&&(rem_pkts>0)) System.out.println(rem_pkts + " packet(s) discarded due to insufficient bandwidth"); } } } Department of Computer science and Engineering Faculty of Engineering and Technology Page 12 Computer Network Laboratory Output: Enter the bucket size 3 Enter the Number of groups 2 Enter the number of packets for group1 1 Enter the input bandwidth for group1 2 Enter the number of packets for groups2 3 Enter the input bandwidth for group2 1 Bucket overflow Enter the value less than 2 1 The total required bandwidth is 3 Enter the output bandwidth 2 Data sent 1 packet remaining Remaining bandwidth 2 Data sent 0 packets remaining Remaining bandwidth2 BUILD SUCCESSFULL Department of Computer science and Engineering Faculty of Engineering and Technology Page 13 Computer Network Laboratory PART-B Boot your machine from NCTUNS. 1- Right click on your mouse and open terminal and type. /usr/local/nctuns/bin/dispatcher 2- Right click on your mouse and open another terminal and type. /usr/local/nctuns/bin/coordinator 3- Right click on your mouse and open terminal and type. /usr/local/nctuns/bin/nctunsclient. After opening third terminal a working window will be shown. Select the draw topology. Department of Computer science and Engineering Faculty of Engineering and Technology Page 14 Computer Network Laboratory 1. Implement three node point to point network with duplex links between them. Set the queue size,vary the bandwidth and find the number of packets dropped. STEPS: Steps to draw the topology. Step1:Select the switch icon on the toolbar and drag it on the working window. Step2:Select the host icon on the toolbar and drag it on the working window. Repeat this for the number of host required. Step3:Select the link icon on the toolbar and drag it on the screen from host1 to switch and again from host2 to switch. Here the switch acts as node3 in the point to point network.This leads to the creation of 3 node point to point network topology.Save this topology by .tpl extension by going to the edit mode on the toolbar.Move the mouse near the host icon to see the IP address, in the same manner check the IP address of the remaining host and note down.p.Click on the command box and type the command Step4:Double click on host1.Click on the node editor and different layer interface will be shown like ARP,FIFO,MAC,TCP_DUMP.From these select full duplex for switch and hald duplex for hubs and in log statistics select number of drop packets.Then click on ADD.By this another dialog box pops up.Click on the command box and type the command according to the following syntax. For HUB: stg[-l duration(sec)][-u port number] HostIP address Ex:stg –u 9800 HostIPadrr 1.0.1.2 For switch: stcp[-p port number][-l writesize] HostIPAddress Ex:stcp –p 9800 –l 1024 1.0.1.2 And click ok.Here host1 acts as sender and HostIPAddr is the address of destination(host2). Step5:Double click on host2 and follow the same steps as shown in step4 with a only change in the command according to the following syntax. For HUB:rtg[-i][-w log] [-p port number] Ex:rtg –u –w log1 For switch:rtcp [–p port number][-l write size] Ex:rtcp –p 9800 –l 1024 Department of Computer science and Engineering Faculty of Engineering and Technology Page 15 Computer Network Laboratory And click on ok. Step6:Double click on the link between node1 and switch to set the bandwidth to some initial value. Step7:Click on the E button to save the changes.Now click on the R button to Run the simulation. Step8:Then goto Menu→Simulation→Run.When the server is executing the user will see the simulation playback time knot at the bottom of the screen moving. Step9:To start the simulation playback mode “p” the user can left click the start icon of the time bar.The animation is played out.During this process plot the graph for this goto Menu→Tools→plot graph press enter.Here we will see the graph window in which open the file name .results folder and select number of drop packets by File→open. During animation three types of data lines are seen. 1.Red line indicates data is transmitted not received. 2. Blue line indicates the acknowledgement. 3. Yellow line shows the connection. Step10:Run the simulation Step11:To view the different results go to filename.results. Department of Computer science and Engineering Faculty of Engineering and Technology Page 16 Computer Network Laboratory Output: Department of Computer science and Engineering Faculty of Engineering and Technology Page 17 Computer Network Laboratory 2. Implement a four node point-to-point network with duplex links connected as follows: n0-n2, n1-n2 and n2-n3.Apply TCP agent between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP and UDP agents changing the parameter and determine the number of packets sent by TCP/UDP. STEPS: Steps to draw the topology Step 1. select the hub icon on the toolbar and drag it onto the working window. Step 2. select the host icon on the toolbar and drag it onto the working window. Repeat this for another 2 host icons. Step 3. select the link icon on the toolbar and drag it on the screen from host1 to the hub, repeat this for another 2 host icons. Step 4. Go to edit mode "E" and save the topology. Move the mouse near the host icon to see the IP address, in the same manner check the IP addressess of the remaining host and note down. Step 5. setup a TCP connection between node1 and node2 using the following commands. 1. double-click on host1. click on node editor and different layeras interface will be show like ARP,FIFO,MAC,TCPDUMP,Physical layers .from those select MAC and then select full-duplex for switches and roters and half-duplex for hubs,and in log statistics select the output and input throughput log to determine the number of packets.press ok. then click on Add. by this another dialogbox popsup. click on the command box and type the command according to the following syntax EX: stcp –p 9800 -1 1024 1.0.1.2 (IP addr of host2) And click on ok 2. double-click on host 2.follow the same steps as shown in above step with a only changes in the command. EX: rtcp -p 9800 -l 1024 Step 6. setup a UDP connection between node2 and node3 using the hub and the following commands. 1. Double-click on host 2. follow the same steps as shown in above steps with a only changes in the command. Department of Computer science and Engineering Faculty of Engineering and Technology Page 18 Computer Network Laboratory EX: stg-u 9800 hostIP Addrr 2. double-click on host3 follow the same steps as shown in above steps with a only change in the command. EX: rtg-u-w log1 Step 7. click on the E button to save the changes. now click on the R button, to run the simulation. Step 8. Then go to menu -->simulation-->run when the sever is executing ,the user will see the time.knot at the bottom of the screen moving . Step 9. to start the simulation play back mode "P" the user can left -click the start icon()of the time bar .The animation is played out.during this process plot the graphs for this go to menu ->tools-->polt graphs press enter . Here we will see a graph window in which open the filename . results folder and select the output and input throughput log to determine the number of packets. Department of Computer science and Engineering Faculty of Engineering and Technology Page 19 Computer Network Laboratory Output: Department of Computer science and Engineering Faculty of Engineering and Technology Page 20 Computer Network Laboratory 3.Implement transmission of ping messages/ trace route over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. STEPS: Steps to draw the topology Step1:Click on the subnet icon on the toolbar and then click on the screen of the working window. Step2:Select the number of host as 6 and bandwidth as 100. Step3:In the edit mode get the IP address of one of the host say host1and then for other host say host2.Set drop packets and number of collisions statistics. Step4:Save topology and perform the same steps and Run the simulation. Step5:Click on any one of the host and click on the command console and ty Ng and destination IP address as #ping 1.0.1.5 press enter Step6:The number of packets are obtained only when the traffic is more on the network. Department of Computer science and Engineering Faculty of Engineering and Technology Page 21 Computer Network Laboratory Output: Department of Computer science and Engineering Faculty of Engineering and Technology Page 22 Computer Network Laboratory 4.Implement an Ethernet LAN using n-nodes and set multiple traffic nodes and determine collision across different nodes. STEPS: Steps to draw the topology Step1:Select the Hub1 icon on the toolbar and drag it on the working window. Step2:Select the host icon on the toolbar and drag it on the working window.Repeat this for another 3 host icons. Step3:Select the Hub2 icon on the toolbar and drag it on the working window. Step4:Select the host icon on the toolbar and drag it on the working window.Repeat this for another 3 host icons.Step3 and step4 creates second side LAN connect these two hubs with a switch,this forms an Ethernet LAN Step5:Save the topology by filename.tpl extension Step6:Double click on host1 on hub1,click on ADD and type the command as Stcp –p 9800 –l 1024 1.0.1.4 and click on ok. Here don’t select MAC layer. Step7:Double click on host2 on hub1,click on ADD and type the command as Stcp –p 9800 –l 1024 1.0.1.5 and click on ok. Here don’t select MAC layer. Step8:Double click on host4 on hub2,Select only number of collision packets,click on ADD and type the command as rtcp –p 9800 –l 1024 and click on ok. Step9:Double click on host5 on hub2,Select only number of collision packets,click on ADD and type the command as rtcp –p 9800 –l 1024 and click on ok. Step10: Click on the E button to save the changes.Now click on the R button to Run the simulation. Step11:Then goto Menu→Simulation→Run.When the server is executing the user will see the simulation playback time knot at the bottom of the screen moving. Department of Computer science and Engineering Faculty of Engineering and Technology Page 23 Computer Network Laboratory Step12:To start the simulation playback mode “p” the user can left click the start icon of the time bar.The animation is played out.During this process plot the graph for this goto Menu→Tools→plot graph press enter.Here we will see the graph window in which open the file name .results folder and select number of drop packets by File→open. Output: Department of Computer science and Engineering Faculty of Engineering and Technology Page 24 Computer Network Laboratory 5.Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot congestion window for different source /destination. STEPS: Steps to draw the topology Step1:Select the Hub1 icon on the toolbar and drag it on the working window. Step2:Select the host icon on the toolbar and drag it on the working window.Repeat this for another 3 host icons. Step3:Select the Hub2 icon on the toolbar and drag it on the working window. Step4:Select the host icon on the toolbar and drag it on the working window.Repeat this for another 3 host icons.Step3 and step4 creates second side LAN connect these two hubs with a switch,this forms an Ethernet LAN Step5:Save the topology by filename.tpl extension Step6:Double click on host1 on hub1,click on ADD and type the command as Stcp –p 9800 –l 1024 1.0.1.4 and click on ok. Stcp –p 9800 –l 1024 1.0.1.5 and click on ok. Here don’t select MAC layer. Step8:Double click on host4 on hub2,Select only number of collision packets,click on ADD and type the command as rtcp –p 9800 –l 1024 and click on ok. Step9:Double click on host5 on hub2,Select only number of collision packets,click on ADD and type the command as rtcp –p 9800 –l 1024 and click on ok. Step10: Click on the E button to save the changes.Now click on the R button to Run the simulation. Step11:Then goto Menu→Simulation→Run.When the server is executing the user will see the simulation playback time knot at the bottom of the screen moving. Step12:To start the simulation playback mode “p” the user can left click the start icon of the time bar.The animation is played out.During this process plot the graph for this goto Department of Computer science and Engineering Faculty of Engineering and Technology Page 25 Computer Network Laboratory Menu→Tools→plot graph press enter.Here we will see the graph window in which open the file name .results folder and select number of drop packets by File→open. Output: Department of Computer science and Engineering Faculty of Engineering and Technology Page 26