You are on page 1of 35

EX.

NO:1 SIMULATION OF ARP AND RARP


DATE:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct arp
{
char pa[30];
char la[30];
}s[10];
main()
{
int i,j,c,r;
char ph[30];
char lg[30];
printf("\n\t SIMULATION ARP AND RARP\n");
printf("Enter the no of hosts");
scanf("%d",&j);
printf("\n Enter the hosts logical address and physical address\n");
for(i=0;i<j;i++)
{
printf("\n logical address");
scanf("%s",&s[i].la);
printf("\n physical address");
scanf("%s",&s[i].pa);
}
do
{
printf("\n\n What you want to do?\n\n 1.ARP\n 2.RARP\n");
printf("\n Enter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the logical address");
scanf("%s",&lg);
for(i=0;i<j;i++)
{
if(strcmp(lg,s[i].la)==0)
{
printf("physical address=%s",s[i].pa);
}
else
{
printf("The given address not in the list");
}
}
break;
case 2:
printf("\n Enter the physical address \n");
scanf("%s",&ph);
for(i=0;i<j;i++)
{
if(strcmp(ph,s[i].pa)==0)
{
printf("logical address:%s\t",s[i].la);
}
else
{
printf("the given number not in the list");
}
break;
}
printf("Do you want to continue(1/0):");
scanf("%d",&r);
}
while(r==1);
}
OUTPUT:
[oecit@localhost ~]$ cc arp.c
[oecit@localhost ~]$ ./a.out

SIMULATION ARP AND RARP


Enter the no of hosts2
Enter the hosts logical address and physical address
logical address 172.60.5.4
physical address180.11.2.6
logical address173.11.16.5
physical address180.5.6.2
What you want to do?
1.ARP
2.RARP

Enter your choice1


Enter the logical address172.60.5.4
physical address=180.11.2.6
The given address not in the list
Do you want to continue(1/0):1

What you want to do?


1.ARP
2.RARP

Enter your choice2


Enter the physical address180.5.6.2
logical address:173.11.16.5
The given number not in the list
Do you want to continue(1/0):0
EX.NO:2 SIMULATION OF OSPF
DATE:

#include<stdio.h>
int main()
{
int a[50][6],i,j,k,n;
printf("Enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("Enter the cost of node[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
printf("The intial adjacency matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
printf("The adjacency matrix after finding the shortest path is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
OUTPUT:
[oecit@localhost ~]$ cc ospf.c
[oecit@localhost ~]$ ./a.out
Enter the number of nodes3
Enter the cost of node[0][0]:0
Enter the cost of node[0][1]:5
Enter the cost of node[0][2]:6
Enter the cost of node[1][0]:1
Enter the cost of node[1][1]:0
Enter the cost of node[1][2]:6
Enter the cost of node[2][0]:9
Enter the cost of node[2][1]:2
Enter the cost of node[2][2]:0

The intial adjacency matrix:


0 5 6
1 0 6
9 2 0

The adjacency matrix after finding the shortest path is


0 5 6
1 0 6
3 2 0
EX.NO:3 DOMAIN NAME SPACE
DATE:

#include<netdb.h>
#include<stdio.h>
#include<arpa/inet.h>
#include<netinet/in.h>
int main(int argc,char **argv)
{
struct hostent *host;
struct in_addr h_addr;
if(argc!=2)
{
fprintf(stderr,"USAGE:nslookup(inet_address)\n");

}
if((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"(mini)nslookup failed on %s\n",argv[1]);

}
h_addr.s_addr=*((unsigned long*)host->h_addr_list[0]);
printf("\n ipaddress=%s\n\n",inet_ntoa(h_addr));
printf("hostname=%s\n\n",host->h_name);
printf("addresslength=%d\n\n",host->h_length);
printf("Addresstype=%d\n\n",host->h_addrtype);
printf("list of address=%s\n\n",inet_ntoa(h_addr_list[0]));

}
OUTPUT:
[oecit@localhost ~]$ cc dns.c
[oecit@localhost ~]$ ./a.out 172.16.0.198
Ip address=172.16.0.198
Host address=172.16.0.198
Address length=4
Address type=2
List of address=172.16.0.198
EX.NO:4 ECHO CHATTING USING TCP
DATE:

CLIENT:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
main()
{
int sd,i=0,port;
static char content[30],echo[30];
struct sockaddr_in ser;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\n Socket cteation problem_check the parameter\n");
return 0;
}
bzero((char*)&ser,sizeof(ser));
printf("\n\n CLient in starting \n");
printf("enter your port");
scanf("%d",&port);
printf("theport address is\n");
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
if(connect(sd,(struct sockaddr*)&ser,sizeof(ser))==-1)
{
sprintf("\n\n::connecting problem-check the server availability status\n");
}
printf("\nCLIENT MODULE\n");
printf("_______________");
printf("enter the message\n");
scanf("%s",content);
send(sd,content,30,0);
i=recv(sd,echo,30,0);
printf("\n%s",echo);
return 0;
exit(1);
}
SERVER:

#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<string.h>
#include<pwd.h>
#include<netdb.h>
main()
{
static char content[30];
int sd,i=0,port;
struct sockaddr_in ser,cli;
printf("\nserver in starting\n");
if((sd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("\n error in creation\n");
return 0;
}
bzero((char*)&ser,sizeof(ser));
printf("\n enter your port no:\t\t");
scanf("%d",&port);
printf("\n\n\nPORT ADDRESS IS%d\n");
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr*)&ser,sizeof(ser))==-1)
{
printf("error in binding\n");
return 0;
}
i=sizeof(cli);
listen(sd,1);
printf("\nSERVER MODULE\n");
printf("_________________");
sd=accept(sd,(struct sockaddr*)&cli,&i);
if(sd==1)
{
printf("\n error in accepting\n");
return 0;
}printf("\n\nACCEPTING\n");
i=0;
i=recv(sd,content,30,0);
printf("Received Message content\t%s\n",content);
send(sd,content,30,0);
printf("the message has been echoed\n");
close(sd);
printf("\n\n");
exit(1);
}
OUTPUT:
CLIENT
[oecit40@localhost ~]$ cc echoc40.c
[oecit40@localhost ~]$ ./a.out
client in starting
enter your port no3215
Port address is3215client module
----------------
enter the message
hello
SERVER
[oecit40@localhost ~]$ cc echoS40.c
[oecit40@localhost ~]$ ./a.out
server in startingEnter your port no:3215
PORT ADDRESS IS3215
server module
Accepting
Message has been encoded
EX.NO:5 CLIENT SERVER CHATING USING TCP
DATE:

TCP SERVER PROGRAM:

#include<stdio.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<pwd.h>
#include<netdb.h>
main()
{
static char content[30][30];
int sd,i=0,port,a,b;
struct sockaddr_in ser,cli;
printf("\n Enter the port no");
scanf("%d",&port);
printf("\n server in starting\n");
if((sd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("\n error in creation\n",0);
return 0;
}
bzero((char*)&ser,sizeof(ser));
printf("\n port address is %d\n",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr*)&ser,sizeof(ser))==1)
{
printf("\n error in binding\n\n");
return 0;
}
i=sizeof(cli);
listen(sd,1);
printf("\n server module\n");
printf("*******************\n");
sd=accept(sd,(struct sockaddr*)&cli,&i);
if(sd==1)
{
printf("\n error in accepting");
return 0;
}
a=0;
printf("\n\n accepting");
while(content[a]!="end")
{
i=0;
a=a+1;
i=recv(sd,content[a],30,0);
printf("\n message from client:%s",content[a]);
printf("\n enter your message:\t");
a=a+1;
scanf("%s",content[a]);
break;
send(sd,content[a],30,0);
}
close(sd);
printf("\n\n");
}
TCP CLIENT PROGRAM:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
main()
{
int sd,i=0,port,b;
static char content[30][30],echo[30];
struct sockaddr_in ser;
printf("Enter the port no");
scanf("%d",&port);
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\n::socket creation problem check the parameter\n");
return 0;
}
bzero((char*)&ser,sizeof(ser));
printf("\n client in starting");
printf("\n port address is %d",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
if(connect(sd,(struct sockaddr*)&ser,sizeof(ser))==-1)
{
printf("\n connecting problem_check the server availability stat
us");
}
printf("\n client module");
printf("**********\n");
b=1;
do
{
printf("\n enter your message:\t");
scanf("%s\n",content[b]);
send(sd,content[b],30,0);
b=b+1;
i=recv(sd,content[b],30,0);
printf("\n message from server:%s\n",content[b]);
if(content[b]=="end");
break;
}
while(content[b]!="end");
return 0;
exit(1);
}
OUTPUT:
Server:
[oecit@localhost ~]$ cc tcp.c
[oecit@localhost ~]$ ./a.out
Enter the port no7500
server in starting
port address is 7500
server
accepting
message from client:hello world
enter your message: hai

client:
[oecit40@localhost ~]$ ./a.out
Enter the port no7500
client in starting
port address is 7500
client module
**********
enter your message: hello world
message from server:hai
EX.NO:6 TRANSFER A FILE USING FTP
DATE:

FTP CLIENT:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main()
{
char x[15],a[15],f[100],c;
int sd,sersock;
struct sockaddr_in servaddr;
sd=socket(AF_INET,SOCK_STREAM,0);
servadd r.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(4455);
servaddr.sin_family=AF_INET;
bzero(&(servaddr.sin_zero),8);
if(connect(sd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr))<0)
{
printf("\n ERROR");
return 0;
}
else
{
printf("\nenter the file to be sent");
scanf("%s",x);
send(sd,x,sizeof(x),0);
printf("\nfile name is sent\n\n");
}
recv(sd,&f,sizeof(f),0);
printf("\nreceivedfile:%s\n\n\n",x,f);
close(sd);
}
FTP SERVER:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main()
{
char x[15],a[15],f[100],c;
FILE*fp;
int n,sd,clisock,size,i=0;
struct sockaddr_in sersock;
struct sockaddr cliaddr;
sd=socket(AF_INET,SOCK_STREAM,0);
sersock.sin_family=AF_INET;
sersock.sin_port=htons(4455);
sersock.sin_addr.s_addr=INADDR_ANY;
bzero(&(sersock.sin_zero),8);
bind(sd,(struct sockaddr*)&sersock,sizeof(sersock));
listen(sd,5);
size=sizeof(struct sockaddr_in);
printf("\nwaiting for receiver to respond------\n");
clisock=accept(sd,&cliaddr,&size);
perror("\nconnection");
recv(clisock,&x,sizeof(x),0);
puts(x);
printf("\nFile name received %s\n\n",x);
fp=fopen(x,"r");
if(fp!=0)
{
while((c=fgetc(fp))!=EOF)
{
f[i]=c;
i++;
}
f[i]='\0';
send(clisock,&f,sizeof(f),0);
printf("\nrequested file sent successfully");
}
else
{
printf("\nenter the error message: \n");
scanf("%s",f);
send(clisock,&f,sizeof(f),0);
printf("\nrequested file notfound!!!!");
}
close(sd);
}
OUTPUT:
CLIENT:
[oecit@localhost ~]$ cc ftpcli.c
[oecit@localhost ~]$./a.out
Enter the file to be sent aa.c
File name is sent
Received file: aa.c

SERVER:
[oecit@localhost ~]$ cc ftpser.c
[oecit@localhost ~]$./a.out
Waiting for the receiver to respond
Connection:success
aa.c
file name is received
enter the error message:bad
requested file not found!!!!!!!!!!!!!
EX.NO:7 REMOTE COMMAND EXECUTION IN C
DATE:

RPC CLIENT:

#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define MAX_LINE (1000)
int main(int argc,char* argv[])
{
int conn_s;
short int port;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
char* szAddress;
char* szPort;
char* endptr;
printf("\nremot host address:%s\nre^Zmoteportno %s\n",argv[1],argv[2]);
szAddress=argv[1];
szPort=argv[2];
port=strtol(szPort,&endptr,0);
if(*endptr)
{
printf("ECHO CLNT:invalidport supplied:\n");
exit(EXIT_FAILURE);
}
if((conn_s=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("ECHOCLNT:error creating listening socket\n");
exit(EXIT_FAILURE);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(port);
if(inet_aton(szAddress,&servaddr.sin_addr)<=0)
{
printf("ECHOCLNT:invalid remote ip address\n");
printf("ECHOCLNT:error calling connect()\n");
exit(EXIT_FAILURE);
}
printf("enter the command to be executed:");
fgets(buffer,MAX_LINE,0);
write(conn_s,buffer,strlen(buffer)+1);
return EXIT_SUCCESS;
}
RPC SERVER:

#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#define MAX_LINE (1000)
int main(int argc,char* argv[])
{
int list_s;
int conn_s;
short int port;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
char* endptr;
printf("\n enter server portno to listen");
scanf("%d",&port);
if((list_s=socket(AF_INET,SOCK_STREAM,0))<0)
{
fprintf(stderr,"ECBOSERV:error creating listeningsocket:\n");
exit(EXIT_FAILURE);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(list_s,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
fprintf(stderr,"ECHOSERV:errorcalling bind()\n");
if(listen(list_s,5)<0)
{
fprintf(stderr,"ECHOSERV:errorcallinglisten()");
exit("EXIT_FAILURE");
}
while(1)
{
if((conn_s=accept(list_s,NULL,NULL))<0)
{
fprintf(stderr,"ECHOSERV:error calling accept()");
exit(EXIT_FAILURE);
}
strcpy(buffer,"");
read(conn_s,buffer,20);
system(buffer);
if(close(conn_s)<0)
{
fprintf(stderr,"ECHOSERV:error calling close\n");
exit(EXIT_FAILURE);
}
}
}
}
OUTPUT:

CLIENT:
[oecit@localhost ~]$ cc remotec.c
[oecit@localhost ~]$ ./a.out 172.16.0.198 7899
Remote host address:172.16.0.198
REmoteport no:7899
Enter the command to be executed:ls

SERVER:
[oecit@localhost ~]$ cc remotes.c
[oecit@localhost ~]$ ./a.out
Enter server port to listen7899
Tcpclient.c tcpserever.c signal.c
E X.NO:8A SIGNAL HANDLER
DATE:

#include<unistd.h>
#include<sys/types.h>
#include<signal.h>
#include<stdio.h>
void inthandler(int signum)
{
printf("\n SIGIMT received");
}
void termhandler(int signum)
{
printf("\n SIGTERM received\n");
}
void conthandler(int signum)
{
printf("\n SIGCONT received\n");
}
int main()
{
signal(SIGINT,inthandler);
signal(SIGTERM,termhandler);
signal(SIGCONT,conthandler);
while(1)
printf("\n program mumbering\n");
return 0;}
OUTPUT:
[oecit@localhost ~]$ cc sig.c
[oecit@localhost ~]$ ./a.out &
[5] 10149
program running
SIGINT received
Program running
SIGTERM received
program running
[oecit40@localhost ~]$ kill 10149
[oecit40@localhost ~]$ kill -SIGkill 10149
EX.NO:8B REGISTERING A COMMON HANDLER AND BLOCKING SIGNAL
DATE:

#include<unistd.h>
#include<sys/types.h>
#include<signal.h>
void sighandler(int signum)
{
switch(signum)
{
case SIGINT:
printf("SIGINT received\n");
break;
case SIGTERM:
printf("SIGTERM received\n");
break;
case SIGCONT:
printf("SIGCONT received\n");
break;
}
}
int main()
{
char buffer[80]="\0";
sigset_t block;
signal(SIGINT,sighandler);
signal(SIGTERM,sighandler);
signal(SIGCONT,sighandler);
sigemptyset(&block);
sigaddset(&block,SIGTERM);
sigaddset(&block,SIGINT);
sigaddset(&block,SIGCONT);
sigprocmask(SIG_BLOCK,&block,NULL);
while(strcmp(buffer,"n")!=0)
{
printf("\r Enter the string");
gets(buffer);
puts(buffer);
}
sigprocmask(SIG_UNBLOCK,&block,NULL);
while(1)
printf("program running\n");
return 0;
}
OUTPUT:
[oecit@localhost ~]$ ./a.out
Enter the string suba
suba
Enter the string kavi
kavi
Enter the string raji
raji
Enter the string n
program running
SIGINT received
Program running
SIGTERM received
program running
EX.NO:9 HANDLING ZOMBIE
DATE:

#include<stdio.h>
#include<unistd.h>
int main(void)
{
int pid;
printf("Hello world\n");
printf("I amtheparent process andpid is:%d\n",getpid());
printf("Here I am before use for forking\n");
pid=fork();
printf("Here I am first after forking");
if(pid==0)
{
printf("I am the child process and pid is:%d\n",getpid());
}
else
printf("I am the parent process and pid is:%d\n",getpid());
if(pid!=0)
{
while(1)
sleep(100);
}
else
{
exit(42);
}
}
OUTPUT:
[oecit@localhost ~]$ ./a.out &
[4] 5588
[oecit@localhost ~]$ Hello world
I amtheparent process andpid is:5588
Here I am before use for forking
Here I am first after forkingI am the child process and pid is:5589
Here I am first after forkingI am the parent process and pid is:5588
ps
PID TTY TIME CMD
5420 pts/35 00:00:00 bash
5511 pts/35 00:00:00 a.out
5525 pts/35 00:00:00 a.out
5560 pts/35 00:00:00 a.out
5588 pts/35 00:00:00 a.out
5589 pts/35 00:00:00 a.out <defunct>
5628 pts/35 00:00:00 ps
[oecit40@localhost ~]$ kill 5588
[oecit40@localhost ~]$ ps
PID TTY TIME CMD
5420 pts/35 00:00:00 bash
5511 pts/35 00:00:00 a.out
5525 pts/35 00:00:00 a.out
5560 pts/35 00:00:00 a.out
5650 pts/35 00:00:00 ps
[4] Terminated ./a.out
EX.NO:10 SIMULATION OF SLIDING WINDOW PROTOCOL
DATE:

CLIENT:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
struct ackmnt
{
char msg[10];
int fno;
}ack;
int main()
{
char y[15];
int sd,clisock,size,frame,i;
struct sockaddr_in ser;
struct sockaddr cli,servaddr;
sd=socket(AF_INET,SOCK_STREAM,0);
ser.sin_family=AF_INET;
ser.sin_port=htons(6543);
ser.sin_addr.s_addr=INADDR_ANY;
bzero(&(ser.sin_zero),8);
if(connect(sd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr))>0)
{
printf("error in connection");
}
else
{
printf("enter no of frames\n");
scanf("%d",&frame);
for(i=0,ack.fno=0;i<frame;i++)
{
printf("enter the info to sent\n");
scanf("%s",&ack.msg);
send(sd,&cli,sizeof(struct ackmnt),0);
recv(sd,&ack.fno,sizeof(struct ackmnt),0);
printf("\nDATA SEND SUCCESSFULLY");
}
strcpy(ack.msg,"eof");
send(clisock,&ack,sizeof(struct ackmnt),0);
}
close(sd);
}
SERVER:

#include<sys/socket.h>
#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<netinet/in.h>
struct ackmnt
{
char msg[10];
int fno;
}ack;
int main()
{
char y[15];
int sd,clisock,size,frame,i;
struct sockaddr_in ser;
struct sockaddr cli,serv;
sd=socket(AF_INET,SOCK_STREAM,0);
ser.sin_family=AF_INET;
ser.sin_port=htons(6543);
ser.sin_addr.s_addr=INADDR_ANY;
bzero(&(ser.sin_zero),8);
bind(sd,(struct sockaddr*)&serv,sizeof(serv));
listen(sd,5);
size=sizeof(struct sockaddr_in);
printf("\nWAITING\n");
clisock=accept(sd,&cli,&size);
perror("connection");
ack.fno=0;
while(strcmp(ack.msg,"eof")!=0)
{
recv(sd,&cli,sizeof(struct ackmnt),0);
printf("\nRECEIVED DATA IS %s\n",ack.msg);
printf("\nSENDING ACKNOWLEDGEMENT\n");
ack.fno=ack.fno+1;
send(clisock,&ack,sizeof(struct ackmnt),0);
}
}
OUTPUT:
CLIENT:
[oecit@localhost ~]$ cc ftpcli.c
[oecit@localhost ~]$ ./a.out

Enter the number of frames: 3


Enter the information to be sent HI

Acknowledgement 1
Data sent successfully
Enter the information to be sent : Hello

Acknowledgement 2
Data sent successfully
Enter the information to be sent : Bye

Acknowledgement 3
Data sent successfully

SERVER:

[oecit@localhost ~]$ cc ftpser.c


[oecit@localhost ~]$ ./a.out

Waiting
Connection success
Received Data is: Hi
Sending Acknowledgement…

Received Data is: Hello


Sending Acknowledgement…

Received Data is: Hello


Sending Acknowledgement…

You might also like