Computer Network Practical
Computer Network Practical
Bit stuffing is the insertion of non-information bits into data. It is used to define the end of a frame
and the beginning of the next frame in variable-size framing1.
The stuffed bits should not be confused with overhead bits, which are non-data bits that are
necessary for transmission1.
Bit stuffing can also serve other purposes such as synchronizing several channels before multiplexing,
rate-matching two single channels to each other and run-length limited coding
#include <stdio.h>
#include <string.h>
int main() {
scanf("%d", &n);
scanf("%d", &a[i]);
i = 0;
count = 1;
j = 0;
while (i < n) {
if (a[i] == 1) {
b[j] = a[i];
j++;
b[j] = a[k];
count++;
if (count == 5) {
j++;
b[j] = 0;
i = k;
}
} else {
b[j] = a[i];
i++;
j++;
printf("%d", b[i]);
return 0;
Byte stuffing is a method used in the Data Link layer to separate frames in variable-size framing. It
involves adding an extra byte called the escape character (ESC)
before every byte in the message with the same pattern as the flag byte1. If the ESC sequence is
found in the message byte, then another ESC byte is stuffed before it
Purpose of Byte Stuffing. In Data Link layer, the stream of bits from physical layer are divided into
data frames. The...
Frame in a Character Oriented Framing. In character oriented protocols, the message is coded as 8-
bit characters,...
Byte Stuffing Mechanism. If the pattern of the flag byte is present in the message...
#include <stdio.h>
#include <string.h>
int brr[30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 'F') {
brr[j] = 'E';
j++;
brr[j] = 'F';
brr[j] = 'E';
j++;
brr[j] = 'E';
} else {
brr[j] = arr[i];
i++;
j++;
printf("%c", brr[i]);
int main() {
int N = 6;
byteStuffing(N, arr);
return 0;
3
TCP (Transmission Control Protocol) is a connection-oriented protocol that provides reliable data
transfer between a client and a server.
To establish a TCP connection between a client and a server, the following steps are taken:
The client sends a SYN (synchronize) packet to the server with a unique, random sequence number.
The server responds with a SYN-ACK (synchronize-acknowledge) packet that contains its own unique
sequence number and an acknowledgment number equal to the client’s sequence number plus one.
The client responds with an ACK (acknowledge) packet that contains an acknowledgment number
equal to the server’s sequence number plus one.
Once these steps are completed, the TCP connection is established and data can be transferred
between the client and server.
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
char buff[MAX];
int n;
for (;;) {
bzero(buff, MAX);
bzero(buff, MAX);
n = 0;
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
// Driver function
int main()
if (sockfd == -1) {
exit(0);
}
else
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
exit(0);
else
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
else
printf("Server listening..\n");
len = sizeof(cli);
if (connfd < 0) {
else
func(connfd);
close(sockfd);
4.
1. Socket creation:
domain: integer, specifies communication domain. We use AF_ LOCAL as defined in the POSIX
standard for communication between processes on the same host. For communicating between
processes on different hosts connected by IPV4, we use AF_INET and AF_I NET 6 for processes
connected by IPV6.
protocol: Protocol value for Internet Protocol(IP), which is 0. This is the same number which appears
on protocol field in the IP header of a packet.(man protocols for more details)
2. Setsockopt:
This helps in manipulating options for the socket referred by the file descriptor sockfd. This is
completely optional, but it helps in reuse of address and port. Prevents error such as: “address
already in use”.
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int opt = 1;
char buffer[1024] = { 0 };
perror("socket failed");
exit(EXIT_FAILURE);
if (setsockopt(server_fd, SOL_SOCKET,
sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
sizeof(address))
< 0) {
perror("bind failed");
exit(EXIT_FAILURE);
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
if ((new_socket
(socklen_t*)&addrlen))
< 0) {
perror("accept");
exit(EXIT_FAILURE);
printf("%s\n", buffer);
close(new_socket);
shutdown(server_fd, SHUT_RDWR);
return 0;
}
5.
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int server_socket;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
listen(server_socket, 5);
int client_socket;
close(server_socket);
return 0;
Client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int network_socket;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
if (connection_status == -1) {
exit(1);
char server_response[256];
return 0;