Sie sind auf Seite 1von 8

//WriteaprogramforerrordetectingcodeusingCRCCCITT(16bits).

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
intmain()
{
charrem[50],a[50],s[50],c,msg[50]
chargen[]="10001000000000101"
inti,genlen,t,j,flag=0,k,n
system("clear")
printf("\nGeneratorpolynomialisCRC
CCITT:%s",gen)
genlen=strlen(gen)
k=genlen1

printf("\nEnterthemessage:")
n=0
while((c=getchar())!='\n')
{
msg[n]=c
n++
}
msg[n]='\0'
for(i=0i<ni++)
a[i]=msg[i]
for(i=0i<ki++)
a[n+i]='0'
a[n+k]='\0'
printf("\nMessagepolynomialappendedwithzero's:")
puts(a)
for(i=0i<ni++)
{
if(a[i]=='1')
{
t=i
for(j=0j<=kj++)
{
if(a[t]==gen[j])
a[t]='0'
else
a[t]='1'
t++
}
}
}
for(i=0i<ki++)
rem[i]=a[n+i]
rem[k]='\0'
printf("\nThechecksumappended:")
puts(rem)
printf("\nMessagewithchecksumappended:")
for(i=0i<ni++)
a[i]=msg[i]
for(i=0i<ki++)
a[n+i]=rem[i]
a[n+k]='\0'
puts(a)
n=0

n=0
printf("\nEnterthereceivedpolynomial:")
while((c=getchar())!='\n')
{
s[n]=c
n++
}
s[n]='\0'
for(i=0i<ni++)
{
if(s[i]=='1')
{
t=i
for(j=0j<=kj++,t++)
{
if(s[t]==gen[j])
s[t]='0'
else
s[t]='1'
}
}
}
for(i=0i<ki++)
rem[i]=s[n+i]
rem[k]='\0'
for(i=0i<ki++)
{
if(rem[i]=='1')
flag=1
}
if(flag==0)
printf("\nThereceivedpolynomialiserrorfree\n")
else
printf("\nAlert!Thereceivedpolynomialhaserror!!\n")
return0
}
//Writeaprogramfordistancevectoralgorithmtofindsuitablepathfortransmission.
#include<stdio.h>
#include<string.h>
structnode{
intdist[20]
intfrom[20]
}rt[10]
intmain()
{
intdmat[20][20],i,j,k
intn=i=j=k=0,count=0
printf("EnterTheNumberofNodes\n")
scanf("%d",&n)
printf("EnterTheCostMatrix\n")
for(i=0i<ni++)
{
for(j=0j<nj++)
{
scanf("%d",&dmat[i][j])
dmat[i][i]=0
rt[i].dist[j]=dmat[i][j]
rt[i].from[j]=j
}
}
do
{
count=0

count=0
for(i=0i<ni++)
{
for(j=0j<nj++)
{
for(k=0k<nk++)
{
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]
rt[i].from[j]=k
count++
}
}
}
}
}while(count!=0)
for(i=0i<ni++)
{
printf("StateValueForRouter%dIs\n",i)
for(j=0j<nj++)
printf("\t\tVia%dDistance%d\n",
rt[i].from[j],rt[i].dist[j])
}
}
/*UsingTCP/IPsockets,writeaclientserverprogramtomaketheclientsendthefilenameand
tomaketheserversendbackthecontentsoftherequestedfileifpresent.*/
//9server.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/fcntl.h>
#include<netdb.h>
#defineSERVER_PORT2234
#defineBUF_SIZE4096
intmain()
{
ints,b,l,fd,sa,bytes,on=1
charbuf[BUF_SIZE],fname[255]
structsockaddr_inchannel
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)
if(s<0)
{
printf("socketfailed")exit(0)
}
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&on,sizeof(on))
memset(&channel,0,sizeof(channel))//allocatememoryfor'channel'
channel.sin_family=AF_INET//assignvalues
channel.sin_addr.s_addr=htonl(INADDR_ANY)
channel.sin_port=htons(SERVER_PORT)
b=bind(s,(structsockaddr*)&channel,sizeof(channel))
if(b<0)
{
printf("bindfailed")
exit(0)
}
listen(s,5)
//listenchannelforanyrequest

//listenchannelforanyrequest
while(1)
{
printf("\n\nWaitingforrequest:\n")
sa=accept(s,0,0)
//createasocketforcommunication
if(sa<0)
{
printf("acceptfailed")
exit(0)
}
memset(fname,0,sizeof(fname))
read(sa,fname,BUF_SIZE)
//readfilenamefromchannel
printf("requestedfilename:%s",fname)
fd=open(fname,O_RDONLY)
//openfiletoREAD
if(fd<0)
{
printf("\nErrormessagesenttoclient\n")
write(sa,"couldnotopenrequestedfile",40)
}
else
{
while(1)
{
bytes=read(fd,buf,BUF_SIZE)//readfile&storeinbuf
if(bytes<=0)break
write(sa,buf,bytes)//writethecontentstochannel
}
close(fd)
//closeconnection
}
close(sa)
}
}
//9client.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/fcntl.h>
#include<netdb.h>
#defineSERVER_PORT2234
#defineBUF_SIZE4096
intmain()
{
intc,s,bytes
charbuf[BUF_SIZE],fname[255]
structhostent*h
structsockaddr_inchannel
printf("Enterthefilename:")
gets(fname)
h=gethostbyname("localhost")//getserveraddress
if(!h)
{
printf("gethostbynamefailed")
exit(0)
}
memset(&channel,0,sizeof(channel))//allocatememoryfor'channel'
channel.sin_family=AF_INET
//assignvalues
memcpy(&channel.sin_addr.s_addr,h>h_addr,h>h_length)
channel.sin_port=htons(SERVER_PORT)

channel.sin_port=htons(SERVER_PORT)
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)
if(s<0)
{
printf("socketcreationfailed")
exit(0)
}
c=connect(s,(structsockaddr*)&channel,sizeof(channel))
if(c<0)
{
printf("connectfailed")
exit(0)
}
write(s,fname,strlen(fname))//writethefilenametochannel
while(1)
{
bytes=read(s,buf,BUF_SIZE)
if(bytes<=0)exit(0)
write(1,buf,bytes)
}
}
//ImplementtheaboveprogramusingasmessagequeuesorFIFOsasIPCchannels.
//10server.c
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#defineFIFO1_NAME"fifo1"
#defineFIFO2_NAME"fifo2"
intmain()
{
charfilename[100],buf[100],buf1[300]
intm,n,f1,fd,fd2,filesize

mknod(FIFO1_NAME,S_IFIFO|0666,0)
mknod(FIFO2_NAME,S_IFIFO|0666,0)

printf("SERVERonline!\n\n")
fd=open(FIFO1_NAME,O_RDONLY)

n=read(fd,&filename,100)
filename[n]='\0'
f1=open(filename,O_RDONLY)
if(f1==1)
{
fd2=open(FIFO2_NAME,O_WRONLY)
write(fd2,"Filedoesnotexist",25)
exit(0)
}
printf("SERVER:%sfound!\nTransferringthecontents...\n",filename)
filesize=lseek(f1,0,2)
lseek(f1,0,0)

n=read(f1,buf1,filesize)
buf1[n]='\0'
fd2=open(FIFO2_NAME,O_WRONLY)
write(fd2,buf1,n)

printf("SERVER:TransferCompleted!\n")
exit(0)

exit(0)
unlink(FIFO1_NAME)
unlink(FIFO2_NAME)
}
//10client.c
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#defineFIFO1_NAME"fifo1"
#defineFIFO2_NAME"fifo2"
intmain()
{
charfilename[100],buf[100]
intm,n,fl,fd,fd2
mknod(FIFO1_NAME,S_IFIFO|0666,0)
mknod(FIFO2_NAME,S_IFIFO|0666,0)
fd=open(FIFO1_NAME,O_WRONLY)
printf("ClientOnline!\nCLIENT:enterthefilename...\n\n")
scanf("%s",filename)
write(fd,filename,strlen(filename))

printf("Waitingforreply...\n")
fd2=open(FIFO2_NAME,O_RDONLY)
n=read(fd2,&buf,300)
buf[n]='\0'
printf("Filereceived!...thecontentsare....\n\n")
puts(buf)
unlink(FIFO1_NAME)
unlink(FIFO2_NAME)
exit(1)
}
//WriteaprogramforsimpleRSAalgorithmtoencryptanddecryptthedata.
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<string.h>
usingnamespacestd
longintgcd(longinta,longintb)
{
if(a==0)
returnb
if(b==0)
returna
returngcd(b,a%b)
}
longintisprime(longinta)
{
inti
for(i=2i<ai++){
if((a%i)==0)
return0
}
return1
}
longintencrypt(charch,longintn,longinte)
{

{
inti
longinttemp=ch
for(i=1i<ei++)
temp=(temp*ch)%n
returntemp
}
chardecrypt(longintch,longintn,longintd)
{
inti
longinttemp=ch
for(i=1i<di++)
ch=(temp*ch)%n
returnch
}
intmain()
{
longinti,len
longintp,q,n,phi,e,d,cipher[50]
chartext[50]
cout<<"Enterthetexttobeencrypted:"
cin.getline(text,sizeof(text))
len=strlen(text)
do{
p=rand()%30
}while(!isprime(p))
do{
q=rand()%30
}while(!isprime(q))
n=p*q
phi=(p1)*(q1)
do{
e=rand()%phi
}while(gcd(phi,e)!=1)
do{
d=rand()%phi
}while(((d*e)%phi)!=1)
cout<<"Twoprimenumbers(pandq)are:"<<p<<"and"<<q<<endl
cout<<"n(p*q)="<<p<<"*"<<q<<"="<<p*q<<endl
cout<<"(p1)*(q1)="<<phi<<endl
cout<<"Publickey(n,e):("<<n<<","<<e<<")\n"
cout<<"Privatekey(n,d):("<<n<<","<<d<<")\n"
for(i=0i<leni++)
cipher[i]=encrypt(text[i],n,e)
cout<<"Encryptedmessage:"
for(i=0i<leni++)
cout<<cipher[i]
for(i=0i<leni++)
text[i]=decrypt(cipher[i],n,d)
cout<<endl
cout<<"Decryptedmessage:"
for(i=0i<leni++)
cout<<text[i]
cout<<endl
return0
}
//Writeaprogramforcongestioncontrolusingleakybucketalgorithm
#include<stdio.h>
#include<string.h>
intmin(intx,inty)

intmin(intx,inty)
{
if(x<y)
returnx
else
returny
}
intmain()
{
intdrop=0,mini,nsec,cap,count=0,i,inp[25],process
printf("EnterTheBucketSize\n")
scanf("%d",&cap)
printf("EnterTheProcessingRate\n")
scanf("%d",&process)
printf("EnterTheNo.OfSecondsYouWantToSimulate\n")
scanf("%d",&nsec)
for(i=0i<nseci++)
{
printf("EnterTheSizeOfThePacketEnteringAt%dsec\n",i+1)
scanf("%d",&inp[i])
}
printf("\nSecond|PacketReceived|PacketSent|PacketLeft|PacketDropped\n")
for(i=0i<nseci++)
{
count+=inp[i]
if(count>cap)
{
drop=countcap
count=cap
}
printf("%d",i+1)
printf("\t%d",inp[i])
mini=min(count,process)
printf("\t\t%d",mini)
count=countmini
printf("\t\t%d",count)
printf("\t\t%d\n",drop)
drop=0
}
for(count!=0i++)
{
if(count>cap)
{
drop=countcap
count=cap
}
printf("%d",i+1)
printf("\t0")
mini=min(count,process)
printf("\t\t%d",mini)
count=countmini
printf("\t\t%d",count)
printf("\t\t%d\n",drop)
}
return0
}

Das könnte Ihnen auch gefallen