AIM:
To write a program for bit stuffing.
ALGORITHM:
SERVER:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Bind the IP address and Port number
STEP 6: Listen and accept the client’s request for the connection
STEP 7: Establish the connection with the client
STEP 8: Display the file name
STEP 9: Dispaly the input message
STEP 10: Dispaly the output message after bit stuffing
STEP 11: Close the socket
STEP 12: Stop
CLIENT:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Call the connect() function
STEP 6: Read the file name
STEP 7: Send the file name to the server
STEP 8: Close the socket
STEP 9: Stop
SOURCE CODE:
SERVER:
#include<stdio.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX 60
void stuff();
int i,j,tem;
char buff[4096],t;
FILE *f1;
int main(int arg,char **argv)
{
int sockfd,newsockfd,clength;
struct sockaddr_in serv_addr,cli_addr;
char t[MAX],str[MAX];
strcpy(t,"exit");
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nBinded");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nListening...\n");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
close(sockfd);
read(newsockfd,&str,MAX);
printf("\nClient Message \nFile Name:\t%s\n",str);
f1=fopen(str,"r");
while(fgets(buff,4096,f1)!=NULL)
{
stuff();
write(newsockfd,buff,MAX);
printf("\n");
}
fclose(f1);
return 0;
}
void stuff()
{
i=j=tem=0;
printf("\nInput : %s",buff);
printf("\nOutput : ");
do
{
if(buff[i]=='1')
{
if(j==4)
{
tem=i-4;
t='0';
for(;tem<=i;tem++)
printf("%c",buff[tem]);
j=-1;
printf("%c",t);
}
i+=1;
j+=1;
}
else
{
tem=i;
tem=tem-j;
for(;tem<=i;tem++)
printf("%c",buff[tem]);
i+=1;
j=0;
}
}while(buff[i]!='\0');
}
CLIENT:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX 60
int main(int argc,char **argv)
{
int sockfd,n;
struct sockaddr_in serv_addr;
struct hostent *server;
char send[MAX],recvline[MAX],s[MAX],name[MAX];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nEnter the source file name:\n");
scanf("%s",send);
write(sockfd,send,MAX);
while((n=read(sockfd,recvline,MAX))!=0)
{
printf("%s",recvline);
}
close(sockfd);
return 0;
}
OUTPUT:
SERVER:
CLIENT:
RESULT:
Thus the program for bit stuffing was executed and the output was verified.
EmoticonEmoticon