0% found this document useful (0 votes)
18 views

Computer Network Practical

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Computer Network Practical

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1

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() {

int a[20], b[30], i, j, k, count, n;

printf("Enter frame size (Example: 8):");

scanf("%d", &n);

printf("Enter the frame in the form of 0 and 1 :");

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

i = 0;

count = 1;

j = 0;

while (i < n) {

if (a[i] == 1) {

b[j] = a[i];

for (k = i + 1; a[k] == 1 && k < n && count < 5; k++) {

j++;

b[j] = a[k];

count++;

if (count == 5) {

j++;

b[j] = 0;

i = k;
}

} else {

b[j] = a[i];

i++;

j++;

printf("After Bit Stuffing :");

for (i = 0; i < j; i++)

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

What is byte stuffing in computer networks?

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>

void byteStuffing(int N, int arr[]) {

// Here we will store the stuffed array.

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';

} else if (arr[i] == 'E') {

brr[j] = 'E';

j++;

brr[j] = 'E';

} else {

brr[j] = arr[i];

i++;

j++;

for (i = 0; i < j; i++)

printf("%c", brr[i]);

int main() {

int N = 6;

int arr[] = { 'F', 'E', 'A', 'B', 'F', 'E' };

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>

#include <unistd.h> // read(), write(), close()

#define MAX 80

#define PORT 8080

#define SA struct sockaddr

// Function designed for chat between client and server.

void func(int connfd)

char buff[MAX];

int n;

// infinite loop for chat

for (;;) {

bzero(buff, MAX);

// read the message from client and copy it in buffer


read(connfd, buff, sizeof(buff));

// print buffer which contains the client contents

printf("From client: %s\t To client : ", buff);

bzero(buff, MAX);

n = 0;

// copy server message in the buffer

while ((buff[n++] = getchar()) != '\n')

// and send that buffer to client

write(connfd, buff, sizeof(buff));

// if msg contains "Exit" then server exit and chat ended.

if (strncmp("exit", buff, 4) == 0) {

printf("Server Exit...\n");

break;

// Driver function

int main()

int sockfd, connfd, len;

struct sockaddr_in servaddr, cli;

// socket create and verification

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd == -1) {

printf("socket creation failed...\n");

exit(0);
}

else

printf("Socket successfully created..\n");

bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(PORT);

// Binding newly created socket to given IP and verification

if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {

printf("socket bind failed...\n");

exit(0);

else

printf("Socket successfully binded..\n");

// Now server is ready to listen and verification

if ((listen(sockfd, 5)) != 0) {

printf("Listen failed...\n");

exit(0);

else

printf("Server listening..\n");

len = sizeof(cli);

// Accept the data packet from client and verification

connfd = accept(sockfd, (SA*)&cli, &len);

if (connfd < 0) {

printf("server accept failed...\n");


exit(0);

else

printf("server accept the client...\n");

// Function for chatting between client and server

func(connfd);

// After chatting close the socket

close(sockfd);

4.

1. Socket creation:

sockfd: socket descriptor, an integer (like a file-handle)

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.

type: communication type

SOCK_STREAM: TCP(reliable, connection oriented)

SOCK_DGRAM: UDP(unreliable, connectionless)

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);

// Server side C/C++ program to demonstrate Socket


// programming

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <unistd.h>

#define PORT 8080

int main(int argc, char const* argv[])

int server_fd, new_socket, valread;

struct sockaddr_in address;

int opt = 1;

int addrlen = sizeof(address);

char buffer[1024] = { 0 };

char* hello = "Hello from server";

// Creating socket file descriptor

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {

perror("socket failed");

exit(EXIT_FAILURE);

// Forcefully attaching socket to the port 8080

if (setsockopt(server_fd, SOL_SOCKET,

SO_REUSEADDR | SO_REUSEPORT, &opt,

sizeof(opt))) {

perror("setsockopt");

exit(EXIT_FAILURE);

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;

address.sin_port = htons(PORT);

// Forcefully attaching socket to the port 8080

if (bind(server_fd, (struct sockaddr*)&address,

sizeof(address))

< 0) {

perror("bind failed");

exit(EXIT_FAILURE);

if (listen(server_fd, 3) < 0) {

perror("listen");

exit(EXIT_FAILURE);

if ((new_socket

= accept(server_fd, (struct sockaddr*)&address,

(socklen_t*)&addrlen))

< 0) {

perror("accept");

exit(EXIT_FAILURE);

valread = read(new_socket, buffer, 1024);

printf("%s\n", buffer);

send(new_socket, hello, strlen(hello), 0);

printf("Hello message sent\n");

// closing the connected socket

close(new_socket);

// closing the listening 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_socket = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in server_address;

server_address.sin_family = AF_INET;

server_address.sin_port = htons(9002);

server_address.sin_addr.s_addr = INADDR_ANY;

bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));

listen(server_socket, 5);

int client_socket;

client_socket = accept(server_socket, NULL, NULL);

char server_message[256] = "You have reached the server!";

send(client_socket, server_message, sizeof(server_message), 0);

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;

network_socket = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in server_address;

server_address.sin_family = AF_INET;

server_address.sin_port = htons(9002);

server_address.sin_addr.s_addr = INADDR_ANY;

int connection_status = connect(network_socket, (struct sockaddr *) &server_address,


sizeof(server_address));

if (connection_status == -1) {

printf("There was an error making a connection to the remote socket \n\n");

exit(1);

char server_response[256];

recv(network_socket, &server_response, sizeof(server_response), 0);

printf("The server sent the data: %s\n", server_response);


close(network_socket);

return 0;

You might also like