0% found this document useful (0 votes)
111 views10 pages

Chat Application

The document contains code for a DNS lookup program that allows a user to lookup an IP address given a domain name or lookup a domain name given an IP address by searching a DNS.txt file containing domain name and IP address mappings. The program prompts the user to select forward or backward lookup, gets the domain name or IP as input, opens and searches the DNS.txt file, and prints the result if found or an error if not found. Sample output showing lookups of www.yahoo.com by name and IP are also included.

Uploaded by

Mohamed Fameen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views10 pages

Chat Application

The document contains code for a DNS lookup program that allows a user to lookup an IP address given a domain name or lookup a domain name given an IP address by searching a DNS.txt file containing domain name and IP address mappings. The program prompts the user to select forward or backward lookup, gets the domain name or IP as input, opens and searches the DNS.txt file, and prints the result if found or an error if not found. Sample output showing lookups of www.yahoo.com by name and IP are also included.

Uploaded by

Mohamed Fameen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

CHAT APPLICATION SERVER #include<stdio.h> #include<netdb.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.

h> #define MAX 100 #define PORT 43454 #define SA struct sockaddr void func(int sockfd) { char buff[MAX]; int n,clen; struct sockaddr_in cli; clen=sizeof(cli); for(;;) { bzero(buff,MAX); recvfrom(sockfd,buff,sizeof(buff),0,(SA*)&cli,&clen); printf("\t From client:%s \t To client:",buff); bzero(buff,MAX); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,sizeof(buff),0,(SA*)&cli,clen); if(strncmp("exit",buff,4)==0) { printf("\n server exit...\n"); break; }}} int main() { int sockfd; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("\n socket creation failed"); exit(0); } else printf("\n socket successfully created...\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(PORT); if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0) {

printf("\n socket bind falied...\n"); exit(0); } else printf("\n socket successfully binded...\n"); func(sockfd); close(sockfd); } CLIENT #include<stdio.h> #include<netdb.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> #define MAX 100 #define PORT 43454 #define SA struct sockaddr int main() { char buff[MAX]; int sockfd,len,n; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("\n socket creation failed...\n"); exit(0); } else printf("\n socket successfully created...\n"); bzero(&servaddr,sizeof(len)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); servaddr.sin_port=htons(PORT); len=sizeof(servaddr); for(;;) { printf("\n enter string:"); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,sizeof(buff),0,(SA*) &servaddr,len); bzero(buff,sizeof(buff)); recvfrom(sockfd,buff,sizeof(buff),0,(SA*) &servaddr,&len); printf("\n from server:%s \n",buff); if(strncmp("exit",buff,4)==0) {

printf("\n client exit...\n"); break; } } close(sockfd); }

CHAT APPLICATION OUTPUT: Server: Socket successfully created Socket successfully binded From client: hi To client : how are you From client : exit Server exit Client: Socket successfully created Enter string : hi From server : how are you Enter string : exit From server : exit Client exit

HTTP: Program coding: #include<stdio.h> #include<stdlib.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h> #include<sys/types.h> #include<sys/socket.h> #define size 100 int main(int argc, char *argv[]) { struct sockaddr_in sock; struct hostent *hp; char *req,*hostname,*cp; FILE *local; char buff[size]; int con,i,l,nrv,sd; if(argc!=3) { printf(\nUsage: %s<server>filename,argv[0]); exit(1); } if(cp=strchr(argv[1],/)) { *cp=\0; l=strlen(argv[1]); hostname=malloc(l+1); strcpy(hostname,argv[1]); *cp=/; l=strlen(cp); req=malloc(l+1); strcpy(req,cp); } else { hostname=argv[1]; req=/; } printf(\nHost=%s\nReq= %s,hostname,req); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) { exit(1);

} bzero(&sock,sizeof (sock)); sock.sin_family=AF_INET; con=inet_pton(AF_INET, argv[1], &sock); sock.sin_port=htons(80); con=connect(sd,(struct sockaddr *)&sock, sizeof (sock)); if(con<0) { perror(\nConnection failed); exit(1); } sprintf(buff,Get HTTP:%s//1.1\r\nHost: %s\r\nConnection: class\r\n\r, req, hostname); printf(Buff=%s\n, buff); l=strlen(buff); local=fopen(argv[2],w); write(sd,buff,1); do { nrv=read(sd,buff,size); if(nrv>0) { for(i=0;i<nrv;i++) putc(buff[i],local); } else break; } while(1); close(sd); fclose(local); return 0; }

Output: cc webpage.c ./a.out http:/197.168.2.3/z :hi.txt hello.txt Host=http: Connection failed: Connection refused Req= /197.168.2.3/z :hi.txt

DNS: Program coding: #include<stdio.h> int main() { int i,flag=0; char buf[80],s1[80],s2[80]; FILE *fp; printf("\nSelect Choice \n\t1.Forward Mode\n\t2.Backword Mode"); printf("\nYour choice is : "); scanf("%d",&i); switch(i) { case 1: printf("\nEnter Name of Server : "); scanf("%s",buf); fp=fopen("dns.txt","r"); while(!feof(fp)) { fscanf(fp,"%s",s1); fscanf(fp,"%s",s2); if((strcmp(s1,buf))==0) { printf("\nThe IP address of Server is : %s\n\n",s2); flag=1; } bzero(s1,80); bzero(s2,80); } if(flag==0) { printf("\nInvalid choice..!\n"); } flag=0; bzero(buf,80); fclose(fp); break; case 2: printf("\nEnter IP address of server : "); scanf("%s",buf); fp=fopen("dns.txt","r"); while(!feof(fp)) { fscanf(fp,"%s",s1);

fscanf(fp,"%s",s2); if((strcmp(s2,buf))==0) { printf("\nThe Name of Server is : %s\n\n",s1); flag=1; } bzero(s1,80); bzero(s2,80); } if(flag==0) { printf("\nInvalid choice..!\n"); } flag=0; bzero(buf,80); fclose(fp); break; default : printf("\nYou have entered invalid choice!"); exit(1); } return 0; }

DNS.txt: www.yahoo.com 123.45.67.89 www.facebook.com 45.234.56.11 www.yebhi.com 221.223.245.244 OUTPUT: Select choice 1.forward mode 2.backward mode Your choice is: 1 Enter the name of the server: www.yahoo.com The IP address of the server is: 123.45.67.89 Select choice 1.forward mode 2.backward mode Your choice is: 2 Enter the IP address of the server:123.45.67.89 Name of the server is: www.yahoo.com

You might also like