Write C programs to implement TCP Socket. The client will take a bit stream from the user and send it to the server. The server will implement bit stuffing and send the stream back to the client. The client will print it.
Client Program
//This code is written by Souvik Ghosh
//TCP Client C Program
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = 9734;
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1) {
perror("not connected!!");
exit(1);
}else{
printf("connected!!\n");
}
//user input
int size;
int stuffedSize;
int flag;
printf("please enter the number of bits you want to send: ");
scanf("%d",&size);
int bits[size];
printf("please enter %d bits: ",size);
for(int i=0;i<size;i++){
scanf("%d",&bits[i]);
}
//read and write via sockfd
write(sockfd,&size,sizeof(int));
write(sockfd,&bits,size*sizeof(int));
printf("bits sent\n");
printf("waiting for the server...\n");
//check for stuffing
read(sockfd,&flag,sizeof(int));
if(flag){
printf("stuffed!!\n");
}else{
printf("no need to stuff!!\n");
}
//size of bits that to be received
read(sockfd,&stuffedSize,sizeof(int));
int stuffedBits[stuffedSize];
//read the bits
read(sockfd,&stuffedBits,sizeof(stuffedBits));
printf("numbers received: ");
int i=0;
for(int i=0;i<stuffedSize;i++){
printf("%d ",stuffedBits[i]);
}
printf("\n");
// close the socket connection
close(sockfd);
exit(0);
}
Server Program
//This code is written by Souvik Ghosh
//TCP Server C Program
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/*
struct sockaddr_in {
short int sin_family; //AF_INET
unsigned short int sin_port; //Port number
struct in_addr sin_addr; // Internet address
};
The IP address structure, in_addr, is defined as follows:
struct in_addr {
unsigned long int s_addr;
};
*/
// Function for bit stuffing
void bitStuffing(int N, int arr[],int brr[],int *len,int *flag)
{
// Variables to traverse arrays
int i, j, k;
i = 0;
j = 0;
//check for stuffing
int check=0;
// Loop to traverse in the range [0, N)
while (i < N) {
// If the current bit is 1 and previous bit is 0
if (arr[i] == 1 && arr[i-1]==0) {
// Stores the count of consecutive ones
int count = 0;
// Loop to check for next 5 bits
for (k = i; arr[k] == 1 && k < N && count < 5; k++) {
brr[j] = arr[k];
j++;
count++;
// If 5 consecutive set bits are found insert a 0 bit
if (count == 5) {
brr[j] = 0;
check=1;
}
i = k;
}
}
// Otherwise insert arr[i] into the array brr[]
else {
brr[j] = arr[i];
}
i++;
j++;
}
if(check){
printf("stuffed!!\n");
}else{
printf("no need to stuff!!\n");
}
*len=j;
*flag = check;
}
int main(){
int client_sockfd, server_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = 9734;
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
listen(server_sockfd, 5);
printf("Server started.\n");
while(1) {
printf("server waiting...\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);
//read
int n;
read(client_sockfd,&n,sizeof(int));
int receivedBits[n];
int sentBits[n*2];
int len;
int flag;
read(client_sockfd,&receivedBits,n*sizeof(int));
printf("bits received\n");
//perform operation
printf("performing operation...\n");
bitStuffing(n,receivedBits,sentBits,&len,&flag);
//write
write(client_sockfd,&flag,sizeof(int));
write(client_sockfd,&len,sizeof(int));
write(client_sockfd,&sentBits,len*sizeof(int));
for(int i=0;i<len;i++){
printf("%d ",sentBits[i]);
}
printf("\n");
printf("bits sent\n");
// close the socket connection
close(client_sockfd);
}
return 0;
}
Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.
Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…