Simple
network management protocol
Agent1 :
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{
int i,sd,sd2,nsd,clilen,sport,len;
char sendmsg[20],recvmsg[100];
char
oid[5][10]={"client1","client2","client3","cleint4","client5"};
char
wsize[5][5]={"5","10","15","3","6"};
struct sockaddr_in servaddr,cliaddr;
printf("I'm the Agent - TCP Connection\n");
printf("\nEnter the Server port");
printf("\n_____________________\n");
scanf("%d",&sport);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("Can't
Create \n");
else
printf("Socket
is Created\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(sport);
sd2=bind(sd,(struct
sockaddr*)&servaddr,sizeof(servaddr));
if(sd2<0)
printf("
Can't Bind\n");
else
printf("\n Binded\n");
listen(sd,5);
clilen=sizeof(cliaddr);
nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
if(nsd<0)
printf("Can't Accept\n");
else
printf("Accepted\n");
recv(nsd,recvmsg,100,0);
for (i=0;i<5;i++)
{
if(strcmp(recvmsg,oid[i])==0)
{
send(nsd,wsize[i],100,0);
break;
}
}
}
Agent 2 :
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{
int i,sd,sd2,nsd,clilen,sport,len;
char sendmsg[20],recvmsg[100];
char
oid[5][10]={"System1","System2","System3","System4","System5"};
char
mdate[5][15]={"1-10-095","10-03-08","14.03.81","11.07.07","17.12.77"};
char time[5][15]={"9am","10pm","11am","12.30pm","11.30am"};
struct sockaddr_in servaddr,cliaddr;
printf("Enter the Server port");
printf("\n_____________________\n");
scanf("%d",&sport);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("Can't Create \n");
else
printf("Socket is Created\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(sport);
sd2=bind(sd,(struct
sockaddr*)&servaddr,sizeof(servaddr));
if(sd2<0)
printf("
Can't Bind\n");
else
printf("\n Binded\n");
listen(sd,5);
clilen=sizeof(cliaddr);
nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
if(nsd<0)
printf("Can't Accept\n");
else
printf("Accepted\n");
recv(nsd,recvmsg,100,0);
for(i=0;i<5;i++)
{ if(strcmp(recvmsg,oid[i])==0)
{
send(nsd,mdate[i],100,0);
send(nsd,time[i],100,0);
break;
}
}
}
Manager :
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
main()
{
int csd,cport,len,i;
char sendmsg[20],rcvmsg[100],rmsg[100],oid[100];
struct sockaddr_in servaddr;
printf("Enter the port\n");
scanf("%d",&cport);
csd=socket(AF_INET,SOCK_STREAM,0);
if(csd<0)
printf("Can't Create\n");
else
printf("Scocket is Created\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(cport);
if(connect(csd,(struct
sockaddr*)&servaddr,sizeof(servaddr))<0)
printf("Can't Connect\n");
else
printf("Connected\n");
printf("\n 1.TCP Connection\n");
printf("\n 2. System \n");
printf("Enter the number for the type of informtion
needed....\n");
scanf("%d",&i);
if(i==1)
{
printf("Enter the Object ID for Client\n");
scanf("%s",oid);
send(csd,oid,100,0);
recv(csd,rmsg,100,0);
printf("\n The window size of %s is %s",oid,rmsg);
}
else
{
printf("\nEnter the Object ID for the System\n");
scanf("%s",oid);
send(csd,oid,100,0);
recv(csd,rmsg,100,0);
printf("\nThe Manufacturing date for %s is %s",oid,rmsg);
recv(csd,rmsg,100,0);
printf("\nThe time of Last Utilization for %s is
%s",oid,rmsg);
}
}
OUTPUT:
Agent1 :
[mecse8@localhost mecse8]$ cc agent1.c -o a1
[mecse8@localhost mecse8]$ ./a1
I'm the Agent - TCP Connection
Enter the Server port
65232
Socket is Created
Binded
Accepted
Manager:
[mecse8@localhost mecse8]$ ./m
Enter the port
6523
Scocket is Created
Connected
1.TCP Connection
2. System
Enter the number for the type of informtion needed....
1
Enter the Object ID for Client
client1
The window size of
client1 is 5
Agent 2:
[mecse8@localhost mecse8]$ ./a2
Enter the Server port
6523
Socket is Created
Binded
Accepted
Manager :
[mecse8@localhost mecse8]$ ./m
Enter the port
6523
Scocket is Created
Connected
1.TCP Connection
2. System
Enter the number for the type of informtion needed....
2
Enter the Object ID for the System
System3
The Manufacturing date for System3 is 14.03.81
The time of Last Utilization for System3 is 11am
No comments:
Post a Comment