Socket Program For Echo Command: AIM Algorithm
Socket Program For Echo Command: AIM Algorithm
Socket Program For Echo Command: AIM Algorithm
AIM
To create a socket program for echo command.
ALGORITHM
PROGRAM
SERVER
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.hexpl>
#include<stdio.h>
int main(int argc,char**argv)
{
socklen_t len;
int listenfd,connfd,n;
struct sockaddr_in servaddr,cliaddr;
char buff[1024];
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd<0)
perror("can not create a socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(2000);
bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(listenfd,5);
for(;;)
{
len=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&len);
n=read(connfd,buff,1024);
buff[n]=0;
write(connfd,buff,sizeof(buff));
}
close(listenfd);
return();
}
CLIENT
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
int main(int argc,char**argv)
{
socklen_t len;
int sockfd,n;
struct sockaddr_in servaddr,cliaddr;
char buff[1024];
char str[1024];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
perror("unable to create a socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(2000);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
scanf("%s",str);
write(sockfd,str,strlen(str));
n=read(sockfd,buff,1024);
buff[n]=0;
fputs(buff,stdout);
close(sockfd);
return();
}
OUTPUT
RESULT
TCP SOCKET PROGRAMMING
AIM :
ALGORITHM
TCP CLIENT
TCP SERVER
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORT 2020
#define BACKLOG 2
main()
{
int fd,fd1,sin_addr,i=0;
char content[30];
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
if((fd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("Socket() error\n");
exit(-1);
}
bzero((char *)&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(fd,(struct sockaddr*)&server,sizeof(server))==-1)
{
printf("Bind() error");
exit(-1);
}
if(listen(fd,BACKLOG)==-1)
{
printf("listen() error\n");
exit(-1);
}
printf("\n The server is in listening mode……\n");
sin_size=sizeof(client);
if((fd1=accept(fd,(struct sockaddr*)&client,&sin_size))==-1)
{
printf("accept() error\n");
exit(-1);
}
i=0;
printf("Point to point connection, type EXIT to quit……\n");
if(!fork())
while(1)
{
i=recv(fd1,content,30,0);
content[i]='\0';
printf("%s\n",content);
if(!strcmp(content,"EXIT"))
{
printf("Client wants to close..");
break;
}
}
else
while(1)
{
scanf("%s",content);
send(fd1,content,30,0);
if(!strcmp(content,"EXIT"))
{
printf("Write mode on exit..");
break;
}
}
close(fd);
close(fd1);
}
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORT 2020
main()
{
int fd1,i;
char content[30];
struct sockaddr_in client;
if((fd1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("Socket() error");
exit(-1);
}
bzero(&client,sizeof(client));
client.sin_family=AF_INET;
client.sin_port=htons(PORT);
if(connect(fd1,(struct sockaddr*)&client,sizeof(client))==-1)
{
printf("connect() error");
exit(-1);
}
printf("Point to point communication , type EXIT to quit……\n");
i=0;
if(!fork())
while(1)
{
i=recv(fd1,content,30,0);
content[i]='\0';
printf("%s\n",content);
if(!strcmp(content,"EXIT"))
{
break;
}
}
else
while(1)
{
scanf("%s",content);
send(fd1,content,30,0);
if(!strcmp(content,"EXIT"))
{
printf("Write mode on exit..");
break;
}
}
close(fd1);
}
OUTPUT
//server side
//client side
AIM
To create a program to implement remote command execution.
ALGORITHM
PROGRAM
SERVER
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>/*close*/
#include<math.h>
#define SUCCESS 0
#define END_LINE.0x0
#define SERVER_PORT 2234
#define MAX_MSG 100
int main(int argc,char**argv[])
{
FILE*fp;
int sd,i,rc,sen,newSd; socketlen_t cliLen;
struct sockaddr_in cliAddr,serAddr;
char line[1024]char cmd[1024];
bzero(&servAddr,sizeof(servAddr));
/*create socket*/
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
perror("cannot open socket");
return ERROR;
}
/*bind server port*/
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s-addr=hton(INADDR_any);
servaddr.sin_port=htons(SERVER_PORT);
if(bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0);
{
perror("cannot bind port");
return ERROR;
}
while(1)
{
printf("%s:waiting for data on port UDP %u\n",argv[0],SERVER_PORT);
cliLen=sizeof(cliAddr);
rc=recvfrom(sd,line,100,0,(struct sockaddr*)&cliAddr,&cliLen);
line[rc]='\0';
if(rc<0)
{
perror("Cannot read...");
close(newSd);
exit(0);
}
printf("\nReceived message is:%s",line);
sleep(rand()%6);
char temp[]={">y.txt"};
strcat(line,temp);
system(line);
fp=fopen("y.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s",cmd);
sen=sendto(sd,cmd,strlen(cmd)+1,0,(struct sockaddr*)"&cliAddr,cliLen);
if(sen<0)
{
perror("cannot send...");
close(sd);
exit(0);
}
printf("\nMessage sent is:%s",cmd);
}
close(sd);
}
CLIENT
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>/*close*/
#include<stdio.h>
#include<sys/time.h>
#define SERVER_PORT 2234
#define MAX_MSG 100
int main(int argc,char**argv[])
{
long int t1,t2;
int sd,rc,sen,i;
struct sockaddr_in localAddr,serAddr;
struct hostent*h;
char s_msg[20];
char r_msg[20];
if(argc<1)
{
printf("usage:%s<server>\n",argv[0]);
exit(1);
}
servAddr.sin_family=h->haddrtype;
memcpy((char*)&servAddr.sin_addr,h->h_addr_list[0],h->h_length);
servAddr.sin_port=htons(SERVER_PORT);
/*create socket*/
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
perror("cannot open socket");
exit(1);
}
/*bind server port*/
localAddr.sin_family=AF_INET;
localAddr.sin_addr.s-addr=htonl(INADDR_any);
localAddr.sin_port=htons(0);
rc=bind(sd,(struct sockaddr*)&localAddr,sizeof(localAddr));
if(rc<0)
{
printf("%s:cannot bind port TCP %u\n",argv[0],SERVER_PORT);
perror("error");
exit(1);
}
printf("Enter the command:");
scanf("%s",s_msg);
socklen_t len=sizeof(servAddr);
rc=sendto(sd,s_msg,100,0,struct sockaddr*)&servAddr,len);
if(rc<0)
{
perror("Cannot send data...");
close(sd);
exit(1);
}
sen=recvfrom(sd,r_msg,100,0,(struct sockaddr*)&servAddr,&len);
r_msg[sen];
if(sen<0)
{
perror("cannot receive...");
close(sd);
exit(0);
}
while(1)
{
{
sen=recvfrom(sd,r_msg,100,0,(struct sockaddr*)&servAddr,&len);
r_msg[sen];
if(sen<0)
{
perror("cannot receive...");
close(sd);
exit(0);
}
printf("%s\n",r_msg);
}
printf("\n");
close(sd);
return 0;
}
OUTPUT
RESULT
IMPLEMENTATION OF CRC AND HAMMING
CODE FOR ERROR HANDELING
SIMULATION OF ROUTING PROTOCOL
AIM:
To Write a program to simulate routing protocol
ALGORITHM:
PROGRAM:
#include<stdio.h>
//#include<conio.h>
#include<string.h>
int i,j,k,m,e,c,co,len[10];
long int sp;
int weight[10][10],cost[10][10];
long int path[10][10];
int node[10];
char name[10];
void get_node();
void get_wt();
void display_given();
void display_result();
void shortpath(int,int);
void main()
{
int n1,n2;
char ch[2];
//clrscr();
get_node();
get_wt();
do
{
//clrscr();
display_given();
// getch();
//clrscr();
printf("\nEnter the nodes to find the shortest path:");
scanf("%d %d",&n1,&n2);
shortpath(n1,n2);
// getch();
printf("\n\nDo you want to continue....press Y:");
scanf("%s",&ch);
}while(!strcmp(ch,"y"));
// getch();
}
void get_node()
{
printf("\nEnter the no of nodes in the graph:");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("\nEnter the name of the node %d:",i);
scanf("%s",&name[i]);
// name[i]=getch();
printf("%c",name[i]);
// getch();
node[i]=i;
}
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
weight[i][j]=0;
cost[i][j]=0;
path[i][j]=0;
} } }
void get_wt()
{
int node1,node2;
int w;
printf("Enter the no of edges:");
scanf("%d",&e);
for(k=1;k<=e;k++)
{
printf("\nEnter the snode,dnode and wt of the edge %d:",k);
scanf("%d %d %d",&node1,&node2,&w);
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(node[i]==node1&&node[j]==node2)
weight[i][j]=w;
} } }
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(weight[i][j]!=0)
{
cost[i][j]=weight[i][j];
path[i][j]=(i*10)+j;
}
else
cost[i][j]=236;
} } }
void display_given()
{
printf("\nMatrix representation of given graph:\n\t");
for(i=1;i<=m;i++)
printf("\t%c",name[i]);
printf("\n");
for(i=1;i<=m;i++)
{
printf("\t%c",name[i]);
for(j=1;j<=m;j++)
printf("\t%d",weight[i][j]);
printf("\n");
}
void display_result()
{
printf("\nMatrix representation of result cost graph:\n\t");
for(i=1;i<=m;i++)
printf("\t%c",name[i]);
printf("\n");
for(i=1;i<=m;i++)
{
printf("\t%c",name[i]);
for(j=1;j<=m;j++)
{
if(cost[i][j]<236)
printf("\t%d",cost[i][j]);
else
printf("\t%c",236);
}
printf("\n");
}
1 2 3 4
1 0 1 1 0
2 0 0 1 1
3 0 0 0 3
4 0 0 0 0
1 2 3 4
1 ∞ 1 1 ∞
2 ∞ ∞ 1 1
3 ∞ ∞ ∞ 3
4 ∞ ∞ ∞ ∞
1 2 3 4
1 0 12 13 0
2 0 0 23 24
3 0 0 0 34
4 0 0 0 0
Enter node to find shortest path: 1 4
Matrix Representation of Result cost graph:
1 2 3 4
1 ∞ 1 1 2
2 ∞ ∞ 1 1
3 ∞ ∞ ∞ 3
4 ∞ ∞ ∞ ∞
1 2 3 4
1 0 12 13 124
2 0 0 23 24
3 0 0 0 34
4 0 0 0 0
The Shortest path for given nodes:
1→2→4
The weight of the path is:2
RESULT:
Thus the program to simulate OSPF routing protocol was written and
output was verified.