You are on page 1of 21

SOCKET PROGRAM FOR ECHO COMMAND

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 :

To Write a C program to implement TCP client server connection using Sockets.

ALGORITHM

TCP CLIENT

Step 1 : Find the ip address and port number at server.


Step 2 : Create tcp socket.
Step 3 : Connect the socket to server(server must be up and listening for new request).
Step 4 : Sent/receive data with server using the socket.
Step 5 : Close the connection.

TCP SERVER

Step 1 : Find the ip address and port number of server.


Step 2 : Create a TCP server socket.
Step 3 : Find the server socket to server ip and port number.
Step 4 : Accept a new connection from client returns a client socket that represents the
client.
Step 5 :Close the connection.
TCP Server Program

#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);
}

TCP Client Program

#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

The server is in listening mode……


Point to point communication, Type EXIT to quit……
Hi
Good morng..
Have a nice day
Bye…
Client wants to close..
EXIT
Write mode on exit..

//client side

Point to point communication, Type EXIT to quit……


Hi
Good morning..
Have a nice day
Bye..
EXIT
Write mode on exit..
RESULT
Thus the Implementation of TCP Socket programming was completed
successfully.

REMOTE COMMAND EXECUTION

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:

1.Get the number of nodes in the graph.


2.Get the names of the node.
3.Assign the link costs to the graph.
4. get the source node and the node that is to be reached as inputs.
5.select the path from source to the destination whose total cost is lesser than the
others.

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");
}

printf("\nMatrix representation of 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");
}
printf("\nMatrix representation of path 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%ld",path[i][j]);
printf("\n");
}
// getch();
}
void shortpath(int node1,int node2)
{
for(k=1;k<=m;k++)
{
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(cost[i][j]<cost[i][k]+cost[k][j])
cost[i][j]=cost[i][j];
else
{
cost[i][j]=cost[i][k]+cost[k][j];
path[i][j]=(path[i][k]*10)+j;
} } } }
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(node1==node[i]&&node2==node[j])
{
co=cost[i][j];
sp=path[i][j];
} } }
display_result();
printf("\nThe shortest path for given nodes:\n");
c=0;i=1;
while(sp>0)
{
len[i]=sp%10;
c=c+1;
i=i+1;
sp=sp/10;
}
for(i=c;i>1;i--)
{
printf("\t%c",name[len[i]]);
printf("--->");
}
printf("%c",name[len[1]]);
printf("\nThe weight of the path is :%d",co);
}

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");
}

printf("\nMatrix representation of result path 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%ld",path[i][j]);
printf("\n");
}
}
OUTPUT:
Enter the no. of nodes:4
Enter the name of node1:1
Enter the name of node2:2
Enter the name of node3:3
Enter the name of node4:4
Enter the Snode,dnode and Weight of edge 1:1 2 1
Enter the Snode,dnode and Weight of edge 2:1 3 2
Enter the Snode,dnode and Weight of edge 3:3 4 3
Enter the Snode,dnode and Weight of edge 4:2 4 1
Enter the Snode,dnode and Weight of edge 5:2 3 1
Matrix Representation of given graph:

1 2 3 4
1 0 1 1 0
2 0 0 1 1
3 0 0 0 3
4 0 0 0 0

Matrix Representation of cost graph:

1 2 3 4
1 ∞ 1 1 ∞
2 ∞ ∞ 1 1
3 ∞ ∞ ∞ 3
4 ∞ ∞ ∞ ∞

Matrix Representation of path graph:

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 ∞ ∞ ∞ ∞

Matrix Representation of Result path graph:

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.

You might also like