Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.6 kB
5
Indexable
Never
LINK

#include<stdio.h>

#include<conio.h>

#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
 int G[MAX][MAX],i,j,n,u;
 printf("Enter no. of vertices:");
 scanf("%d",&n);
 printf("\nEnter the adjacency matrix:\n");
 
 for(i=0;i<n;i++)
 for(j=0;j<n;j++)
 scanf("%d",&G[i][j]);
 
 printf("\nEnter the starting node:");
 scanf("%d",&u);
 dijkstra(G,n,u);
 
 return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
 int cost[MAX][MAX],distance[MAX],pred[MAX];
 int visited[MAX],count,mindistance,nextnode,i,j;
 
 //pred[] stores the predecessor of each node
 //count gives the number of nodes seen so far
 //create the cost matrix
 for(i=0;i<n;i++)
 for(j=0;j<n;j++)
 if(G[i][j]==0)
 cost[i][j]=INFINITY;
 else
 cost[i][j]=G[i][j];
 
 //initialize pred[],distance[] and visited[]
 for(i=0;i<n;i++)
{
 distance[i]=cost[startnode][i];
 pred[i]=startnode;
 visited[i]=0;
 }
 distance[startnode]=0;
 visited[startnode]=1;
 count=1;
 while(count<n-1)
 {
 mindistance=INFINITY;
 //nextnode gives the node at minimum distance
 for(i=0;i<n;i++)
 if(distance[i]<mindistance&&!visited[i])
 {
 mindistance=distance[i];
 nextnode=i;
 }
 //check if a better path exists through nextnode 
 visited[nextnode]=1;
 for(i=0;i<n;i++)
 if(!visited[i])
 if(mindistance+cost[nextnode][i]<distance[i])
 {
 distance[i]=mindistance+cost[nextnode][i];
 pred[i]=nextnode;
 }
 count++; }
 //print the path and distance of each node
 for(i=0;i<n;i++)
 if(i!=startnode)
 {
 printf("\nDistance of node%d=%d",i,distance[i]);
 printf("\nPath=%d",i);
 j=i;
 do
 { j=pred[j];
 printf("<-%d",j);
 }while(j!=startnode);
 }
}


DISTANCE

#include<stdio.h>

struct node

{
 unsigned dist[20];
 unsigned from[20];
}rt[10];
int main()
{
 int dmat[20][20];
 int n,i,j,k,count=0;
 printf("\nEnter the number of nodes : ");
 scanf("%d",&n);
 printf("\nEnter the cost matrix :\n");
 for(i=0;i<n;i++)
 for(j=0;j<n;j++)
 { 
 scanf("%d",&dmat[i][j]);
 dmat[i][i]=0;
 rt[i].dist[j]=dmat[i][j];
 rt[i].from[j]=j;
 }
 do
 { 
 count=0;
 for(i=0;i<n;i++)
 for(j=0;j<n;j++)
 for(k=0;k<n;k++)
 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=0;i<n;i++)
 {
 printf("\n\nState value for router %d is \n",i+1);
 for(j=0;j<n;j++)
 {
 printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
 }
 }
printf("\n\n");
}


SMTP SER
#include<stdio.h>

#include<fcntl.h>

#include<stdlib.h>
#include<netinet/in.h>
#include<unistd.h>
#include<sys/socket.h>
#define max 1024
int fdl,l;
struct sockaddr_in server,client;
transfer()
{
int n,retval,fd;
char *data;
data=((void *)malloc(max));
fd=open("mailbox",O_RDWR||O_APPEND);
retval=recvfrom(fdl,data,max,0,(struct sockaddr *)&client,&l);
write(fd,data,retval);
printf("\n recevd 1 mail\n");
}
main()
{
int l=0;
server.sin_family=AF_INET;
server.sin_port=htons(6299);
server.sin_addr.s_addr=htonl(INADDR_ANY);
fdl=socket(AF_INET,SOCK_DGRAM,0);
l=sizeof(client);
bind(fdl,(struct sockaddr *)&server,l);
transfer();
}


SMTP CLIENT

#include<sys/types.h>

#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define max 1024
int sockfd,l; struct sockaddr_in server,client;
char *buf,*str;
send_data()
{
char ch;
buf=(void *)malloc(max);
str=(void *)malloc(max);
int fd,retval,len=0;
printf("CC:");
sprintf(str,"\n CC:"); 
len+=strlen(str);
strcat(buf,str);
scanf("%s",str);
len+=strlen(str);
strcat(buf,str);
printf("From:");
sprintf(str,"\nFrom:");
len+=strlen(str);
strcat(buf,str);
scanf("%s",str);
len+=strlen(str);
strcat(buf,str);
printf("\nSubject:");
sprintf(str,"\nSubject:");
len+=strlen(str);
strcat(buf,str);
scanf("%s",str);
len+=strlen(str);
strcat(buf,str);
printf("msg:");
sprintf(str,"\nMsg:");
len+=strlen(str);
strcat(buf,str);
while((ch=getchar())!='!')
{
buf[len]=ch;
len++;
}
buf[len++]='\0';
sendto(sockfd,buf,len,0,(struct sockaddr *)&server,sizeof(struct sockaddr_in));
printf("send one mail");
close(sockfd);
}
main()
{
char ch;
server.sin_family=AF_INET;
server.sin_port=htons(6299);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
client.sin_family=AF_INET;
client.sin_port=htons(6299);
client.sin_addr.s_addr=htonl(INADDR_ANY);
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bind(sockfd,(struct sockaddr *)&client,sizeof(client));
printf("C:compose \n R:read mail \n");
scanf("%c",&ch);
 switch(ch)
{
case 'C':send_data();
break;
case 'R':printf("pls check mailbox \n");
} }