├── ASSIGNMENT NO 1 CN.txt ├── Array ├── TCPClientArray.java ├── TCPServerArray.java ├── UDPClientArray.java └── UDPServerArray.java ├── Lab Assignment 5 TCPdump.pdf ├── README.md ├── TCP GENERAL ├── TCPClient.java └── TCPServer.java ├── TCPClient.java ├── TCPClient_revint.java ├── TCPEvenOddClient.java ├── TCPEvenOddServer.java ├── TCPServer.java ├── TCPServer_revint.java ├── UDP GENERAL ├── UDPClient.java └── UDPServer.java ├── UDPCLient_revint.java ├── UDPClient_vowels.java ├── UDPClientvow.java ├── UDPServer_revint.java ├── UDPServer_vowels.java ├── UDPServervow.java ├── link.tcl ├── routing.tcl ├── tcp client mam.java ├── tcp only.tcl ├── tcp server mam.java ├── tr file read.jpg ├── udp client mam.java ├── udp only.tcl ├── udp server mam.java ├── udp tcp mix.txt ├── udpclientsqrt.java ├── udpserversqrt.java └── varaitions.java /ASSIGNMENT NO 1 CN.txt: -------------------------------------------------------------------------------- 1 | ASSIGNMENT NO 1 2 | 3 | Ping: "Ping" is a computer networking tool that is used to test the reachability of a networked device and measure the time it takes for packets of data to travel from the source to the destination and back. The "ping" command sends a series of small packets of data to the target device and measures the time it takes for the device to respond to each packet. The time it takes for the packets to travel to the target and back is known as the "round-trip time," and this information can be used to diagnose network connectivity problems, determine the speed and reliability of a network connection, or evaluate the performance of a device. 4 | 5 | -a : Resolve addresses to hostnames. 6 | -w timeout : Timeout in milliseconds to wait for each reply. 7 | -f : Set Don't Fragment flag in packet (IPv4-only). 8 | -i : Specifies the value of the Time To Live (TTL) field in the IP header for echo Request messages sent. The default is the default TTL value for the host. The maximum TTL is 255. 9 | -n size: This option is used to control the number of packets sent to the target device. 10 | 11 | ------------------------------------------------------------------------------------------------------------------------------------------------------- 12 | 13 | Ipconfig: IPCONFIG stands for Internet Protocol Configuration. This is a command-line application which displays all the current TCP/IP (Transmission Control Protocol/Internet Protocol) network configuration, refreshes the DHCP (Dynamic Host Configuration Protocol) and DNS (Domain Name Server). It also displays IP address, subnet mask, and default gateway for all adapters. 14 | /all: Displays the full TCP/IP configuration for all adapters. Adapters can represent physical interfaces, such as installed network adapters, or logical interfaces, such as dial-up connections 15 | /displaydns: Displays the contents of the DNS client resolver cache, which includes both entries preloaded from the local Hosts file and any recently obtained resource records for name queries resolved by the computer. The DNS Client service uses this information to resolve frequently queried names quickly, before querying its configured DNS servers. 16 | /flushdns: Flushes and resets the contents of the DNS client resolver cache. During DNS troubleshooting, you can use this procedure to discard negative cache entries from the cache, as well as any other entries that have been added dynamically. 17 | /release : Releases the current IP address assigned to the computer, effectively resetting the IP configuration. This parameter sends a request to the DHCP server to abandon the active lease(s) and removes it (or them) from your system. 18 | /renew: Requests a new IP address from the DHCP server. 19 | 20 | ------------------------------------------------------------------------------------------------------------------------------------------------------- 21 | 22 | Tracert: This diagnostic tool determines the path taken to a destination by sending Internet Control Message Protocol (ICMP) echo Request or ICMPv6 messages to the destination with incrementally increasing time to live (TTL) field values. 23 | a)-d: Stops attempts to resolve the IP addresses of intermediate routers to their names. This can speed up the return of results. 24 | b)-h: Specifies the maximum number of hops in the path to search for the target (destination). The default is 30 hops. 25 | c)-w: Specifies the amount of time in milliseconds to wait for the ICMP time Exceeded or echo Reply message corresponding to a given echo Request message to be received. If not received within the time-out, an asterisk (*) is displayed. The default time-out is 4000 (4 seconds). 26 | d)-4: Specifies that tracert.exe can use only IPv4 for this trace. 27 | e) -j : Specifies a loose source route for the trace, allowing you to specify the intermediate hops that packets should take along the way. 28 | The tracepath and traceroute terminal programs are crucial in network diagnostics. Both commands map the network and display possible packet routes and transit delays from a source to a destination. 29 | 30 | ------------------------------------------------------------------------------------------------------------------------------------------------------- 31 | 32 | 4)nslookup: Displays information that you can use to diagnose Domain Name System infrastructure. The nslookup command-line tool has two modes: interactive and noninteractive. 33 | a)-d2: This performs a lookup of all addresses associated with the specified hostname. 34 | b) -query=mx : This performs a lookup of the mail exchange (MX) records associated with the specified domain, which are used to route email for that domain. 35 | c)-type=ns: By checking the NS records, you can see which is the authoritative server for a specific domain. 36 | d)-type=soa: you can see the start of authority and get information about the zone. 37 | 38 | --------------------------------------------------------------------------------------------------------------------------------------------------------- 39 | 40 | 5)netstat: Netstat stands for “network statistics”. If you’re having difficulties accessing the internet, the netstat command can help you identify where the problem lies. Netstat will display all of your computer’s active network connections and the status of those connections. If a connection is not working, netstat can often provide more information about why it is not working. 41 | a)-a: Shows all active connections and listening ports on the computer 42 | b)-e: Displays Ethernet statistics, including the number of bytes and packets sent and received. 43 | c)-n: Shows active connections and their associated IP addresses and port numbers. The ‘-n’ option causes ‘netstat’ to display addresses and port numbers in numerical form, rather than resolving them to hostnames and service names. 44 | d)-s: Displays a summary of all network statistics, including information on the number of segments received, errors, and more. 45 | 6)route: The ‘route’ command is used to manipulate the IP routing table in Windows.With the ‘route’ command, you can view the current routing table, add new routes, modify existing routes, and delete routes.The ‘route’ command is often used in advanced network configuration scenarios, such as setting up VPN connections, specifying custom routes for specific networks, or resolving connectivity issues. 46 | 7)hostname: The ‘hostname’ command is used to display or set the hostname of a computer in windows. When run without any options, the ‘hostname’ command will display the current hostname of the computer. 47 | 48 | ---------------------------------------------------------------------------------------------------------------------------------------------------------- 49 | 50 | 8)ARP: The arp command displays and modifies the Internet-to-adapter address translation tables used by the Address in Networks and communication management. The arp command displays the current ARP entry for the host specified by the HostName variable. 51 | a)-a: Displays the current ARP (Address Resolution Protocol) cache, which maps IP addresses to MAC addresses on the local network. 52 | b)-v: Displays the ARP cache in verbose mode, including additional information such as the type of ARP entries (dynamic or static) and the interface used for each entry. 53 | 54 | ---------------------------------------------------------------------------------------------------------------------- 55 | 56 | 9)curl: It is a command-line tool powered by the libcurl library to transfer data to and from the server using various protocols, such as HTTP, HTTPS, FTP, FTPS, IMAP, IMAPS, POP3, POP3S, SMTP, and SMTPS. It is highly popular for automation and scripts due to its wide range of features and protocol support. 57 | a)curl : Downloads the content of the specified URL and displays it in the console. 58 | b)curl -o: Downloads the content of the specified URL and saves it to a file with the specified name. 59 | 60 | ----------------------------------------------------------------------------------------------------------------------- 61 | 62 | 10)whois : It allows you to perform lookup of owner information of a website by querying databases that stores the registered users of a domain or IP address. 63 | a) whois -H : Retrieves information about the specified domain name, but suppresses the inclusion of headers and footers. 64 | 65 | ----------------------------------------------------------------------------------------------------------------------- 66 | 67 | 11)Dig: dig command stands for Domain Information Groper. It is used for retrieving information about DNS name servers. It is basically used by network administrators. It is used for verifying and troubleshooting DNS problems and to perform DNS lookups. Dig command replaces older tools such as nslookup and the host. 68 | 69 | ------------------------------------------------------------------------------------------------------------------------- 70 | 71 | 12)mtr: The mtr command is a combination of ping and traceroute commands. It is a network diagnostic tool that continuously sends packets showing ping time for each hop. It also displays network problems of the entire route taken by the network packets. 72 | 73 | --------------------------------------------------------------------------------------------------------------------------- 74 | 75 | 13)tcpdump: tcpdump is a packet sniffing and packet analyzing tool for a System Administrator to troubleshoot connectivity issues in Linux. It is used to capture, filter, and analyze network traffic such as TCP/IP packets going through your system. It is many times used as a security tool as well. It saves the captured information in a pcap file, these pcap files can then be opened through Wireshark or through the command tool itself. 76 | 77 | --------------------------------------------------------------------------------------------------------------------------------------------------- 78 | 79 | 14)ss: A replacement for the deprecated netstat command, ss gives you detailed information about how your computer communicates with other computers, networks, and services. ss displays statistics for Transmission Control Protocol (TCP), User Datagram Protocol (UDP), Unix (interprocess), and raw sockets. Raw sockets operate at the network OSI level, which means TCP and UDP headers have to be handled by the application software, not by the transport layer. Internet Control Message Protocol (ICMP) messages and the ping utility both use raw sockets. 80 | 81 | --------------------------------------------------------------------------------------------------------------------------------------------------- 82 | 83 | 15) Wget: Wget is the non-interactive network downloader which is used to download files from the server even when the user has not logged on to the system and it can work in the background without hindering the current process. 84 | 85 | --------------------------------------------------------------------------------------------------------------------------------------------------- 86 | 87 | 16) route: Command in Linux is used when you want to work with the IP/kernel routing table. It is mainly used to set up static routes to specific hosts or networks via an interface. It is used for showing or update the IP/kernel routing table. 88 | 89 | --------------------------------------------------------------------------------------------------------------------------------------------------- 90 | 91 | 17) Host: The host command also displays any aliases associatd with the HostName parameter. If the local host is using the DOMAIN protocol, the local or remote name server database is queried before searching the local /etc/hosts file. The host command may also return other name records found in the DNS (Domain Name System). 92 | 93 | --------------------------------------------------------------------------------------------------------------------------------------------------- 94 | 95 | 18) ifconfig: veryh much similar to ipconfig 96 | 97 | 98 | -------------------------------------------------------------------------------- /Array/TCPClientArray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.Scanner; 4 | 5 | public class TCPClientArray { 6 | public static void main(String[] args) { 7 | try (Socket clientSocket = new Socket("localhost", 9876)) { 8 | // Ask the user to input an array 9 | Scanner sc = new Scanner(System.in); 10 | System.out.print("Enter an array of integers separated by spaces: "); 11 | String arrayString = sc.nextLine(); 12 | String[] arrayStrings = arrayString.split(" "); 13 | int[] array = new int[arrayStrings.length]; 14 | for (int i = 0; i < arrayStrings.length; i++) { 15 | array[i] = Integer.parseInt(arrayStrings[i]); 16 | } 17 | 18 | // Serialize the array 19 | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 20 | ObjectOutputStream objectOut = new ObjectOutputStream(byteOut); 21 | objectOut.writeObject(array); 22 | byte[] data = byteOut.toByteArray(); 23 | 24 | // Send the serialized array to the server 25 | OutputStream outToServer = clientSocket.getOutputStream(); 26 | outToServer.write(data); 27 | System.out.println("Sent array to server"); 28 | 29 | // Receive the maximum number from the server 30 | InputStream inFromServer = clientSocket.getInputStream(); 31 | byte[] receiveData = new byte[1024]; 32 | inFromServer.read(receiveData); 33 | int max = Integer.parseInt(new String(receiveData).trim()); 34 | System.out.println("Received max number from server: " + max); 35 | 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Array/TCPServerArray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class TCPServerArray { 5 | public static void main(String[] args) { 6 | try (ServerSocket serverSocket = new ServerSocket(9876)) { 7 | System.out.println("Server started. Listening for connections..."); 8 | 9 | while (true) { 10 | // Accept the client connection 11 | Socket clientSocket = serverSocket.accept(); 12 | System.out.println("Connection accepted from client"); 13 | 14 | // Receive the serialized array from the client 15 | InputStream inFromClient = clientSocket.getInputStream(); 16 | byte[] receiveData = new byte[1024]; 17 | inFromClient.read(receiveData); 18 | System.out.println("Received array from client"); 19 | 20 | // Deserialize the array 21 | ObjectInputStream objectIn = new ObjectInputStream(new ByteArrayInputStream(receiveData)); 22 | int[] array = (int[]) objectIn.readObject(); 23 | System.out.print("Received array: ["); 24 | for (int i = 0; i < array.length; i++) { 25 | System.out.print(array[i]); 26 | if (i != array.length - 1) { 27 | System.out.print(", "); 28 | } 29 | } 30 | System.out.println("]"); 31 | 32 | // Find the maximum number in the array 33 | int max = array[0]; 34 | for (int j = 0; j < array.length; j++) { 35 | if (array[j] > max) { 36 | max = array[j]; 37 | } 38 | } 39 | System.out.println("Max number in array: " + max); 40 | 41 | // Send the maximum number back to the client 42 | OutputStream outToClient = clientSocket.getOutputStream(); 43 | outToClient.write(Integer.toString(max).getBytes()); 44 | System.out.println("Sent max number to client"); 45 | 46 | clientSocket.close(); 47 | System.out.println("Connection closed"); 48 | } 49 | } catch (IOException | ClassNotFoundException e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Array/UDPClientArray.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | import java.util.Scanner; 4 | 5 | public class UDPClientArray { 6 | public static void main(String args[]) { 7 | try { 8 | DatagramSocket clientSocket = new DatagramSocket(); 9 | 10 | // ask the user to input an array 11 | Scanner sc = new Scanner(System.in); 12 | System.out.print("Enter an array of integers separated by spaces: "); 13 | String arrayString = sc.nextLine(); 14 | String[] arrayStrings = arrayString.split(" "); 15 | int[] array = new int[arrayStrings.length]; 16 | for (int i = 0; i < arrayStrings.length; i++) { 17 | array[i] = Integer.parseInt(arrayStrings[i]); 18 | } 19 | 20 | // serialize the array 21 | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 22 | ObjectOutputStream out = new ObjectOutputStream(byteOut); 23 | out.writeObject(array); 24 | byte[] data = byteOut.toByteArray(); 25 | 26 | // send the serialized array to the server 27 | InetAddress IPAddress = InetAddress.getByName("localhost"); 28 | int port = 9876; 29 | DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddress, port); 30 | clientSocket.send(sendPacket); 31 | System.out.println("Sent array to server"); 32 | 33 | 34 | 35 | // receive the maximum number from the server 36 | byte[] receiveData = new byte[1024]; 37 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 38 | clientSocket.receive(receivePacket); 39 | String maxString = new String(receivePacket.getData(), 0, receivePacket.getLength()); 40 | int max = Integer.parseInt(maxString); 41 | System.out.println("Received max number from server: " + max); 42 | 43 | clientSocket.close(); 44 | } catch (IOException e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Array/UDPServerArray.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class UDPServerArray { 5 | public static void main(String args[]) { 6 | try { 7 | DatagramSocket serverSocket = new DatagramSocket(9876); 8 | System.out.println("Server started. Listening for connections..."); 9 | 10 | while (true) { 11 | // receive the serialized array from the client 12 | byte[] receiveData = new byte[1024]; 13 | byte[] sendData = new byte[1024]; 14 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 15 | serverSocket.receive(receivePacket); 16 | System.out.println("Received array from client"); 17 | 18 | // deserialize the array 19 | ByteArrayInputStream byteIn = new ByteArrayInputStream(receiveData); 20 | ObjectInputStream in = new ObjectInputStream(byteIn); 21 | int[] array = (int[]) in.readObject(); 22 | System.out.print("Received array: ["); 23 | for (int i = 0; i < array.length; i++) { 24 | System.out.print(array[i]); 25 | if (i != array.length - 1) { 26 | System.out.print(", "); 27 | } 28 | } 29 | System.out.println("]"); 30 | 31 | int max = array[0]; 32 | for(int j=0;jmax){ 34 | max = array[j]; 35 | } 36 | 37 | } 38 | System.out.println(max); 39 | String maxi = Integer.toString(max); 40 | sendData = (maxi).getBytes(); 41 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receivePacket.getAddress(), receivePacket.getPort()); 42 | serverSocket.send(sendPacket); 43 | 44 | 45 | System.out.println("Connection closed"); 46 | } 47 | } catch (IOException | ClassNotFoundException e) { 48 | e.printStackTrace(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Lab Assignment 5 TCPdump.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deepprajapati25/Network-Lab/36cd4bb730b8eb2e77e25ba040f72fc6eca8ea8c/Lab Assignment 5 TCPdump.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Network-Lab 2 | \This Repo consists of all CN Lab Assignments 3 | 4 | \Even how to Read a tr file is shown 5 | 6 | \wireshark Tcp and Udp packets analysis is not included 7 | 8 | \Casestudy assignment is not included 9 | -------------------------------------------------------------------------------- /TCP GENERAL/TCPClient.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | import java.util.Scanner; 4 | 5 | public class TCPClient { 6 | public static void main(String args[]) { 7 | try { 8 | Socket clientSocket = new Socket("localhost", 9876); 9 | 10 | // ask the user to input a string and an integer to send to the server 11 | Scanner sc = new Scanner(System.in); 12 | System.out.print("Enter a string: "); 13 | String clientString = sc.nextLine(); 14 | System.out.print("Enter an integer: "); 15 | int clientInt = sc.nextInt(); 16 | sc.nextLine(); // consume the newline character 17 | 18 | // send the string and integer to the server 19 | OutputStream outToServer = clientSocket.getOutputStream(); 20 | DataOutputStream out = new DataOutputStream(outToServer); 21 | out.writeBytes(clientString + "\n"); 22 | out.writeBytes(clientInt + "\n"); 23 | 24 | // receive an integer and a string from the server 25 | BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 26 | int serverInt = Integer.parseInt(inFromServer.readLine()); 27 | String serverString = inFromServer.readLine(); 28 | System.out.println("From server: " + serverInt + " " + serverString); 29 | 30 | clientSocket.close(); 31 | } catch (IOException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /TCP GENERAL/TCPServer.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class TCPServer { 5 | public static void main(String args[]) { 6 | try { 7 | ServerSocket serverSocket = new ServerSocket(9876); 8 | System.out.println("Server started. Listening for connections..."); 9 | 10 | while (true) { 11 | Socket clientSocket = serverSocket.accept(); 12 | System.out.println("Connection accepted"); 13 | 14 | // read the string and integer from the client 15 | BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 16 | String clientString = inFromClient.readLine(); 17 | int clientInt = Integer.parseInt(inFromClient.readLine()); 18 | System.out.println("Received from client: " + clientString + " " + clientInt); 19 | 20 | // send back an integer and a string to the client 21 | int serverInt = clientInt * 2; 22 | String serverString = "Hello " + clientString + ", the integer you sent multiplied by 2 is: " + serverInt; 23 | DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream()); 24 | outToClient.writeBytes(serverInt + "\n"); 25 | outToClient.writeBytes(serverString + "\n"); 26 | 27 | clientSocket.close(); 28 | System.out.println("Connection closed"); 29 | } 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TCPClient.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class TCPClient { 5 | public static void main(String args[]) throws Exception { 6 | // create input stream to read from user 7 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 8 | 9 | // create client socket and connect to server 10 | Socket clientSocket = new Socket("localhost", 6789); 11 | System.out.println("Connected to server"); 12 | 13 | // create output stream to send to server 14 | DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 15 | 16 | // create input stream to read from server 17 | BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 18 | 19 | // read number from user 20 | System.out.print("Enter a number: "); 21 | double number = Double.parseDouble(inFromUser.readLine()); 22 | 23 | // send number to server 24 | outToServer.writeBytes(Double.toString(number) + '\n'); 25 | 26 | // read result from server 27 | String serverInput = inFromServer.readLine(); 28 | double result = Double.parseDouble(serverInput); 29 | 30 | // display result 31 | System.out.println("Result: " + result); 32 | 33 | // close connection 34 | clientSocket.close(); 35 | System.out.println("Connection closed"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TCPClient_revint.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class TCPClient_revint { 5 | public static void main(String args[]) throws Exception { 6 | // create input stream to read from user 7 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 8 | 9 | // create client socket and connect to server 10 | Socket clientSocket = new Socket("localhost", 6789); 11 | System.out.println("Connected to server"); 12 | 13 | // create output stream to send to server 14 | DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 15 | 16 | // create input stream to read from server 17 | BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 18 | 19 | // read number from user 20 | System.out.print("Enter a number: "); 21 | double number = Double.parseDouble(inFromUser.readLine()); 22 | 23 | // send number to server 24 | outToServer.writeBytes(Double.toString(number) + '\n'); 25 | 26 | // read result from server 27 | String serverInput = inFromServer.readLine(); 28 | int reversedNumber = Integer.parseInt(serverInput); 29 | 30 | // display result 31 | System.out.println("Result: " + reversedNumber); 32 | 33 | // close connection 34 | clientSocket.close(); 35 | System.out.println("Connection closed"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TCPEvenOddClient.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class TCPEvenOddClient { 5 | public static void main(String args[]) throws Exception { 6 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 7 | Socket clientSocket = new Socket("localhost", 9855); 8 | DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 9 | BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 10 | System.out.println("Enter a number to check if it is even or not"); 11 | String sentence = inFromUser.readLine(); 12 | outToServer.writeBytes(sentence + "\n"); 13 | String response = inFromServer.readLine(); 14 | System.out.println("From server: " + response); 15 | clientSocket.close(); 16 | System.out.println("connection terminated"); 17 | } 18 | } -------------------------------------------------------------------------------- /TCPEvenOddServer.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class TCPEvenOddServer { 5 | public static void main(String args[]) throws Exception { 6 | try (ServerSocket welcomeSocket = new ServerSocket(9855)) { 7 | System.out.println("Connected to server"); 8 | String sentence; 9 | while (true) { 10 | Socket connectionSocket = welcomeSocket.accept(); 11 | BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 12 | System.out.println(connectionSocket.getInputStream()); 13 | sentence = inFromClient.readLine(); 14 | System.out.println(sentence); 15 | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 16 | String response = evenOrOdd(sentence); 17 | outToClient.write(response.getBytes()); 18 | connectionSocket.close(); 19 | } 20 | } 21 | } 22 | 23 | public static String evenOrOdd(String sentence) { 24 | int num = Integer.parseInt(sentence); 25 | String numStr; 26 | if(num %2 == 0){ 27 | numStr = "Even number"; 28 | } 29 | 30 | else{ 31 | numStr = "Odd number"; 32 | } 33 | 34 | return numStr; 35 | } 36 | } -------------------------------------------------------------------------------- /TCPServer.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.lang.Math.*; 4 | public class TCPServer { 5 | public static void main(String args[]) throws Exception { 6 | // create server socket 7 | ServerSocket welcomeSocket = new ServerSocket(6789); 8 | System.out.println("Server started"); 9 | 10 | while (true) { 11 | // wait for client connection 12 | Socket connectionSocket = welcomeSocket.accept(); 13 | System.out.println("Client connected"); 14 | 15 | // create input stream to read from client 16 | BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 17 | 18 | // create output stream to send result to client 19 | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 20 | 21 | // read number from client 22 | String clientInput = inFromClient.readLine(); 23 | double number = Double.parseDouble(clientInput); 24 | 25 | // calculate square root 26 | double result = Math.sqrt(number); 27 | 28 | // send result to client 29 | outToClient.writeBytes(Double.toString(result) + '\n'); 30 | System.out.println("Result sent: " + result); 31 | 32 | // close connection 33 | connectionSocket.close(); 34 | System.out.println("Connection closed"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TCPServer_revint.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class TCPServer_revint { 5 | public static void main(String args[]) throws Exception { 6 | // create server socket 7 | ServerSocket welcomeSocket = new ServerSocket(6789); 8 | System.out.println("Server started"); 9 | 10 | while (true) { 11 | // wait for client connection 12 | Socket connectionSocket = welcomeSocket.accept(); 13 | System.out.println("Client connected"); 14 | 15 | // create input stream to read from client 16 | BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 17 | 18 | // create output stream to send result to client 19 | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 20 | 21 | // read number from client 22 | String clientInput = inFromClient.readLine(); 23 | double number = Double.parseDouble(clientInput); 24 | int integerNumber = (int) number; 25 | 26 | // calculate reverse 27 | int reversedNumber = 0; 28 | while (integerNumber != 0) { 29 | int digit = integerNumber % 10; 30 | reversedNumber = reversedNumber * 10 + digit; 31 | integerNumber /= 10; 32 | } 33 | 34 | // send result to client 35 | outToClient.writeBytes(Integer.toString(reversedNumber) + '\n'); 36 | System.out.println("Result sent: " + reversedNumber); 37 | 38 | // close connection 39 | connectionSocket.close(); 40 | System.out.println("Connection closed"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UDP GENERAL/UDPClient.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | 3 | public class UDPClient { 4 | public static void main(String[] args) { 5 | try { 6 | DatagramSocket clientSocket = new DatagramSocket(); 7 | InetAddress IPAddress = InetAddress.getByName("localhost"); 8 | 9 | // ask the user to input a string and an integer to send to the server 10 | System.out.print("Enter a string: "); 11 | byte[] sendData = new byte[1024]; 12 | String clientString = System.console().readLine(); 13 | System.out.print("Enter an integer: "); 14 | int clientInt = Integer.parseInt(System.console().readLine()); 15 | String clientData = clientString + " " + clientInt; 16 | sendData = clientData.getBytes(); 17 | 18 | // send the string and integer to the server 19 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); 20 | clientSocket.send(sendPacket); 21 | 22 | // receive an integer and a string from the server 23 | byte[] receiveData = new byte[1024]; 24 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 25 | clientSocket.receive(receivePacket); 26 | String serverString = new String(receivePacket.getData(), 0, receivePacket.getLength()); 27 | String[] tokens = serverString.split("\\n"); 28 | int serverInt = Integer.parseInt(tokens[0]); 29 | serverString = tokens[1]; 30 | System.out.println("From server: " + serverInt + " " + serverString); 31 | 32 | clientSocket.close(); 33 | } catch (Exception e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /UDP GENERAL/UDPServer.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | 3 | public class UDPServer { 4 | public static void main(String[] args) { 5 | try { 6 | DatagramSocket serverSocket = new DatagramSocket(9876); 7 | byte[] receiveData = new byte[1024]; 8 | byte[] sendData = new byte[1024]; 9 | System.out.println("Server started. Listening for connections..."); 10 | 11 | while (true) { 12 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 13 | serverSocket.receive(receivePacket); 14 | System.out.println("Connection accepted"); 15 | 16 | // read the string and integer from the client 17 | String clientString = new String(receivePacket.getData(), 0, receivePacket.getLength()); 18 | int clientInt = Integer.parseInt(clientString.replaceAll("[^0-9]+", "")); // extract only digits 19 | System.out.println("Received from client: " + clientString + " " + clientInt); 20 | 21 | // send back an integer and a string to the client 22 | int serverInt = clientInt * 2; 23 | String serverString = "Hello " + clientString + ", the integer you sent multiplied by 2 is: " + serverInt; 24 | InetAddress IPAddress = receivePacket.getAddress(); 25 | int port = receivePacket.getPort(); 26 | sendData = (serverInt + "\n" + serverString).getBytes(); 27 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 28 | serverSocket.send(sendPacket); 29 | 30 | System.out.println("Connection closed"); 31 | } 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /UDPCLient_revint.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | class UDPClient_revint { 4 | public static void main(String args[]) throws Exception { 5 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 6 | DatagramSocket clientSocket = new DatagramSocket(); 7 | InetAddress IPAddress = InetAddress.getByName("localhost"); 8 | byte[] sendData = new byte[1024]; 9 | byte[] receiveData = new byte[1024]; 10 | System.out.print("Enter a number: "); 11 | String sentence = inFromUser.readLine(); 12 | 13 | sendData = sentence.getBytes(); 14 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 2525); 15 | clientSocket.send(sendPacket); 16 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 17 | clientSocket.receive(receivePacket); 18 | String modifiedSentence = new String(receivePacket.getData()); 19 | System.out.println("From Server: " + modifiedSentence); 20 | clientSocket.close(); 21 | } 22 | } -------------------------------------------------------------------------------- /UDPClient_vowels.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class UDPClient_vowels { 5 | public static void main(String args[]) throws Exception { 6 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 7 | DatagramSocket clientSocket = new DatagramSocket(); 8 | InetAddress IPAddress = InetAddress.getByName("localhost"); 9 | byte[] sendData = new byte[1024]; 10 | byte[] receiveData = new byte[1024]; 11 | System.out.println("Enter a String to Count Vowels"); 12 | String sentence = inFromUser.readLine(); 13 | sendData = sentence.getBytes(); 14 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); 15 | clientSocket.send(sendPacket); 16 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 17 | clientSocket.receive(receivePacket); 18 | String response = new String(receivePacket.getData()); 19 | System.out.println("From server: " + response); 20 | clientSocket.close(); 21 | System.out.println("connection terminated"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /UDPClientvow.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | import java.util.Scanner; 4 | 5 | public class UDPClientvow { 6 | public static void main(String args[]) { 7 | DatagramSocket clientSocket = null; 8 | try { 9 | clientSocket = new DatagramSocket(); 10 | 11 | Scanner sc = new Scanner(System.in); 12 | System.out.print("Enter a sentence: "); 13 | String sentence = sc.nextLine(); 14 | 15 | byte[] sendData = sentence.getBytes(); 16 | 17 | InetAddress IPAddress = InetAddress.getByName("localhost"); 18 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); 19 | clientSocket.send(sendPacket); 20 | 21 | byte[] receiveData = new byte[1024]; 22 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 23 | clientSocket.receive(receivePacket); 24 | 25 | String vowels = new String(receivePacket.getData(), 0, receivePacket.getLength()); 26 | System.out.println("Extracted vowels from server: " + vowels); 27 | } catch (IOException e) { 28 | e.printStackTrace(); 29 | } finally { 30 | if (clientSocket != null) { 31 | clientSocket.close(); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /UDPServer_revint.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | class UDPServer_revint { 5 | public static void main(String args[]) throws Exception { 6 | DatagramSocket serverSocket = new DatagramSocket(2525); 7 | byte[] receiveData = new byte[1024]; 8 | byte[] sendData = new byte[1024]; 9 | System.out.println("Client connected"); 10 | while (true) { 11 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 12 | serverSocket.receive(receivePacket); 13 | 14 | String sentence = new String(receivePacket.getData()); 15 | System.out.println("Received " + sentence); 16 | InetAddress IPAddress = receivePacket.getAddress(); 17 | int port = receivePacket.getPort(); 18 | double num = Double.parseDouble(sentence); 19 | int number = (int) num; 20 | int reversedNumber = 0; 21 | while (number != 0) { 22 | int digit = number % 10; 23 | reversedNumber = reversedNumber * 10 + digit; 24 | number /= 10; 25 | } 26 | sendData = Integer.toString(reversedNumber).getBytes(); 27 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 28 | serverSocket.send(sendPacket); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /UDPServer_vowels.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class UDPServer_vowels { 5 | public static void main(String args[]) throws Exception { 6 | DatagramSocket serverSocket = new DatagramSocket(9876); 7 | byte[] receiveData = new byte[1024]; 8 | byte[] sendData = new byte[1024]; 9 | System.out.println("Connected to server"); 10 | while (true) { 11 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 12 | serverSocket.receive(receivePacket); 13 | String sentence = new String(receivePacket.getData()); 14 | InetAddress IPAddress = receivePacket.getAddress(); 15 | int port = receivePacket.getPort(); 16 | int vowelCount = countVowels(sentence); 17 | String response = "Number of vowels: " + vowelCount; 18 | sendData = response.getBytes(); 19 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 20 | serverSocket.send(sendPacket); 21 | } 22 | } 23 | 24 | public static int countVowels(String sentence) { 25 | int count = 0; 26 | String vowels = "aeiouAEIOU"; 27 | for (int i = 0; i < sentence.length(); i++) { 28 | if (vowels.indexOf(sentence.charAt(i)) != -1) { 29 | count++; 30 | } 31 | } 32 | return count; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /UDPServervow.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class UDPServervow { 5 | public static void main(String args[]) { 6 | DatagramSocket serverSocket = null; 7 | try { 8 | serverSocket = new DatagramSocket(9876); 9 | byte[] receiveData = new byte[1024]; 10 | 11 | while (true) { 12 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 13 | serverSocket.receive(receivePacket); 14 | 15 | String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength()); 16 | System.out.println("Received sentence from client: " + sentence); 17 | 18 | String vowels = extractVowels(sentence); 19 | System.out.println("Extracted vowels: " + vowels); 20 | 21 | InetAddress IPAddress = receivePacket.getAddress(); 22 | int port = receivePacket.getPort(); 23 | byte[] sendData = vowels.getBytes(); 24 | 25 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 26 | serverSocket.send(sendPacket); 27 | } 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } finally { 31 | if (serverSocket != null) { 32 | serverSocket.close(); 33 | } 34 | } 35 | } 36 | 37 | private static String extractVowels(String sentence) { 38 | StringBuilder vowels = new StringBuilder(); 39 | for (int i = 0; i < sentence.length(); i++) { 40 | char c = sentence.charAt(i); 41 | if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { 42 | vowels.append(c); 43 | } 44 | } 45 | return vowels.toString(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /link.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | set nr [open out.tr w] 3 | $ns trace-all $nr 4 | set nf [open out.nam w] 5 | $ns namtrace-all $nf 6 | 7 | proc finish {} { 8 | global ns nr nf 9 | $ns flush-trace 10 | close $nf 11 | close $nr 12 | exec nam out.nam & 13 | exit 0 14 | } 15 | 16 | for {set i 0} {$i < 12} {incr i} { 17 | set n($i) [$ns node] 18 | } 19 | 20 | for {set i 0} {$i < 8} {incr i} { 21 | $ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail 22 | } 23 | $ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail 24 | $ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail 25 | $ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail 26 | $ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail 27 | $ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail 28 | $ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail 29 | 30 | set udp0 [new Agent/UDP] 31 | $ns attach-agent $n(0) $udp0 32 | set cbr0 [new Application/Traffic/CBR] 33 | $cbr0 set packetSize_ 500 34 | $cbr0 set interval_ 0.005 35 | $cbr0 attach-agent $udp0 36 | set null0 [new Agent/Null] 37 | $ns attach-agent $n(5) $null0 38 | $ns connect $udp0 $null0 39 | 40 | set udp1 [new Agent/UDP] 41 | $ns attach-agent $n(1) $udp1 42 | set cbr1 [new Application/Traffic/CBR] 43 | $cbr1 set packetSize_ 500 44 | $cbr1 set interval_ 0.005 45 | $cbr1 attach-agent $udp1 46 | set null1 [new Agent/Null] 47 | $ns attach-agent $n(6) $null1 48 | $ns connect $udp1 $null1 49 | 50 | $ns rtproto LS 51 | $ns rtmodel-at 10.0 down $n(11) $n(5) 52 | $ns rtmodel-at 15.0 down $n(7) $n(6) 53 | $ns rtmodel-at 30.0 up $n(11) $n(5) 54 | $ns rtmodel-at 20.0 up $n(7) $n(6) 55 | 56 | $udp0 set fid_ 1 57 | $udp1 set fid_ 2 58 | 59 | $ns color 1 Red 60 | $ns color 2 Green 61 | 62 | $ns at 1.0 "$cbr0 start" 63 | $ns at 2.0 "$cbr1 start" 64 | 65 | $ns at 45 "finish" 66 | $ns run 67 | 68 | -------------------------------------------------------------------------------- /routing.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | set nr [open out.tr w] 3 | $ns trace-all $nr 4 | set nf [open out.nam w] 5 | 6 | $ns namtrace-all $nf 7 | proc finish { } { 8 | global ns nr nf 9 | $ns flush-trace 10 | close $nf 11 | close $nr 12 | exec nam out.nam & 13 | exit 0 14 | } 15 | 16 | for { set i 0 } { $i < 12} { incr i 1 } { 17 | set n($i) [$ns node]} 18 | 19 | for {set i 0} {$i < 8} {incr i} { 20 | $ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail } 21 | $ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail 22 | $ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail 23 | $ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail 24 | $ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail 25 | $ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail 26 | $ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail 27 | 28 | set udp0 [new Agent/UDP] 29 | $ns attach-agent $n(0) $udp0 30 | set cbr0 [new Application/Traffic/CBR] 31 | $cbr0 set packetSize_ 500 32 | $cbr0 set interval_ 0.005 33 | $cbr0 attach-agent $udp0 34 | set null0 [new Agent/Null] 35 | $ns attach-agent $n(5) $null0 36 | $ns connect $udp0 $null0 37 | 38 | set udp1 [new Agent/UDP] 39 | $ns attach-agent $n(1) $udp1 40 | set cbr1 [new Application/Traffic/CBR] 41 | $cbr1 set packetSize_ 500 42 | $cbr1 set interval_ 0.005 43 | $cbr1 attach-agent $udp1 44 | set null0 [new Agent/Null] 45 | $ns attach-agent $n(5) $null0 46 | $ns connect $udp1 $null0 47 | 48 | $ns rtproto DV 49 | 50 | $ns rtmodel-at 10.0 down $n(11) $n(5) 51 | $ns rtmodel-at 15.0 down $n(7) $n(6) 52 | $ns rtmodel-at 30.0 up $n(11) $n(5) 53 | $ns rtmodel-at 20.0 up $n(7) $n(6) 54 | 55 | $udp0 set fid_ 1 56 | $udp1 set fid_ 2 57 | 58 | $ns color 1 Red 59 | $ns color 2 Green 60 | 61 | $ns at 1.0 "$cbr0 start" 62 | $ns at 2.0 "$cbr1 start" 63 | 64 | $ns at 45 "finish" 65 | $ns run -------------------------------------------------------------------------------- /tcp client mam.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | class TCPClient { 5 | 6 | public static void main(String argv[]) throws Exception { 7 | 8 | String sentence; 9 | String modifiedSentence; 10 | 11 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 12 | Socket clientSocket = new Socket("localhost", 6789); 13 | 14 | DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 15 | BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 16 | 17 | sentence = inFromUser.readLine(); 18 | outToServer.writeBytes(sentence + '\n'); 19 | 20 | modifiedSentence = inFromServer.readLine(); 21 | System.out.println("FROM SERVER: " + modifiedSentence); 22 | 23 | clientSocket.close(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tcp only.tcl: -------------------------------------------------------------------------------- 1 | #Create a simulator object 2 | set ns [new Simulator] 3 | 4 | #Define different colors for data flows (for NAM) 5 | $ns color 1 Red 6 | 7 | 8 | #Open the NAM trace file 9 | set nf [open tcpppp83.nam w] 10 | $ns namtrace-all $nf 11 | 12 | set np [open tcpppp83.tr w] 13 | $ns trace-all $np 14 | 15 | #Define a 'finish' procedure 16 | proc finish {} { 17 | global ns nf np 18 | $ns flush-trace 19 | #Close the NAM trace file 20 | close $nf 21 | #Execute NAM on the trace file 22 | exec nam tcpppp83.nam & 23 | exit 0 24 | } 25 | 26 | #Create four nodes 27 | set n0 [$ns node] 28 | 29 | set n2 [$ns node] 30 | set n3 [$ns node] 31 | 32 | #Create links between the nodes 33 | $ns duplex-link $n0 $n2 2Mb 10ms DropTail 34 | 35 | $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail 36 | 37 | #Set Queue Size of link (n2-n3) to 10 38 | $ns queue-limit $n2 $n3 10 39 | 40 | #Give node position (for NAM) 41 | $ns duplex-link-op $n0 $n2 orient right-up 42 | 43 | $ns duplex-link-op $n2 $n3 orient right 44 | 45 | #Monitor the queue for link (n2-n3). (for NAM) 46 | $ns duplex-link-op $n2 $n3 queuePos 0.5 47 | 48 | 49 | #Setup a TCP connection 50 | set tcp [new Agent/TCP] 51 | $tcp set class_ 2 52 | $ns attach-agent $n0 $tcp 53 | set sink [new Agent/TCPSink] 54 | $ns attach-agent $n3 $sink 55 | $ns connect $tcp $sink 56 | $tcp set fid_ 1 57 | 58 | #Setup a FTP over TCP connection 59 | set ftp [new Application/FTP] 60 | $ftp attach-agent $tcp 61 | 62 | # setting packet size 63 | $ftp set packet_size_ 1000 64 | 65 | #setting bit rate 66 | $ftp set rate_ 1mb 67 | 68 | # setting random false means no noise 69 | $ftp set random_ false 70 | 71 | 72 | #Schedule events for the CBR and FTP agents 73 | $ns at 0.1 "$ftp start" 74 | $ns at 4.5 "$ftp stop" 75 | 76 | #Detach tcp and sink agents (not really necessary) 77 | $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" 78 | 79 | #Call the finish procedure after 5 seconds of simulation time 80 | $ns at 5.0 "finish" 81 | 82 | #Print CBR packet size and interval 83 | puts "FTP packet size = [$ftp set packet_size_]" 84 | 85 | 86 | #Run the simulation 87 | $ns run 88 | 89 | -------------------------------------------------------------------------------- /tcp server mam.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | class TCPServer { 5 | 6 | public static void main(String argv[]) throws Exception { 7 | 8 | String clientSentence; 9 | String capitalizedSentence; 10 | 11 | ServerSocket welcomeSocket = new ServerSocket(6789); 12 | 13 | while (true) { 14 | Socket connectionSocket = welcomeSocket.accept(); 15 | BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 16 | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 17 | 18 | clientSentence = inFromClient.readLine(); 19 | 20 | System.out.println("Received: " + clientSentence); 21 | 22 | capitalizedSentence = clientSentence.toUpperCase() + '\n'; 23 | 24 | outToClient.writeBytes(capitalizedSentence); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tr file read.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deepprajapati25/Network-Lab/36cd4bb730b8eb2e77e25ba040f72fc6eca8ea8c/tr file read.jpg -------------------------------------------------------------------------------- /udp client mam.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | class UDPClient { 5 | 6 | public static void main(String args[]) throws Exception { 7 | 8 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 9 | 10 | DatagramSocket clientSocket = new DatagramSocket(); 11 | InetAddress IPAddress = InetAddress.getByName("localhost"); 12 | 13 | byte[] sendData = new byte[1024]; 14 | byte[] receiveData = new byte[1024]; 15 | 16 | String sentence = inFromUser.readLine(); 17 | sendData = sentence.getBytes(); 18 | 19 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); 20 | clientSocket.send(sendPacket); 21 | 22 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 23 | clientSocket.receive(receivePacket); 24 | 25 | String modifiedSentence = new String(receivePacket.getData()); 26 | System.out.println("FROM SERVER: " + modifiedSentence); 27 | 28 | clientSocket.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /udp only.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | 3 | #Define different colors for data flows (for NAM) 4 | 5 | $ns color 2 Red 6 | 7 | #Open the NAM trace file 8 | set nf [open udpp83.nam w] 9 | $ns namtrace-all $nf 10 | 11 | set np [open udpp83.tr w] 12 | $ns trace-all $np 13 | 14 | #Define a 'finish' procedure 15 | proc finish {} { 16 | global ns nf np 17 | $ns flush-trace 18 | #Close the NAM trace file 19 | close $nf 20 | #Execute NAM on the trace file 21 | exec nam udpp83.nam & 22 | exit 0 23 | } 24 | 25 | #Create four nodes 26 | 27 | set n1 [$ns node] 28 | set n2 [$ns node] 29 | set n3 [$ns node] 30 | 31 | #Create links between the nodes 32 | 33 | $ns duplex-link $n1 $n2 2Mb 10ms DropTail 34 | $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail 35 | 36 | #Set Queue Size of link (n2-n3) to 10 37 | $ns queue-limit $n2 $n3 10 38 | 39 | #Give node position (for NAM) 40 | 41 | $ns duplex-link-op $n1 $n2 orient right-up 42 | $ns duplex-link-op $n2 $n3 orient right 43 | 44 | #Monitor the queue for link (n2-n3). (for NAM) 45 | $ns duplex-link-op $n2 $n3 queuePos 0.5 46 | 47 | 48 | 49 | #Setup a UDP connection 50 | set udp [new Agent/UDP] 51 | $ns attach-agent $n1 $udp 52 | 53 | set null [new Agent/Null] 54 | $ns attach-agent $n3 $null 55 | 56 | $ns connect $udp $null 57 | $udp set fid_ 2 58 | 59 | #Setup a CBR over UDP connection 60 | set cbr [new Application/Traffic/CBR] 61 | $cbr attach-agent $udp 62 | 63 | 64 | # setting packet size 65 | $cbr set packet_size_ 1000 66 | 67 | #setting bit rate 68 | $cbr set rate_ 1mb 69 | 70 | # setting random false means no noise 71 | $cbr set random_ false 72 | 73 | 74 | #Schedule events for the CBR and FTP agents 75 | $ns at 0.1 "$cbr start" 76 | $ns at 4.5 "$cbr stop" 77 | 78 | #Call the finish procedure after 5 seconds of simulation time 79 | $ns at 5.0 "finish" 80 | 81 | #Print CBR packet size and interval 82 | puts "CBR packet size = [$cbr set packet_size_]" 83 | puts "CBR interval = [$cbr set interval_]" 84 | 85 | #Run the simulation 86 | $ns run 87 | -------------------------------------------------------------------------------- /udp server mam.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | class UDPServer { 5 | 6 | public static void main(String args[]) throws Exception { 7 | 8 | DatagramSocket serverSocket = new DatagramSocket(9876); 9 | 10 | byte[] receiveData = new byte[1024]; 11 | byte[] sendData = new byte[1024]; 12 | 13 | while (true) { 14 | 15 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 16 | serverSocket.receive(receivePacket); 17 | 18 | String sentence = new String(receivePacket.getData()); 19 | System.out.println("RECEIVED: " + sentence); 20 | 21 | InetAddress IPAddress = receivePacket.getAddress(); 22 | int port = receivePacket.getPort(); 23 | 24 | String capitalizedSentence = sentence.toUpperCase(); 25 | sendData = capitalizedSentence.getBytes(); 26 | 27 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 28 | serverSocket.send(sendPacket); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /udp tcp mix.txt: -------------------------------------------------------------------------------- 1 | #Create a simulator object 2 | set ns [new Simulator] 3 | 4 | #Define different colors for data flows (for NAM) 5 | $ns color 1 Blue 6 | $ns color 2 Red 7 | 8 | #Open the NAM trace file 9 | set nf [open out.nam w] 10 | $ns namtrace-all $nf 11 | 12 | set np [open out.tr w] 13 | $ns trace-all $np 14 | 15 | #Define a 'finish' procedure 16 | proc finish {} { 17 | global ns nf np 18 | $ns flush-trace 19 | #Close the NAM trace file 20 | close $nf 21 | #Execute NAM on the trace file 22 | exec nam out.nam & 23 | exit 0 24 | } 25 | 26 | #Create four nodes 27 | set n0 [$ns node] 28 | set n1 [$ns node] 29 | set n2 [$ns node] 30 | set n3 [$ns node] 31 | 32 | #Create links between the nodes 33 | $ns duplex-link $n0 $n2 2Mb 10ms DropTail 34 | $ns duplex-link $n1 $n2 2Mb 10ms DropTail 35 | $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail 36 | 37 | #Set Queue Size of link (n2-n3) to 10 38 | $ns queue-limit $n2 $n3 10 39 | 40 | #Give node position (for NAM) 41 | $ns duplex-link-op $n0 $n2 orient right-down 42 | $ns duplex-link-op $n1 $n2 orient right-up 43 | $ns duplex-link-op $n2 $n3 orient right 44 | 45 | #Monitor the queue for link (n2-n3). (for NAM) 46 | $ns duplex-link-op $n2 $n3 queuePos 0.5 47 | 48 | 49 | #Setup a TCP connection 50 | set tcp [new Agent/TCP] 51 | $tcp set class_ 2 52 | $ns attach-agent $n0 $tcp 53 | set sink [new Agent/TCPSink] 54 | $ns attach-agent $n3 $sink 55 | $ns connect $tcp $sink 56 | $tcp set fid_ 1 57 | 58 | #Setup a FTP over TCP connection 59 | set ftp [new Application/FTP] 60 | $ftp attach-agent $tcp 61 | 62 | 63 | 64 | #Setup a UDP connection 65 | set udp [new Agent/UDP] 66 | $ns attach-agent $n1 $udp 67 | 68 | set null [new Agent/Null] 69 | $ns attach-agent $n3 $null 70 | 71 | $ns connect $udp $null 72 | $udp set fid_ 2 73 | 74 | #Setup a CBR over UDP connection 75 | set cbr [new Application/Traffic/CBR] 76 | $cbr attach-agent $udp 77 | 78 | 79 | # setting packet size 80 | $cbr set packet_size_ 1000 81 | 82 | #setting bit rate 83 | $cbr set rate_ 1mb 84 | 85 | # setting random false means no noise 86 | $cbr set random_ false 87 | 88 | 89 | #Schedule events for the CBR and FTP agents 90 | $ns at 0.1 "$cbr start" 91 | $ns at 1.0 "$ftp start" 92 | $ns at 4.0 "$ftp stop" 93 | $ns at 4.5 "$cbr stop" 94 | 95 | #Detach tcp and sink agents (not really necessary) 96 | $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" 97 | 98 | #Call the finish procedure after 5 seconds of simulation time 99 | $ns at 5.0 "finish" 100 | 101 | #Print CBR packet size and interval 102 | puts "CBR packet size = [$cbr set packet_size_]" 103 | puts "CBR interval = [$cbr set interval_]" 104 | 105 | #Run the simulation 106 | $ns run 107 | 108 | -------------------------------------------------------------------------------- /udpclientsqrt.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | class udpclientsqrt { 4 | public static void main(String args[]) throws Exception { 5 | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 6 | DatagramSocket clientSocket = new DatagramSocket(); 7 | InetAddress IPAddress = InetAddress.getByName("localhost"); 8 | byte[] sendData = new byte[1024]; 9 | byte[] receiveData = new byte[1024]; 10 | String sentence = inFromUser.readLine(); 11 | sendData = sentence.getBytes(); 12 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 2525); 13 | clientSocket.send(sendPacket); 14 | DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 15 | clientSocket.receive(receivePacket); 16 | String modifiedSentence = new String(receivePacket.getData()); 17 | System.out.println("From Server: " + modifiedSentence); 18 | clientSocket.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /udpserversqrt.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.lang.Math.*; 4 | class udpserversqrt 5 | { 6 | public static void main(String args[]) throws Exception 7 | { 8 | DatagramSocket serverSocket=new DatagramSocket(2525); 9 | byte[] receiveData=new byte[1024]; 10 | byte[] sendData=new byte[1024]; 11 | while(true) 12 | { 13 | DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length); 14 | serverSocket.receive(receivePacket); 15 | 16 | String sentence=new String(receivePacket.getData()); 17 | System.out.println("Received "+sentence); 18 | InetAddress IPAddress=receivePacket.getAddress(); 19 | int port=receivePacket.getPort(); 20 | double num=Double.parseDouble(sentence); 21 | double result=Math.sqrt(num); 22 | sendData=Double.toString(result).getBytes(); 23 | DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,port); 24 | serverSocket.send(sendPacket); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /varaitions.java: -------------------------------------------------------------------------------- 1 | #Palindrome in java function 2 | 3 | String reversed = ""; 4 | for (int i = input.length() - 1; i >= 0; i--) { 5 | reversed += input.charAt(i); 6 | } 7 | 8 | // Compare original string with reversed string 9 | if (input.equals(reversed)) { 10 | System.out.println("The input string is a palindrome."); 11 | } else { 12 | System.out.println("The input string is not a palindrome."); 13 | } 14 | 15 | ----------------------------------------------------------------------------- 16 | 17 | #Factorial in java 18 | int factorial = 1; 19 | 20 | for (int i = 1; i <= n; i++) { 21 | factorial *= i; 22 | } 23 | 24 | 25 | ----------------------------------------------------------------------------- 26 | #vowels extract 27 | private static String extractVowels(String sentence) { 28 | StringBuilder vowels = new StringBuilder(); 29 | for (int i = 0; i < sentence.length(); i++) { 30 | char c = sentence.charAt(i); 31 | if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { 32 | vowels.append(c); 33 | } 34 | } 35 | return vowels.toString(); 36 | } 37 | --------------------------------------------------------------------------------