Vous êtes sur la page 1sur 35

Expt No: Date: SIMULATION OF ARP Aim: To write a program for simulation of ARP

Algorithm: 1. Start 2. Address resolution protocol is used to convert ip address to physical address. 3. First step in simulation of ARP is to get the ip address which is in dotted decimal format. 4. The ip address which is in network layer is converted to MAC address which is in hexadecimal format. 5. Thus the simulation of ARP is performed.

Program Code: #include<sys/types.h> #include<sys/socket.h> #include<net/if_arp.h> #include<sys/ioctl.h> #include<stdio.h> #include<unistd.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> #include<string.h> int main(int argc,char *argv[]) { struct sockaddr_in sin={0}; struct arpreq myarp={{0}}; char *ptr; int sd; sin.sin_family=AF_INET; if(inet_aton(argv[1],&sin.sin_addr)==0) { printf("IP address entered '%s' is not valid \n",argv[1]); exit(0); } memcpy(&myarp.arp_pa,&sin,sizeof(myarp.arp_pa)); strcpy(myarp.arp_dev,"etho"); sd=socket(AF_INET,SOCK_DGRAM,0); if(ioctl(sd,SIOCGARP,&myarp)==1) { printf("No entry in arp cache for '%s'",argv[1]); exit(0); } ptr=&myarp.arp_pa.sa_data[0]; printf("MAC address for '%s'",argv[1]); printf("%X:%X:%X:%X:%X: %X",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+3),*(ptr+4),*(ptr+5),*(ptr+6)); return 0; }

Output: [student@localhost ~]$ cc arp.c [student@localhost ~]$ ./a.out 123.23.12.34 MAC address for '123.23.12.34'0:0:7B:17:17:C

Result: Thus, the program for simulation of ARP program was compiled & executed successfully.

Expt No: Date: UDP ECHO SERVER Aim: To echo the message in the client through server using UDP. Algorithm: Client 1. Set client machine address and server port address. 2. Using socket() create socket for server. 3. Read the message given in the client. 4. Send the message to server from client. 5. Receive the same message from the server. 6. Print the message. Server 1. Set server port address using socket(). 2. Create a server by specifying server port. 3. Receive the message from client and resend the message to client. 4. Stop the process.

Program Code: Client #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> #include<arpa/inet.h> #include<stdio.h> #define MAXLINE 1024 int main(int argc,char *argv[]) { int sockfd; int n; socklen_t len; char sendline[1024],recvline[1024]; struct sockaddr_in servaddr; scanf("%s",sendline); sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(4560); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); len=sizeof(servaddr); sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,len); n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL); recvline[n]=0; printf("%s",recvline); return 0; }

Server #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<netdb.h> #include<arpa/inet.h> #define MAXLINE 1024 int main(int argc,char **argv) {

int sockfd; int n; socklen_t len; char msg[1024]; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htons(INADDR_ANY); servaddr.sin_port=htons(4560); bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); for(;;) { len=sizeof(cliaddr); n=recvfrom(sockfd,msg,MAXLINE,0,(struct sockaddr *)&cliaddr,&len); printf("%s",msg); if(n<0) perror("send error"); sendto(sockfd,msg,n,0,(struct sockaddr *)&cliaddr,len); } return 0; }

Output: ./a.out udpserver.c ./a.out udpclient.c Hi Hi

Result: Thus the message was echoed in the client through server using UDP

Expt No: Date: Sliding Window Protocol Aim: To demonstrate sliding window protocol by sending frames and acknowledging it.

Algorithm: 1. 2. 3. 4. 5. 6. 7. Initialize the temporary variables to 0 and the size of the sliding window is 8; Get the number of frames. Send each frame to the receiver. The function simulate() and receive() generate random numbers. As each frame is sent the window size is decreased. After all the frames are sent to the receiver , the receiver sends an acknowledgement. Thus sliding window protocol was demonstrated.

Program Code:

#include<stdio.h> #include<stdlib.h> int main() { int temp1,temp2,temp3,temp4,winsize=8,noframes,moreframes; int receive(int); int simulate(int); int i; temp1=0; temp2=0; temp3=0; temp4=0; for(i=0;i<=200;i++) rand(); noframes=5; printf("\n Number of frames: %d\n",noframes); moreframes=noframes; while(moreframes>0) { temp1=simulate(winsize); winsize=temp1; temp4+=temp1; if(temp4>=noframes) for(i=temp3+1;i<=temp4;i++) printf("\n Sending frame %d",i); temp2=receive(temp1); temp3+=temp2; temp3=noframes; printf("\n Acknowledgement for the frames upto %d\n",temp3); moreframes=temp2; temp4=temp3; if(winsize<=0) winsize=8; } printf("\nEnd of sliding window protocol\n"); } int receive(int temp1) { int i; for(i=0;i<100;i++) rand(); i=rand()%temp1; return i; } int simulate(int winsize)

{ int temp1,i; for(i=0;i<50;i++) temp1=rand(); if(temp1==0) temp1=simulate(winsize); i=temp1%winsize; if(i==0) return winsize; else return temp1%winsize; }

Output: Number of frame is 5 Sending frame 1 Sending frame 2 Sending frame 3 Sending frame 4 Sending frame 5 Acknowledgement for the frames upto 5 End of sliding window protocol Result: Thus sliding window protocol was demonstrated successfully.

EX NO:

DATE: BIT STUFFING AIM To perform bit stuffing on the given string to be transmitted ALGORITHM 1.Get the data to be transmitted. 2.Read the data from left to right. 3.If five consecutive 1's are present,then flag pattern is detected. 4.Insert a zero and send the data to the receiver. 5.The data received is displayed after removing the inserted 0's.

PROGRAM CODE

#include<stdio.h> #include<string.h> void main() { char data[100],recv[100]; int k=0,n,i,j,c=0; printf("\n\n\t***sender***\n"); printf("\nenter the data to b transmitted"); scanf("%s",data); for(i=0;i<strlen(data);i++) { if(data[i]=='1') c++; else if(data[i]=='0') c=0; if(c==5) { for(j=strlen(data);j>=(i+1);j) data[j+1]=data[j]; data[i+1]='0'; } } printf("\n\n\n.... flag pattern detected!!!"); printf("\n\n the bit stuffed data"); printf("%s",data); printf("\n\n\t***receiver***\n"); printf("\n\nthe data received is"); strcpy(recv,data); for(i=0;i<strlen(recv);i++) {if(recv[i]=='1') k++; else if(recv[i]=='0') k=0; if(k==5) { n=i+1; k=0; for(j=n;j<strlen(recv);j++) recv[j]=recv[j+1]; }} printf("%s",recv); }

OUTPUT [student@localhost ~]$ ./a.out bit.c ***sender*** enter the data to b transmitted10011111100 .... flag pattern detected!!! the bit stuffed data100111110100 ***receiver*** the data received is10011111100

RESULT Thus bit stuffing was performed successfully.

Ex. No.

Date: CYCLIC REDUNDANCY CHECK

AIM: To perform cyclic redundancy check on the given dividend. ALGORITHM: 1. Get the dividend and the divisor. 2. Calculate the string length of the divisor as n. 3. Append n1 0's to the dividend. 4. Perform binary division and get the remainder. 5. Remove n1 0's from the dividend and append the remainder. 6. Perform binary division on the dividend. 7. If the remainder is 0,then there is no error,else there is error. 8. Print the result. 9. Stop.

PROGRAM CODE:

#include<stdio.h> #include<string.h> char rem[30],par[20],div[20],divd[20]; int f=1,c,a=0,n,i,s,m,j,k,p,e=0; int main() { void exor(); void division(); printf("\n enter the divisor"); scanf("%s",&div); printf("\n enter the dividend"); scanf("%s",&divd); n=strlen(div); m=strlen(divd); printf("\n"); j=m; k=n1; p=m1; for(i=0;i<k;i++) { divd[j]='0'; j++; } printf("added divd=%s",divd); division(); j=m; printf("\n final remainder"); for(i=0;i<k;i++) { divd[j]=rem[i+1]; j++; } printf("\n Data transmitted \n CRC data that is transmitted is %s",divd); division(); printf("\n Data received \n Checking received data\n CRC remainder"); for(i=0;i<k;i++) { printf("%c",rem[i]); e++; } for(i=0;i<e;i++) {

if(rem[i]=='0') continue; else { printf("\n error is detected\n"); goto xy; } } printf("no error"); xy: return 0; } void exor() { for(i=0;i<n;i++) { if(rem[i+1]==par[i]) rem[i]='0'; else rem[i]='1'; } } void division() { for(i=0;i<n;i++) { if(divd[i]==div[i]) rem[i]='0'; else rem[i]='1'; c=ai; } for(s=0;s<p;s++) { rem[i]=divd[a+1]; a++; if(rem[1]=='0') { for(i=0;i<n;i++) par[i]='0'; exor(); } else

{ for(i=0;i<n;i++) par[i]=div[i]; exor(); }} }

OUTPUT: [student@localhost ~]$ ./a.out crc.c enter the divisor10 enter the dividend1010 added divd=10100 final remainder Data transmitted CRC data that is transmitted is 10100 Data received Checking received data CRC remainder0 no error

RESULT: Thus cyclic redundancy check was performed successfully.

EXNO: DATE:

MULTICLIENT DATE TIME SERVER

AIM: To create a client server program to get time and date information using the multiclient date time server.

ALGORITHM: Server Declare the necessary arrays and variables. Set server port address using socket(). Get the current date and time. Connect to the client. Stop the process.

Client Set the client machine address. Connect to the server. Read from the server the current date and time. Display the current date and time. Close the connection.

PROGRAM:

Server #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<time.h> #include<string.h> #define MAX_BUF 30 #define PORT 2100 int main() {int sersoc,clisoc; int conn,len,wri; char str[MAX_BUF]; pid_t pid; time_t ticks; socklen_t clilen; struct sockaddr_in servaddr; struct sockaddr_in cliaddr; servaddr.sin_family=AF_INET; servaddr.sin_port=htons(PORT); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); if((sersoc=socket(AF_INET,SOCK_STREAM,0))<0) { perror("Socket Error"); exit(0); } if(bind(sersoc,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) { perror("Bind Error"); exit(0); } listen(sersoc,10); for(;;) {len=sizeof(cliaddr); conn=(accept(sersoc,(struct sockaddr *)&clisoc,&len)); if((pid=fork())==0) {close(sersoc); ticks=time(NULL); strcpy(str,ctime(&ticks)); if((wri==(write(conn,str,sizeof(str),0)))<0) {printf("Write Error"); exit(0); } close(conn);

exit(0);} close(conn);} close(sersoc); return 0; } Client #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<time.h> #include<arpa/inet.h> #define CLI_PORT 2100 #define BUFF_SIZE 30 int main(int argc,char **argv) {int clisoc,IPPORT_IP,re; char recbuff[BUFF_SIZE]; struct sockaddr_in cliaddr; bzero(&cliaddr,sizeof(cliaddr)); cliaddr.sin_family=AF_INET; cliaddr.sin_port=htons(2100); cliaddr.sin_addr.s_addr=inet_addr(argv[1]); if((clisoc=socket(AF_INET,SOCK_STREAM,0))<0) {perror("Socket Error"); exit(0);} if((connect(clisoc,(struct sockaddr *)&cliaddr,sizeof(cliaddr)))<0) {perror("connect error"); exit(0);} if((re=(read(clisoc,recbuff,sizeof(recbuff),0)))<0) {printf("Read Error"); exit(0);} printf("The current date and time :%s \n",recbuff); close(clisoc); return 0; }

OUTPUT: Server [root@localhost ~]# ./a.out Client [root@localhost ~]# ./a.out 127.0.0.1 The current date and time :Mon Sep 10 17:42:56 2007

RESULT: The program was sucessfully created and executed.

DNS USING TCP EX NO: DATE:

AIM: To create a program to implement DNS using TCP.

ALGORITHM: Client. 1.Create an instance of the sockaddr_in for getting the IP address. 2.Create a socket for the client using the socket() function. 3.Get the domain name from the user. 4.Write the domain name on the socket using the write() function. 5.When the client recieves the domain name, read it using the read() function. Server. 1.Specify IP address of the server by socket() function. 2.To bind the server to the socket, use the bind() function. 3.Create listening sockets using the listen() function. 4.Write the domain to the buffer using the write() function. 5.Close the connection after writing.

PROGRAM CODE: Server. #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define TCP_SERV_PORT 8900 #include<stdio.h> #define SA struct sockaddr #include<stdlib.h> #include<netdb.h> int main() { int listenfd,connfd; socklen_t len; struct sockaddr_in servaddr,cliaddr; struct hostent *hp,tp; char host[50],buff[100]; listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(8900); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); bind(listenfd,(SA *)&servaddr,sizeof(servaddr)); listen(listenfd,10); for(; ;) { len=sizeof(cliaddr); connfd=accept(listenfd,(SA *)&cliaddr,&len); read(connfd,host,sizeof(host)); printf("%s",host); if((hp=gethostbyname(host))==NULL) printf("cant get address"); if(inet_ntop(AF_INET,hp->h_addr,buff,sizeof(buff))<=0) printf("host address not available"); printf("%s",buff); write(connfd,buff,strlen(buff)+1); close(connfd); } return 0; }

Client. #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<netdb.h> #include<stdlib.h> #define SA struct sockaddr int main(int argc,char ** argv) { socklen_t len; struct sockaddr_in servaddr; struct hostent tp; char buff[100],host[50]; int sockfd; sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(8900); servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))<0) printf("connect error"); printf("enter the domain name"); scanf("%s",host); write(sockfd,host,strlen(host)+1); if(read(sockfd,buff,sizeof(buff))<0)m printf("read error"); printf("the host address %s",buff); return 0; }

OUTPUT Server. [student@karthik ~]$ cc dnss.c [student@karthik ~]$ ./a.out Client. [student@karthik ~]$ cc dnsc.c [student@karthik ~]$ ./a.out enter the domain name localhost.localdomain the host address 127.0.0.1

RESULT: Thus the program for DNS using TCP was created and executed successfully.

FILE TRANSFER PROTOCOL EXNO: DATE: AIM: To transfer a file using FTP between the server and the client systems. ALGORITHM: Server 1. 2. 3. 4. 5. 6. 7. Set server port address. Using socket function, create a socket for server by specifying server port. Allocate a buffer size. Use TCP socket for sending messages from server to client. Use accept() method to read file by server. Use the input method to read file. Write file to the client.

Client 1. 2. 3. 4. 5. Set client machine address and server port address. Using socket function, create socket for server. Allocate buffer use TCP for receiving message from server to client. Read the file from the server. Read the output from the system.

PROGRAM CODE: Server #include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<sys/socket.h> #include<errno.h> #define SIZE 256 int main() { int srFd,CIFD,len; struct sockaddr_in server,client; char buf[SIZE],buf1[SIZE]; int n,j; FILE *fp,*fp1; srFd=socket(AF_INET,SOCK_STREAM,0); if(srFd<0) { printf("\n Socket error"); exit(0); } bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(2200); server.sin_addr.s_addr=htons(INADDR_ANY); if(bind(srFd,(struct sockaddr *)&server,sizeof(server))<0) { printf("\n server : bind error"); close(srFd); exit(0); } if(listen(srFd,1)<0) { printf("\n Listen Error"); close(CIFD); exit(0); } printf("\n Server is ready to listen\n"); len=sizeof(client); CIFD=accept(srFd,(struct sockaddr *)&client,&len); if(CIFD<0) { printf("\n Accept error"); close(CIFD);

close(srFd); exit(0); } printf("Client gets connected\n"); bzero(&buf,sizeof(buf)); if((n=recv(CIFD,&buf,SIZE,0))<0) { printf("\n Receive Error in Server\n"); close(srFd); close(CIFD); exit(0); } buf[n-1]=NULL; printf("\n File name %s",buf); if((n=send(CIFD,buf,strlen(buf),0))<0) { printf("\n Error"); close(CIFD); exit(0); } close(srFd); close(CIFD); exit(0); }

Client #include<stdio.h> #include<netinet/in.h> #include<sys/socket.h> #include<sys/types.h> #include<string.h> #define SIZE 256 int main() { int CIFD; struct sockaddr_in client; char buf[SIZE]; int n; if((CIFD=socket(AF_INET,SOCK_STREAM,0))<0) { printf("\n Client Socket Error"); exit(0); } bzero(&client,sizeof(client)); client.sin_family=AF_INET; client.sin_port=htons(2200); inet_pton(AF_INET,"127.0.0.1",&client.sin_addr); if(connect(CIFD,(struct sockaddr *)&client,sizeof(client))<0) { printf("\n connection failed"); close(CIFD); exit(0); } printf("\n connection enabled\n"); printf("\n Enter source file\n"); bzero(&buf,sizeof(buf)); if(fgets(buf,SIZE,stdin)==NULL) { printf("\n Failed to get source file name in client\n"); close(CIFD); exit(0); } if(send(CIFD,buf,strlen(buf),0)<0) { printf("\n Send Error\n"); close(CIFD); exit(0); } printf("\n Client message sent\n"); bzero(&buf,sizeof(buf));

if((n=recv(CIFD,buf,SIZE,0))<0) { printf("\n File name receiver error in client from server\n"); close(CIFD); exit(0); } //buf[n]=NULL; printf("\n Destination file name from server %s",buf); buf[n]=NULL; close(CIFD); exit(0); } OUTPUT: Client [student@localhost ~]$ ./a.out Connection Enabled Enter Source File Tps.c Client Message Sent Destination Filename From Server tps.c Server [student@localhost ~]$ ./a.out Server is ready to listen Client get connected Tps.c

RESULT: Thus the program for FTP was created and executed successfully.

TCP CHAT Expt No: Date:

Aim: To write a program using TCP chat for establishing communication between the server & client. Algorithm: 1. 2. 3. 4. 5. 6. Start. The sender must send the message to the receiver through the network. The sender message must terminate with a bye string. As and when the receiver receives tne message bye the control shifts to the receiver. The receiver can now create a new message to be sent to the destinstion ie the sender. Thus the chat program follows recursively.

Program Code: // TCP Server #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #include<strings.h> #define SERV_TCP_PORT 5001 int main(int argc,char *argv[]) { int sockfd,newsockfd,clength; struct sockaddr_in serv_addr,cli_addr; char buffer[4096]; sockfd=socket(AF_INET,SOCK_STREAM,0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=INADDR_ANY; serv_addr.sin_port=htons(SERV_TCP_PORT); bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); listen(sockfd,5); clength=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength); while(strcmp(buffer,"bye\n")!=0)

{ bzero(buffer,4096); read(newsockfd,buffer,4096); printf("\nclient:%s",buffer); bzero(buffer,4096); printf("\nserver:"); fgets(buffer,4096,stdin); write(newsockfd,buffer,4096); } close(sockfd); return 0; }

// TCP Client: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #include<strings.h> #define SERV_TCP_PORT 5001 int main(int argc,char *argv[]) { int sockfd; struct sockaddr_in serv_addr; struct hostent *server; char buffer[4096]; sockfd=socket(AF_INET,SOCK_STREAM,0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); serv_addr.sin_port=htons(SERV_TCP_PORT); connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); printf("\nEnter your message to send..\n"); while(strcmp(buffer,"bye\n")!=0) { bzero(buffer,4096); printf("\nClient");

fgets(buffer,4096,stdin); write(sockfd,buffer,4096); bzero(buffer,4096); read(sockfd,buffer,4096); printf("\nServer:%s",buffer); } close(sockfd); return 0; }

Output: // Server: [student@localhost ~]$ ./a.out hi client: hi server:hi client:hi server: client:bye server:bye [student@localhost ~]$ // Client: [student@localhost ~]$ ./a.out Enter your message to send.. Client:hi Server:hi Client:hi Server:hi Client:bye Server:bye [student@localhost ~]$

Result: Thus, the program for TCP chat was compiled & executed successfully.

Vous aimerez peut-être aussi