1. Implementation of Stop and Wait Protocol and Sliding Window Protocol.
Simulation of Sliding Window Protocol
#include<stdio.h>
#include<stdlib.h> #include<math.h>
int k,time,win=2,i2=0,frame=0,a[20],b[20],i,j,s,r,ack,c,d;
int send(int,int); int receive(); int checsum(int *);
main()
{
int i1=0,j1=0,c1;
printf("Enter the frame size\n");
scanf("%d",&frame);
printf("Enter the window size\n");
scanf("%d",&win);
j1=win;
for(i=0;i<frame;i++)
{
a[i]=rand();
}
k=1;
while(i1<frame)
{
if((frame-i1)<win) j1=frame-i1;
printf("\n\ntransmit the window no %d\n\n",k);
c1=send(i1,i1+j1);
ack=receive(i1,i1+j1,c1);
if (ack!=0)
{
printf("\n\n1.Selective window\n");
printf("2.Go back N\n");
scanf("%d",&ack); switch(ack)
{
case 1:
printf("\n\n\t Selective window \t\nEnter the faulty frame no\n");
scanf("%d",&i2);
printf("\n\n Retransmit the frame %d \n",i2);
send(i2,i2+1);
break;
case 2:
printf("\n\n\t Go back n\t\n\n");
printf("\nRetransmit the frames from %d to %d\n",i1,i1+j1);
send(i1,i1+j1);
break;
}
}
i1=i1+win;
k++;
}
}
int send(c,d)
{
int t1;
for(i=c;i<d;i++)
{ b[i]=a[i]; printf("frame
sent\n",i);
}
s=checsum(&a[c]);
return(s);
}
int receive(c,d,c2) int c2;
{
r=checsum(&b[c]);
if(c2==r)
{
return(0);
}
else return(1);
}
int checsum(int *c)
{
int sum=0;
for(i=0;i<win;i++)
sum=sum^(*c);
return sum;
}
Output:
Enter the frame size
50
Enter the window size 5
transmit the window no 1
frame 0 is sent frame 1 is
sent
frame 2 is sent frame 3 is
sent frame 4 is sent
1.selective window 2.Go back N
1 selective window enter the
faculty frame no 15 retransmit
the frames from 15 frame 15 is
sent
transmit the window no 2
frame 5 is sent frame 6 is
sent frame 7 is sent frame 8
is sent frame 9 is sent
1.selective window 2.Go
back N
2. Study of Socket Programming and Client – Server model
#include<iostream.h>
#include<stdio.j>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<time.h> int
main()
int listenfd,connfd,n; struct sockaddr_in serv;
char buff[100]; int port=1058;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(port);
bind(listenfd,(struct sockaddr*)&serv,sizeof(serv));
listen(listenfd,15); while(1)
connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
cout<<"\nConx.Open!]n";
int child; if((child=fork())<0)
cout<<"\nChild disaster!"; exit(1);
else if(child==0)
close(listenfd); strcpy(buff,"abc");
while(strcmp(buff,"bye")!=0)
if(strcmp(buff,"bye")==0) break; n=read(connfd,buff,100);
buff[n]='\0';
cout<<"\nClient:"<<buff;
cout<<"Server:"; gets(buff);
write(connfd,buff,strlen(buff));
}
cout<<"\n\n Conx closed!";
close(connfd); exit(0);
close(connfd);
return 0;
Client Program:-
#include<stdio.h>
#include<sys/socket.h>
#include<iostream.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netbd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<errno.h>
#define MAX 100
main(int argc.char*argv)
{
int sockfd,n; char buff[100];
struct sockaddr_in serv;
if(argc!=3)
printf("\n Error ! usage:./a.out<IP ADDRESS><PORT>\n");
exit(0);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("\n Error ! Socket not created...\n");
exit(0);
bzero(&serv,sizeof(serv)); serv.sin_family=AF_INET;
serv.sin_port=htons(atoi9argv[2]));
if(inet_pton(AF_INET,argv[1],&serv.sin_addr)<0)
printf("\n Errorin conversion of IP address from string to num\n"); exit(0);
if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)
printf("\n Error ! can not established...\n"); exit
(0);
}
printf("\n connected..\n"); strcpy(buff,"abc");
while(strcmp(buff,"bye")!=0)
cout<<"\n Client:"; gets(buff);
write(sockfd,buff,strlen(buff));
cout<<"\n Server:";
n=read(sockfd,buff,100);
buff[n]='\0'; cout<<buff;
if(n<0)
cout<<"\n Read Error";
exit (0);
cout<<"\n"; return 0;
3. Write a code simulating ARP /RARP protocols.
Variation 1)
//ARP SERVER
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h> #include<string.h>
main()
{ int shmid, a, i;
Char *ptr, *shmptr;
shmid=shmget(3000,10,IPC_CREAT | 0666);
shmptr=shmat(shmid,NULL,0);
ptr=shmptr;
for(i=0;i<3;i++)
{
puts("enter the mac");
scanf("%s",ptr); a=strlen(ptr);
printf("string length:%d",a);
ptr[a]= ' ' ; puts("enter ip");
ptr=ptr+a+1; scanf("%s",ptr);
ptr[a]='\n' ;
ptr= ptr+a+1;
} ptr[strlen(ptr)]= '\0';
printf("\n ARP table at serverside is=\n%s", shmptr);
shmdt(shmptr);
}
ARP table at serverside is a.b.c.d
1.2.3.4
e.f.g.h 5.6.7.8
i.j.k.l 9.1.2.3
//ARP CLIENT
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/shm.h> main()
{
int shmid,a;
char *ptr, *shmptr; char ptr2[51], ip[12],
mac[26]; shmid=shmget(3000,10,0666);
shmptr=shmat(shmid,NULL,0); puts("the arp
table is"); printf("%s",shmptr);
printf("\n1.ARP\n 2.RARP\n 3.EXIT\n");
scanf("%d",&a);
switch(a)
{
case 1: puts("enter ip address");
scanf("%s",ip); ptr=strstr(shmptr,
ip); ptr-=8;
sscanf(ptr,"%s%*s",ptr2);
printf("mac addr is %s",ptr2);
break; case 2: puts("enter mac
addr"); scanf("%s",mac);
ptr=strstr(shmptr, mac);
sscanf(ptr,"%*s%s",ptr2);
printf("%s",ptr2); break; case 3:
exit(1);
}
}
SAMPLE INPUT OUTPUT:
the arp table is a.b.c.d 1.2.3.4
e.f.g.h 5.6.7.8
i.j.k.l 9.1.2.3
1.ARP
2.RARP 3.EXIT enter your
choice: 1 enter ip address:
1.2.3.4 mac addr is a.b.c.d
enter your choice:2 enter mac
address: e.f.g.h ip addr is 5.6.7.8
Variation 2)
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h> main()
{
char host[17],*ptr; long int
addr;
struct in_addr ad; /* netinet/in.h*/
puts("\n\nEnter the host ID"); gets(host);
addr= inet_addr(host); /*convert dotted decimal notation to 32-bit ip addres*/
puts("\n\n32 Bit IP address is:"); printf("\n\n%d",addr);
puts("\n\nEnter the 32 bit ip address :\n");
scanf("%d",&ad.s_addr);
ptr=inet_ntoa(ad); /*convert ip to dotted decimal*/
puts("\n\nHost ip address is");
printf("\n%s",ptr);
}
4. Write a code simulating PING and TRACEROUTE commands
PING:
Variation 1)
int main() {
const char *hostName = [@"stackoverflow.com"
cStringUsingEncoding:NSASCIIStringEncoding]; SCNetworkConnectionFlags flags = 0;
if (SCNetworkCheckReachabilityByName(hostName, &flags) && flags > 0) {
NSLog(@"Host is reachable: %d", flags);
} else {
NSLog(@"Host is unreachable");
}
}
Variation 2) bool success =
false;
const char *host_name = [@"stackoverflow.com"
cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef reachability =
SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags); bool isAvailable = success &&
(flags & kSCNetworkFlagsReachable) && !(flags &
kSCNetworkFlagsConnectionRequired); if (isAvailable) {
NSLog(@"Host is reachable: %d", flags);
}else{
NSLog(@"Host is unreachable");
}
5. Create a socket for HTTP for web page upload and download.
#include<stdio.h>
#include<stdlib.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h> #define size
100
int main(int argc, char *argv[])
{
struct sockaddr_in sock; struct
hostent *hp,port; char
*req,*hostname,*cp; FILE
*local; char buff[size]; int
con,i,l,nrv,sd;
if(argc!=3)
{
printf(“\nUsage: %s<server>filename”,argv[0]); exit(1);
}
if(cp=strchr(argv[1],’/’))
{ *cp=’\0’; l=strlen(argv[1]);
hostname=malloc(l+1);
strcpy(hostname,argv[1]);
*cp=’/’; l=strlen(cp);
req=malloc(l+1);
strcpy(req,cp);
}
else
{
hostname=argv[1]; req=”/”;
}
printf(“\nHost=%s\nReq= %s”,hostname,req); sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror(“\nCannot open socket”); exit(1);
}
bzero(&sock,sizeof(sock)); sock.sin_family=AF_INET;
con=inet_pton(AF_INET, argv[1], &sock);
sock.sin_port=htons(80); con=connect(sd,(struct sockaddr
*)&sock, sizeof(sock)); if(con<0)
{
perror(“\nConnection failed”);
exit(1);
}
sprintf(buff,”Get HTTP:%s//1.1\r\nHost: %s\r\nConnection: class\r\n\r”, req, hostname);
printf(“Buff=%s\n”, buff); l=strlen(buff); local=fopen(argv[2],”w”); write(sd,buff,1);
do
{
nrv=read(sd,buff,size);
if(nrv>0)
{
for(i=0;i<nrv;i++)
putc(buff[i],local);
}
else break;
}
while(1); close(sd);
fclose(local);
return 0;
}
6. Write a program to implement RPC (Remote Procedure Call)
REMOTE PROCEDURE CALL
AIM:To write a network program to call the Remote running process.
ALGORITHM:Step 1: Start the program
Step 2: Implement the operation to be in the procedure.
Step 3: In server side. register list of performance object in the RMI registry
Step 4: By starting the RMI registry we can run our server program to keep the program run
always.
Step 5: If the client want to involve to the procedure by means that by biding the registered
object in the RMI registry if(client) call the procedure and produce the result..
Step 6: Stop the program.
Program 1 for RPC (Interface):
import java.rmi.*; public interface greater
extends Remote
public String getresult(int first,int second)throws RemoteException;
RPC Program for client:
import java.io.*; import
java.rmi.*; public class
great
{
public static void main(String args[]) throws IOException
String result; int
n=0,n1=0;
try
greater f=(greater)Naming.lookup("greaterser");
try
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\n\t Rpc program for clientside");
System.out.println("\n\n\t ******************************");
System.out.println("\n\t Enter the two numbers"); System.out.println("\n\t
the First number is :"); n=Integer.parseInt(br.readLine());
System.out.println("\n\t the Second number is:");
n1=Integer.parseInt(br.readLine());
catch(IOException e)
result=f.getresult(n,n1); if(result.equals("Equal"))
System.out.println("\n\n\t Both nos are equal");
else
System.out.println("\n\n\t The greater no is"+result);
}
catch(Exception e)
System.out.println("Exception from client side:"+e);
}}}
RPC Program for server:
import java.io.*; import java.rmi.*; import java.rmi.server.*; import
java.util.*; public class greaterser extends UnicastRemoteObject
implements greater
public greaterser()throws RemoteException{} public String
getresult(int first,int second)throws RemoteException
if(first<second)
return(Integer.toString(second)); else
if(second<first) return(Integer.toString(first));
else return("EQUAL");
public static void main(String args[])
{ try
{
greaterser f=new greaterser(); Naming.rebind("greaterser",f);
System.out.println("\n\n\t Server is ready");
catch(Exception e)
System.out.println("error from ss");
}}}
OUTPUT:
RESULT:
Thus the program was executed and output is verified successfully.
7. Implementation of Subnetting .
dot-decimal address binary representation
Full IP Address 192.168.5.10 11000000.10101000.00000101.00001010
Subnet Mask 255.255.255.0
11111111.11111111.11111111.00000000
\\ and these bits to get the Network Address. Note, the prefix could be used \\
instead of subnet mask - they represent the same thing.
Network Address 192.168.5.0
11000000.10101000.00000101.00000000
\\ Host and prefix
Host Portion 0.0.0.10 00000000.00000000.00000000.00001010
Prefix /24 11111111.11111111.11111111.00000000
8. Applications using TCP Sockets like
a. Echo client and echo server
IMPLEMENTATION OF ECHO SERVER
AIM:
To Write a Network Socket program for implementing the echo operation.
ALGORITHM:
TCP Echo Server
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for server and bind socket with addresses.
4. Specify number of allowed connections.
5. Wait for connection.
6. Accept connection if any.
7. Retrieve information from Connected Socket.
8. Echo retrieved information to Connected Socket.
9. Close Connected Socket.
TCP Echo Client
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Give message to be echoed.
6. Retrieve echoed information from server and display it.
SERVER PROGRAM:
import java.io.*; import
java.net.*; import
java.lang.String.*; public
class Secho
public static void main(String args[]) throws Exception
{
ServerSocket ss =new ServerSocket(123);
Socket s=ss.accept();
DataInputStream in= new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
String str;
System.out.println("\nSERVER SIDE!...");
while(true)
str=in.readLine();
out.writeBytes(str+"\n");
System.out.println("Msg from Client");
System.out.println(str+"\n");
CLIENT PROGRAM:
import java.io.*; import
java.net.*; import
java.lang.String.*; public
class Cecho
public static void main(String args[]) throws Exception
{
DataInputStream in=new DataInputStream (System.in);
Socket s=new Socket("LocalHost",123);
DataInputStream inecho=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
String str;
System.out.println("\nCLIENT SIDE!...\nType EXIT TO QUIT\nEnter Client Msg");
while((str=in.readLine())!=null)
out.writeBytes(str+"\n");
if(str.equals("exit"))
out.writeBytes("\nClient Terminated");
break;
else
System.out.println("\nEcho From Server");
System.out.print(str+"\n");
System.out.println("\nCLIENT SIDE!...\nEnter Client Msg");
}}
OUTPUT: SERVER:
CLIENT:
RESULT:
Thus the program was executed and output is verified successfully.
b. Chat
CHAT APPLICATION
AIM:
To write a network program for implementing chat using TCP socket.
ALGORITHM:
1. Client sends the request to server
2. Server runs the request and the connection with the client that has requested the
server.
3. The client sends the message to server.
4. The server process it and it is displayed (ie) replier by sending the message to client
also displayed.
5. Stop the program.
SERVER PROGRAM:
import java.io.*; import
java.net.*; public class
chatserver
public static void main(String args[])throws Exception
DataInputStream din=null;
DataOutputStream dout=null;
Socket c=null;
ServerSocket m=null;
DataInputStream stdin=new DataInputStream(System.in);
try
m=new ServerSocket(68); c=m.accept(); din=new
DataInputStream(c.getInputStream()); dout=new
DataOutputStream(c.getOutputStream());
}
catch(Exception e)
while(c!=null)
String m2; System.out.println("Server");
while(true)
String m1=din.readLine();
System.out.println("Message from client.."+m1);
System.out.println("\n\n Enter the message...");
m2=stdin.readLine(); dout.writeBytes(""+m2);
dout.writeBytes("\n");
din.close(); dout.close();
c.close();
m.close();
CLIENT PROGRAM:
import java.io.*; import
java.net.*; public class
chatclient
public static void main(String args[])throws Exception
Socket c=null;
DataInputStream uin=null;
DataInputStream din=null; DataOutputStream
dout=null;
try
c=new Socket("localhost",68); uin=new
DataInputStream(System.in); din=new
DataInputStream(c.getInputStream()); dout=new
DataOutputStream(c.getOutputStream()); }
catch(Exception e)
} if(c!=null)
String inp;
System.out.println("Enter the message:");
while((inp=uin.readLine())!=null)
{
dout.writeBytes(""+inp); dout.writeBytes("\n");
System.out.println("Echoed message from server.."+din.readLine());
System.out.println("Enter ur message:");
}}
din.close(); dout.close();
c.close();
}}
OUTPUT: SERVER:
CLIENT:
RESULT:
Thus the program was executed and output is verified successfully.
c. File Transfer
#include <sys/socket.h> #include
<sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(void)
{
int sockfd = 0;
int bytesReceived = 0;
char recvBuff[256];
memset(recvBuff, '0', sizeof(recvBuff));
struct sockaddr_in serv_addr;
/* Create a socket first */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
/* Initialize sockaddr_in data structure */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000); // port
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Attempt a connection */
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
/* Create file where data will be stored */
FILE *fp;
fp = fopen("sample_file.txt", "ab");
if(NULL == fp)
{
printf("Error opening file");
return 1;
}
/* Receive data in chunks of 256 bytes */
while((bytesReceived = read(sockfd, recvBuff, 256)) > 0)
{
printf("Bytes received %d\n",bytesReceived);
// recvBuff[n] = 0;
fwrite(recvBuff, 1,bytesReceived,fp);
// printf("%s \n", recvBuff);
}
if(bytesReceived < 0)
{
printf("\n Read Error \n");
}
return 0; }
Server:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(void)
{
int listenfd = 0;
int connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
int numrv;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
printf("Socket retrieve success\n");
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr =
htonl(INADDR_ANY); serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
if(listen(listenfd, 10) == -1)
{
printf("Failed to listen\n");
return -1;
}
while(1) {
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
/* Open the file that we wish to transfer */
FILE *fp = fopen("sample_file.txt","rb");
if(fp==NULL) {
printf("File opern error");
return 1;
}
/* Read data from file and send it */
while(1)
{
/* First read file in chunks of 256 bytes */
unsigned char buff[256]={0};
int nread = fread(buff,1,256,fp);
printf("Bytes read %d \n", nread);
/* If read was success, send data. */
if(nread > 0) {
printf("Sending \n");
write(connfd, buff, nread);
} if (nread < 256)
{
if (feof(fp))
printf("End of file\n");
if (ferror(fp))
printf("Error reading\n");
break;
}
}
close(connfd);
sleep(1);
}
return 0;
}
9. Applications using TCP and UDP Sockets like
d. DNS
IMPLEMENTATION OF SIMPLE DNS
AIM:
To write a UDP program for implementing the simple DNS service.
ALGORITHM:
UDP Echo Server
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for server .
4. Bind socket with addresses.
5. Specify number of allowed connections.
6. Retrieve information from Connected Socket.
7. Echo retrieved information to Connected Socket.
8. Close Connected Socket.
UDP Echo Client
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Give message to be echoed.
6. Retrieve echoed information from server and display it.
7. Close the Socket.
SERVER PROGRAM
import java.net.*;
import java.io.*;
class p1sender
{ public static void main(String args[]) throws Exception {
byte senderBuffer[] = new byte[128]; // Packets 128 bytes long
String toName, userIn;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket senderSocket = new DatagramSocket(999);
// Create socket for sending
if (args.length > 0) toName = args[0]; // IP or DNS from command line
else toName = "localhost";
while ((userIn=in.readLine())!=null ){ // Read standard input
senderBuffer=userIn.getBytes(); // Convert from string to byte array
// Send datagram packet out socket
senderSocket.send(new DatagramPacket( senderBuffer, senderBuffer.length,
InetAddress.getByName(toName), 666));
} } }
CLIENT PROGRAM
import java.net.*;
class p1receiver { public static void main(String args[])
throws Exception {
byte receiveData[] = new byte[128];
DatagramSocket receiveSocket = new DatagramSocket(666); // Create socket for receiving
DatagramPacket receivePacket = new DatagramPacket(receiveData, 128);
while (true) {
receiveSocket.receive(receivePacket); // Wait for datagram packet to arrive
// Print packet contents as String
System.out.println(new String(receivePacket.getData(), 0, receivePacket.getLength()));
}}
OUTPUT:
SERVER
CLIENT
RESULT:
Thus the program was executed and output is verified successfully.
e. SNMP
f. File Transfer
10. Perform a case study about the different routing algorithms to
select the network path with its optimum and economical during
data transfer. i. Link State routing ii. Flooding iii. Distance vector
Program for CRC
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for (i=0;i<keylen-1;i++) {
input[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=input[i];
for (i=0;i<msglen;i++) {
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0'; else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--) {
if(temp[j]==key[j])
rem[j-1]='0'; else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for (i=0;i<msglen;i++)
printf("%c",input[i]);
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}
PROGRAM FOR FRAMING
BIT STUFFING
#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;
}
CHARACTER STUFFING
#include<stdio.h>
#include<string.h>
main()
{
char a[30], fs[50] = " ", t[3], sd, ed, x[3], s[3], d[3], y[3];
int i, j, p = 0, q = 0;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
for(i = 0; i < strlen(a); i++)
{
t[0] = a[i];
t[1] = '\0';
if(t[0] == sd)
strcat(fs, s);
else if(t[0] == ed)
strcat(fs, d);
else
strcat(fs, t);
}
strcat(fs, y);
printf("\n After stuffing:%s", fs);
getch();
}