EXPERIMENT 1 AIM: Study of Network devices in detail and connect the computers in Local Area Network. All but the most basic of networks require devices to provide connectivity and functionality. Understanding how these networking devices operate and identifying the functions they perform are essential skills for any network administrator and requirements for a Network+ candidate. The all-network devices are explained below: Hubs: The hub or network hub connects computers and devices and sends messages and data from any one device to all the others. If the desktop computer wants to send data to the lapop and it sends a message to the laptop through the hub, the message will get sent by the hub to all the computers and devices on the network. They need to do work to figure out that the message isnot for them. The message also uses up bandwidth (room) on the network wires or wireless radiowaves and limits how much communication can go on. Hubs are not used often these days. Switch: The switch connects the computer network components but it is smart about it. It knows the address of each item and so when the desktop computer wants to talk to the laptop, it only sends the message to the laptop and nothing else. In order to have a small home network that just connects the local equipment all that is really needed is a switch and network cable or the switch can transmit wireless information that is received by wireless receivers that each of the network devices have. Figure 1 Bridges: Bridges are used to divide larger networks into smaller sections. They do this by sitting between two physical network segments and managing the flow of data between the two. By looking at the MAC address of the devices connected to each segment, bridges can elect to forward the data (if they believe that the destination address is on another interface), or block it from crossing (if they can verify that it is on the interface from which it came). A bridge functions by blocking or forwarding data, based on the destination MAC address written into each frame of data. If the bridge believes the destination address is on a network other than that from which the data was received, it can forward the data to the other networks to which it is connected. If the address is not on the other side of the bridge, the data is blocked from passing. Bridges “learn” the MAC addresses of devices on connected networks by “listening” to network traffic and recording the network from which the traffic originates. Figure 3 shows a representation of a bridge. Figure 3 Figure 2 Routers: In a common configuration, routers are used to create larger networks by joining two network segments. A router derives its name from the fact that it can route data it receives from one network onto another. When a router receives a packet of data, it reads the header of the packet to determine the destination address. Once it has determined the address, it looks in its routing table to determine whether it knows how to reach the destination and, if it does, it forwards the packet to the next hop on the route. The next hop might be the final destination, or it might be another router. Figure 4 shows, in basic terms, how a router works. The routing tables play a very important role in the routing process. They are the means by which the router makes its decisions. For this reason, a routing table needs to be two things. It must be up-to-date, and it must be complete. There are two ways that the router can get the information for the routing table—through static routing or dynamic routing. Modem : Most everyone wants to connect to the internet. A broadband modem is used to take a high speed Internet connection provided by an ISP (Internet Service Provider) and convert the data into a form that your local network can use. The high speed connection can be DSL (Digital Subscriber Line) from a phone company or cable from a cable television provider. In order to be reached on the Internet, your computer needs a unique address on the internet. Your ISP will provide this to you as part of your Internet connection package. This address will generally not be fixed which means that they may change your address from time to time. For the vast majority of users, this makes Figure 4 no difference. If you have only one computer and want to connect to the Internet, you strictly speaking don't need a router. You can plug the network cable from the modem directly into the network connection of your computer. However, you are much better off connecting the modem to a router. The ip address your ISP provides will be assigned to the router. Figure 5 Aim: Algorithm to implement data link layer error detection method ‘checksum’ Program #include<stdio.h> #include<string.h> int main() { char a[20],b[20]; char sum[20],complement[20]; int i,length; printf("Enter first binary string\n"); scanf("%s",a); printf("Enter second binary string\n"); scanf("%s",b); if(strlen(a)==strlen(b)) { length=strlen(a); char carry='0'; for(i=length-1;i>=0;i--) { if(a[i]=='0'&&b[i]=='0'&&carry=='0') { sum[i]='0'; carry='0'; } else if(a[i]=='0'&&b[i]=='0'&&carry=='1') { sum[i]='1'; carry='0'; } else if(a[i]=='0'&&b[i]=='1'&&carry=='0') { sum[i]='1'; carry='0'; } else if(a[i]=='0'&&b[i]=='1'&&carry=='1') { sum[i]='0'; carry='1'; } else if(a[i]=='1'&&b[i]=='0'&&carry=='0') { sum[i]='1'; carry='0'; } else if(a[i]=='1'&&b[i]=='0'&&carry=='1') { sum[i]='0'; carry='1'; } else if(a[i]=='1'&&b[i]=='1'&&carry=='0') { sum[i]='0'; carry='1'; } else if(a[i]=='1'&&b[i]=='1'&&carry=='1') { sum[i]='1'; carry='1'; } else break; } printf("\n Carry=%c, Sum=%s",carry,sum); for(i=length-1;i>0;i--) { if(sum[i]=='1'&& carry=='1') { sum[i]='0'; carry='1'; } else if(sum[i]=='0'&& carry=='1') { sum[i]='1'; break; } else break; } for(i=0;i<length;i++) { if(sum[i]=='0') complement[i]='1'; else complement[i]='0'; } printf("\nChecksum=%s",complement); } else { printf("\n Wrong input Strings"); } } OUTPUT AIM: Write a program for Hamming Code generation for error detection and correction. PROGRAM: #include <stdio.h> int main() { int data[7], received[7], i; int p1, p2, p4, syndrome; printf("Hamming(7,4) code using EVEN parity at positions 1, 2, and 4.\n"); printf("Enter 4 data bits one by one (D1, D2, D3, D4):\n"); // Insert data bits into correct positions: // Positions: [1] P1 [2] P2 [3] D1 [4] P4 [5] D2 [6] D3 [7] D4 scanf("%d", &data[2]); // D1 at position 3 scanf("%d", &data[4]); // D2 at position 5 scanf("%d", &data[5]); // D3 at position 6 scanf("%d", &data[6]); // D4 at position 7 // Compute parity bits for even parity // P1 covers bits 1,3,5,7 → positions: 0,2,4,6 data[0] = data[2] ^ data[4] ^ data[6]; // P2 covers bits 2,3,6,7 → positions: 1,2,5,6 data[1] = data[2] ^ data[5] ^ data[6]; // P4 covers bits 4,5,6,7 → positions: 3,4,5,6 data[3] = data[4] ^ data[5] ^ data[6]; printf("\nEncoded 7-bit Hamming code:\n"); for (i = 0; i < 7; i++) { printf("%d ", data[i]); } // Input received message (can be erroneous) printf("\n\nEnter received 7-bit message one by one:\n"); for (i = 0; i < 7; i++) { scanf("%d", &received[i]); } // Recalculate parity checks for even parity p1 = received[0] ^ received[2] ^ received[4] ^ received[6]; // P1 group p2 = received[1] ^ received[2] ^ received[5] ^ received[6]; // P2 group p4 = received[3] ^ received[4] ^ received[5] ^ received[6]; // P4 group // Syndrome = binary value of parity errors → tells error position syndrome = p4 * 4 + p2 * 2 + p1; if (syndrome == 0) { printf("\nNo error detected. Message is correct.\n"); } else { printf("\nError detected at bit position: %d\n", syndrome); // Correct the bit by flipping received[syndrome - 1] ^= 1; printf("Corrected 7-bit message:\n"); for (i = 0; i < 7; i++) { printf("%d ", received[i]); } printf("\n"); } return 0; } OUTPUT Aim: Algorithm to implement data set of characters the three CRC polynomials – CRC 12, CRC 16 and CRC CCIP. Algorithm 1. Input the data (binary digits). 2. Choose the generator polynomial depending on the CRC type. 3. Append the required number of zero bits (equal to degree of polynomial-1) to the end of data. 4. Perform binary division (modulo-2) using XOR. 5. The remainder after division is the CRC checksum 6. Append the CRC checksum to the original binary data to get the final codeword. Cyclic Redundancy Check for 12 bit Polynomial Cyclic Redundancy Check for 16 bit Polynomial Cyclic Redundancy Check for Consultative Committee for International Telegraphy and Telephony 16 bit Polynomial PROGRAM: #include <stdio.h> #include <string.h> void xorOperation(char *dividend, char *divisor, int length) #XOR Operation { for (int i = 1; i < length; i++) { dividend[i - 1] = ((dividend[i] - '0') ^ (divisor[i] - '0')) + '0'; } } // Perform Modulo-2 division void computeCRC(char *data, char *poly, char *remainder, int polyLen) { int dataLen = strlen(data); #XOR division char temp[128]; strncpy(temp, data, polyLen); temp[polyLen] = '\0'; for (int i = polyLen; i <= dataLen; i++) { if (temp[0] == '1') xorOperation(temp, poly, polyLen); else xorOperation(temp, "00000000000000000", polyLen); // Dummy zero divisor temp[polyLen - 1] = (i < dataLen) ? data[i] : '0'; } strncpy(remainder, temp, polyLen - 1); #polyLen – 1 is CRC checksum remainder[polyLen - 1] = '\0'; } int main() { char binData[128]; printf("Enter binary data: "); scanf("%s", binData); char dataPadded[160], remainder[64]; // CRC-12 char crc12[] = "1100000001111"; // degree 12 strcpy(dataPadded, binData); strcat(dataPadded, "00000000000"); // append 11 zeros computeCRC(dataPadded, crc12, remainder, 12); printf("CRC-12 Checksum: %s\n", remainder); // CRC-16 char crc16[] = "11000000000000101"; // degree 16 strcpy(dataPadded, binData); strcat(dataPadded, "000000000000000"); // append 15 zeros computeCRC(dataPadded, crc16, remainder, 16); printf("CRC-16 Checksum: %s\n", remainder); // CRC-CCITT char crcCCITT[] = "10001000000100001"; // degree 16 strcpy(dataPadded, binData); strcat(dataPadded, "000000000000000"); // append 15 zeros computeCRC(dataPadded, crcCCITT, remainder, 16); printf("CRC-CCITT Checksum: %s\n", remainder); return 0; } OUTPUT Experiment 6. AIM: Write a C Program to implement Sliding window protocol for Goback N. PROGRAM: #include <stdio.h> #include <stdlib.h> #include <time.h> #define WINDOW_SIZE 4 // Example window size void sender(int total_frames) { int frames_sent = 0; int next_frame_to_send = 0; int ack_expected = 0; printf("Sender: Starting transmission...\n"); while (ack_expected < total_frames) { // Send frames within the window for (int i = 0; i < WINDOW_SIZE && (next_frame_to_send < total_frames); i++) { printf("Sender: Sending Frame %d\n", next_frame_to_send); frames_sent++; next_frame_to_send++; } // Simulate acknowledgment or timeout // For simplicity, we'll assume a random success/failure for the first frame in the window // In a real scenario, this involves timers and actual network communication if (rand() % 5 != 0) { // Simulate successful ACK for the first frame in the window printf("Receiver: Acknowledgment received for Frame %d\n", ack_expected); ack_expected++; } else { // Simulate timeout or lost ACK printf("Sender: Timeout! Retransmitting from Frame %d\n", ack_expected); next_frame_to_send = ack_expected; // Go back N } printf("\n"); } printf("Sender: All frames transmitted successfully.\n"); } int main() { srand(time(NULL)); // Seed for random number generation int total_frames; printf("Enter the total number of frames to send: "); scanf("%d", &total_frames); sender(total_frames); return 0; } OUTPUT Experiment 7. AIM: Write a C Program to implement Sliding window protocol for Selective repeat. PROGRAM: #include <stdio.h> #include <stdlib.h> #include <time.h> #define WINDOW_SIZE 4 // Example window size void sender_sr(int total_frames) { int next_frame_to_send = 0; int base = 0; // The oldest unacknowledged frame int sent_frames[total_frames]; // To keep track of sent frames // Initialize sent_frames array for (int i = 0; i < total_frames; i++) { sent_frames[i] = 0; // 0 means not yet sent/acked } printf("Sender: Starting Selective Repeat transmission...\n"); while (base < total_frames) { // Send frames within the window for (int i = 0; i < WINDOW_SIZE && (next_frame_to_send < total_frames); i++) { if (sent_frames[next_frame_to_send] == 0) { // Only send if not already sent/acked printf("Sender: Sending Frame %d\n", next_frame_to_send); sent_frames[next_frame_to_send] = 1; // Mark as sent } next_frame_to_send++; } // Simulate acknowledgments and potential retransmissions // For simplicity, we'll randomly simulate ACKs for frames within the window // In a real scenario, this involves timers and actual network communication for (int i = base; i < next_frame_to_send && i < total_frames; i++) { if (sent_frames[i] == 1) { // If frame was sent and not yet acknowledged if (rand() % 3 != 0) { // Simulate successful ACK printf("Receiver: Acknowledgment received for Frame %d\n", i); sent_frames[i] = 2; // Mark as acknowledged } else { // Simulate lost ACK or damaged frame, requiring retransmission printf("Sender: Timeout/NAK for Frame %d. Retransmitting...\n", i); sent_frames[i] = 0; // Mark for retransmission } } } // Slide the window forward if frames at the base are acknowledged while (base < total_frames && sent_frames[base] == 2) { base++; } printf("\n"); // Reset next_frame_to_send to base to re-evaluate sending opportunities next_frame_to_send = base; } printf("Sender: All frames transmitted successfully using Selective Repeat.\n"); } int main() { srand(time(NULL)); // Seed for random number generation int total_frames; printf("Enter the total number of frames to send: "); scanf("%d", &total_frames); printf("\n--- Go-Back-N Protocol ---\n"); sender_sr(total_frames); printf("\n--- Selective Repeat Protocol ---\n"); sender_sr(total_frames); return 0; } OUTPUT Experiment 8 AIM: Write a Program to implement Stop and Wait Protocol. The Stop and Wait Protocol can be simulated without actual network sockets to demonstrate the core logic. This simulation involves a sender and a receiver, where the sender transmits a frame and waits for an acknowledgment (ACK) before sending the next. PROGRAM: #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include <time.h> int main() { int n,i,wait; printf("Read number of frames you want send:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nframe %d send",i); wait=rand()%8; sleep(5); if(wait>5) { i=i-1; continue; } else printf("\nAck received for frame %d",i); } } OUTPUT: