Beruflich Dokumente
Kultur Dokumente
// UDP header's structure struct udpheader { unsigned short int udph_srcport; unsigned short int udph_destport; unsigned short int udph_len; unsigned short int udph_chksum; }; // Function for checksum calculation. unsigned short csum(unsigned short *buf, int nwords) { // unsigned long sum; for(sum=0; nwords>0; nwords--) sum += *buf++; sum = (sum >> 16) + (sum &0xffff); sum += (sum >> 16); return (unsigned short)(~sum); } int main(int argc, char *argv[]) { int sd; char buffer[PCKT_LEN]; // Our own headers' structures struct ipheader *ip = (struct ipheader *) buffer; struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader)); // Source and destination addresses: IP and port struct sockaddr_in sin, din; int one = 1; const int *val = &one; memset(buffer, 0, PCKT_LEN); // Create a raw socket with UDP protocol sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); if(sd < sin_family =" AF_INET;" sin_family =" AF_INET;" sin_port =" htons(atoi(argv[2]));" sin_port =" htons(atoi(argv[4]));" s_addr =" inet_addr(argv[1]);" s_addr =" inet_addr(argv[3]);">iph_ihl = 5; ip->iph_ver = 4; ip->iph_tos = 16; ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader); ip->iph_ident = htons(54321); ip->iph_ttl = 64; // hops ip->iph_protocol = 17; // UDP ip->iph_sourceip = inet_addr(argv[1]);//Source IP address ip->iph_destip = inet_addr(argv[3]);// destination IP address
// Fabricate the UDP header. udp->udph_srcport = htons(atoi(argv[2]));//source port udp->udph_destport = htons(atoi(argv[4]));//destination port udp->udph_len = htons(sizeof(struct udpheader)); // Calculate the checksum ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader)); if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < count =" 1;">iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("sendto() error"); exit(-1); } else { printf("Count #%u - sendto() is OK.\n", count); sleep(2); } } close(sd); return 0; } Posted by G.Vivek Venkatesh at 7:53 PM 0 comments Labels: Networks Lab
simp.x
#define VERSION_NUMBER 1 %#define foo 127
program SIMP_PROG { version SIMP_VERSION { int ADD(operands) = 1; int SUB(operands) = 2; } = VERSION_NUMBER; } = 555555555;
simpclient.c
/* RPC client for simple addition example */
#include "stdio.h" #include "simp.h" /* Created for us by rpcgen - has everything we need ! */
int add( CLIENT *clnt, int x, int y) { operands ops; int *result;
/* Gather everything into a single data structure to send to the server */ ops.x = x; ops.y = y;
/* Call the client stub created by rpcgen */ result = add_1(&ops,clnt); if (result==NULL) { fprintf(stderr,"Trouble calling remote procedure\n"); exit(0); } return(*result); }
int sub( CLIENT *clnt, int x, int y) { operands ops; int *result;
/* Gather everything into a single data structure to send to the server */ ops.x = x; ops.y = y;
/* Call the client stub created by rpcgen */ result = sub_1(&ops,clnt); if (result==NULL) { fprintf(stderr,"Trouble calling remote procedure\n"); exit(0); } return(*result); }
int main( int argc, char *argv[]) { CLIENT *clnt; int x,y; if (argc!=4) { fprintf(stderr,"Usage: %s hostname num1 num\n",argv[0]); exit(0); }
/* Create a CLIENT data structure that reference the RPC procedure SIMP_PROG, version SIMP_VERSION running on the host specified by the 1st command line arg. */
/* Make sure the create worked */ if (clnt == (CLIENT *) NULL) { clnt_pcreateerror(argv[1]); exit(1); }
simpservice.c
/* Definition of the remote add and subtract procedure used by simple RPC example rpcgen will create a template for you that contains much of the code
needed in this file is you give it the "-Ss" command line arg. */
#include "simp.h"
/* Here is the actual remote procedure */ /* The return value of this procedure must be a pointer to int! */ /* we declare the variable result as static so we can return a pointer to it */
int * add_1_svc(operands *argp, struct svc_req *rqstp) { static int result; printf("Got request: adding %d, %d\n", argp->x, argp->y); result = argp->x + argp->y; return (&result); }
printf("Got request: subtracting %d, %d\n", argp->x, argp->y); result = argp->x - argp->y; return (&result); }
Server
vivek@ubuntu~$ rpcgen -C -a simp.x vivek@ubuntu~$ cc -o server simpservice.c simp_svc.c simp_xdr.c vivek@ubuntu~$ ./server Got request: adding 4, 5 Got request: subtracting 4, 5
Client
Program sender.c (server) #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h"
main() { int sd,i,r,bi,nsd,port,frame,prev_frame=0,count=0;; char ack[5],buff[30]; struct sockaddr_in ser,cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } printf("\n Socket created\n"); bzero((char*)&cli,sizeof(ser)); printf("ENTER PORT NO:\n"); scanf("%d",&port); printf("\n Port Address is %d\n:",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY); bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser)); if(bi==-1) { printf("\n bind error, port busy, plz change port in client and server"); return 0; } i=sizeof(cli); listen(sd,5); nsd = accept(sd,((struct sockaddr *)&cli),&i); if(nsd==-1) { printf("\ncheck the description parameter\n"); return 0; } printf("\nConnection accepted."); while(count<5) { ph: printf("\nSending FRAME %d to the Receiver...\n",prev_frame); snprintf(buff,sizeof(buff),"%d",prev_frame); send(nsd,buff,30,0); r = recv(nsd,ack,5,0); if(strcmp(ack,"ack")==0 || strcmp(ack,"ACK")==0) { count++; if(prev_frame==0) prev_frame=1; else prev_frame = 0; }
else if(strcmp(ack,"nak")==0 || strcmp(ack,"NAK")==0) { printf("\n NAK: So again sending the Previous frame...\n"); goto ph; } } printf("\n Bye.."); send(nsd,"EOF",4,0); close(sd); close(nsd); return 0; } receiver.c (client) #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" int main() { int sd,con,port,i; char content[30],ack[3]; struct sockaddr_in cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } bzero((char*)&cli,sizeof(cli)); cli.sin_family = AF_INET; printf("ENTER PORT NO"); scanf("%d",&port); cli.sin_port=htons(port); cli.sin_addr.s_addr=htonl(INADDR_ANY); con=connect(sd,(struct sockaddr*)&cli,sizeof(cli)); if(con==-1) { printf("\n connection error"); return 0; } i=recv(sd,content,30,0); while(strcmp(content,"EOF") != 0)
{ printf("Received from Sender: Frame %s \n",content); ph: printf("\n Acknowledgement(ACK/NAK) : "); scanf("%s",ack); if(!(strcmp(ack,"ack")==0 || strcmp(ack,"nak")==0 || strcmp(ack,"ACK")==0 || strcmp(ack,"NAK")==0)) { printf("\n Not a valid Acknowledgement..use ACK or NAK..\n "); goto ph; } send(sd,ack,5,0); i=recv(sd,content,30,0); } printf("\n\n Bye..."); close(sd); return 0; }
(Screenshot of output) dnsserver.c #include "stdio.h" #include "string.h" #include "stdlib.h" #include "unistd.h" #include "errno.h" #include "sys/socket.h" #include "sys/types.h" #include "netinet/in.h" #include "arpa/inet.h" #define MAXBUFFLEN 500 int main(int argc,char *argv[]) { int sockfd,port,bi; char buff[MAXBUFFLEN-1]; struct sockaddr_in servaddr,cliaddr; int addr_len,numbytes; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { perror("Socket()"); return 0; } printf("\n Enter the port no:"); scanf("%d",&port); printf("\n The Port no is:%d",port);
servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); servaddr.sin_addr.s_addr = INADDR_ANY; memset(servaddr.sin_zero,'\0',8); bi=bind(sockfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr)); if(bi==-1) { perror("bind"); return 0; } addr_len = sizeof(struct sockaddr*); numbytes = recvfrom(sockfd,buff,MAXBUFFLEN-1,0,(struct sockaddr*)&cliaddr,&addr_len); printf("\n Server Got-Packet from %s",inet_ntoa(cliaddr.sin_addr)); printf("\n Server packet is %d bytes long.",numbytes); buff[numbytes]='\0'; printf("\n The Server packet contains %s",buff); close(sockfd); return 0; } dnsclient.c #include "stdio.h" #include "string.h" #include "stdlib.h" #include "unistd.h" #include "errno.h" #include "sys/socket.h" #include "sys/types.h" #include "netinet/in.h" #include "arpa/inet.h" #include "netdb.h" int main(int argc,char *argv[]) { int sockfd,port; struct sockaddr_in servaddr; struct hostent *he; int addr_len,numbytes; if(argc!=3) { fprintf(stderr,"Client Usage : %s \n",argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) {
perror("gethostbyname error"); exit(1); } sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { perror("Socket."); return 0; } printf("\n Enter the port no:"); scanf("%d",&port); printf("\n The port no is : %d",port); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); servaddr.sin_addr = *((struct in_addr*)he->h_addr); memset(&(servaddr.sin_zero),'\0',8); numbytes = sendto(sockfd,argv[2],sizeof(argv[2]),0,(struct sockaddr*)&servaddr,sizeof(struct sockaddr)); printf("\n Sent %d bytes to %s",numbytes,inet_ntoa(servaddr.sin_addr)); close(sockfd); return 0; }
int main() { int sfd,r,bi,port; char buff[1024]; struct sockaddr_in servaddr,cliaddr; socklen_t clilen; sfd=socket(AF_INET,SOCK_DGRAM,0); if(sfd==-1) { perror("Socket"); return 0; } memset(servaddr.sin_zero,'\0',sizeof(servaddr.sin_zero)); printf("\n Enter the port no:"); scanf("%d",&port); printf("\n The port no is:%d",port); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); servaddr.sin_addr.s_addr = INADDR_ANY; bi=bind(sfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); if(bi==-1) { perror("Bind()"); return 0; } clilen = sizeof(cliaddr); r=recvfrom(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&clilen); buff[r]=0; time_t ticks; ticks = time(NULL); snprintf(buff,sizeof(buff),"%24s\r\n",ctime(&ticks)); sendto(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)); exit(0); return 0; } tclient.c #include "stdio.h" #include "string.h" #include "stdlib.h" #include "sys/socket.h" #include "sys/types.h" #include "netinet/in.h" int main() {
int listenfd,port,r; char buff[1024]; struct sockaddr_in servaddr,cliaddr; socklen_t servlen; listenfd = socket(AF_INET,SOCK_DGRAM,0); if(listenfd==-1) { perror("Socket"); return 0; } memset(servaddr.sin_zero,'\0',sizeof(servaddr.sin_zero)); printf("\n Enter the port no:"); scanf("%d",&port); printf("\n The port no is:%d",port); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); servaddr.sin_addr.s_addr = INADDR_ANY; sendto(listenfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); r=recvfrom(listenfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,&servlen); buff[r]=0; printf("\n The time received from the server:%s",buff); exit(0); return 0; }
TCP - Reliable, Ordered, Heavyweight, Streaming. UDP - Unreliable, Not Ordered, Lightweight, Datagrams. Screenshot of the Output
Program
Server.c #include "stdio.h" #include "sys/types.h" #include "sys/socket.h" #include "netinet/in.h" #include "string.h" #include "stdlib.h" int main() { int len,sfd,connfd,i=0,r,b,l,port; char buff[1024],str[1024],c; struct sockaddr_in servaddr,cliaddr; socklen_t clilen; sfd=socket(AF_INET,SOCK_DGRAM,0); if(sfd==-1) { perror("Socket"); exit(-1);
} memset(servaddr.sin_zero,'\0',sizeof(servaddr.sin_zero)); printf("\n Enter the port no:"); scanf("%d",&port); printf("\n Port No is %d",port); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=INADDR_ANY; servaddr.sin_port=htons(port); b=bind(sfd,(struct sockaddr*)&servaddr, sizeof(servaddr)); if(b==-1) { perror("bind"); exit(-1); } clilen=sizeof(cliaddr); r=recvfrom(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr, &clilen); buff[r]=0; printf("\n Client:%s",buff); printf("\n Server:%s",buff); sendto(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)); exit(0); return 0; } Client.c #include "stdio.h" #include "sys/types.h" #include "sys/socket.h" #include "netinet/in.h" #include "string.h" #include "stdlib.h" int main() { int len,listenfd,connfd,i=0,r,co,port; char buff[1024],c; struct sockaddr_in servaddr,cliaddr; socklen_t servlen; listenfd=socket(AF_INET,SOCK_DGRAM,0); if(listenfd==-1) { perror("socket"); exit(-1); } memset(servaddr.sin_zero,'\0',sizeof(servaddr.sin_zero));
printf("\n Enter the port no:"); scanf("%d",&port); printf("\n Port No is : %d",port); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=INADDR_ANY; servaddr.sin_port=htons(port); printf("\n Client:"); scanf("%s",buff); sendto(listenfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); r=recvfrom(listenfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr, &servlen); buff[r]=0; printf("\n Server:%s\n",buff); exit(0); return 0; }
Screen-shot of output:
Program
server.c #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" main() { int sd,i,len,bi,nsd,port; char content[30],buff[30],last_received[30]; struct sockaddr_in ser,cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } printf("\n Socket created\n"); bzero((char*)&cli,sizeof(ser)); printf("ENTER PORT NO:\n"); scanf("%d",&port); printf("\n Port Address is %d\n:",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY);
bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser)); if(bi==-1) { printf("\n bind error, port busy, plz change port in client and server"); return 0; } i=sizeof(cli); listen(sd,5); nsd = accept(sd,((struct sockaddr *)&cli),&i); if(nsd==-1) { printf("\ncheck the description parameter\n"); return 0; } printf("\nConnection accepted"); i = recv(nsd,content,30,0); send(nsd,content,30,0); while(strcmp(content,"exit")!=0) { printf("\nReceived from client and echoeing it...."); i=recv(nsd,content,30,0); send(nsd,content,30,0); } printf("\n Bye"); send(nsd,"EOF",4,0); close(sd); close(nsd); return 0; } client.c #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" int main() { int sd,con,port,i; char content[30]; struct sockaddr_in cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{ printf("\n Socket problem"); return 0; } bzero((char*)&cli,sizeof(cli)); cli.sin_family = AF_INET; printf("ENTER PORT NO"); scanf("%d",&port); cli.sin_port=htons(port); cli.sin_addr.s_addr=htonl(INADDR_ANY); con=connect(sd,(struct sockaddr*)&cli,sizeof(cli)); if(con==-1) { printf("\n connection error"); return 0; } if(fork()) { printf("\nEnter the data to be send:"); scanf("%s",content); while(strcmp(content,"exit")!=0) { send(sd,content,30,0); scanf("%s",content); } send(sd,"exit",5,0); } else { i=recv(sd,content,30,0); while(strcmp(content,"exit")!=0) { printf("Data Echoed from Server: %s\n",content); i=recv(sd,content,30,0); } send(sd,"exit",5,0); } close(sd); return 0; }
eclient.c
Posted by G.Vivek Venkatesh at 11:18 PM 0 comments Labels: Networks Lab
Screen-shot of output:
Program
server.c #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" #include "time.h" main()
{ int sd,i,len,bi,nsd,port; char content[30],buff[30],last_received[30]; struct sockaddr_in ser,cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } printf("\n Socket created\n"); bzero((char*)&cli,sizeof(ser)); printf("ENTER PORT NO:\n"); scanf("%d",&port); printf("\n Port Address is %d\n:",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY); bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser)); if(bi==-1) { printf("\n bind error, port busy, plz change port in client and server"); return 0; } i=sizeof(cli); listen(sd,5); nsd = accept(sd,((struct sockaddr *)&cli),&i); if(nsd==-1) { printf("\ncheck the description parameter\n"); return 0; } printf("\nConnection accepted"); time_t ticks; ticks=time(NULL); snprintf(buff,sizeof(buff),"%24s\r\n",ctime(&ticks)); send(nsd,buff,30,0); printf("\n Bye.."); send(nsd,"EOF",4,0); close(sd); close(nsd); return 0; }
client.c #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" int main() { int sd,con,port,i; char content[30]; struct sockaddr_in cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } bzero((char*)&cli,sizeof(cli)); cli.sin_family = AF_INET; printf("ENTER PORT NO"); scanf("%d",&port); cli.sin_port=htons(port); cli.sin_addr.s_addr=htonl(INADDR_ANY); con=connect(sd,(struct sockaddr*)&cli,sizeof(cli)); if(con==-1) { printf("\n connection error"); return 0; } i=recv(sd,content,30,0); printf("Received from server %s\n",content); close(sd); return 0; }
Screen-shot of output
Program
server.c #include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" main() { int sd,i,len,bi,nsd,port; char content[30]; struct sockaddr_in ser,cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } printf("\n Socket created\n");
bzero((char*)&cli,sizeof(ser)); printf("ENTER PORT NO:\n"); scanf("%d",&port); printf("\n Port Address is %d\n:",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY); bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser)); if(bi==-1) { printf("\n bind error, port busy, plz change port in client and server"); return 0; } i=sizeof(cli); listen(sd,5); nsd = accept(sd,((struct sockaddr *)&cli),&i); if(nsd==-1) { printf("\ncheck the description parameter\n"); return 0; } printf("\nConnection accepted"); if(fork()) { printf("\n Enter the data to be send type exit for stop:\n"); scanf("%s",content); while(strcmp(content,"exit")!=0) { send(nsd,content,30,0); scanf("%s",content); } send(nsd,"exit",5,0); } else i = recv(nsd,content,30,0); while(strcmp(content,"exit")!=0) { printf("\nReceived from client %s\n",content); i=recv(nsd,content,30,0); } while(strcmp(content,"exit")!=0) {
printf("\n Received from client %s\n",content); i=recv(nsd,content,30,0); } printf("\n Bye"); send(nsd,"EOF",4,0); close(sd); close(nsd); return 0; }
client.c
#include "stdio.h" #include "sys/types.h" #include "netinet/in.h" #include "string.h" #include "sys/socket.h" #include "stdlib.h" #include "unistd.h" int main() { int sd,con,port,i; char content[30]; struct sockaddr_in cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } bzero((char*)&cli,sizeof(cli)); cli.sin_family = AF_INET; printf("ENTER PORT NO"); scanf("%d",&port); cli.sin_port=htons(port); cli.sin_addr.s_addr=htonl(INADDR_ANY); con=connect(sd,(struct sockaddr*)&cli,sizeof(cli)); if(con==-1) { printf("\n connection error");
return 0; } if(fork()) { printf("\nEnter the data to be send type exit for stop"); scanf("%s",content); while(strcmp(content,"exit")!=0) { send(sd,content,30,0); scanf("%s",content); } send(sd,"exit",5,0); } else { i=recv(sd,content,30,0); while(strcmp(content,"exit")!=0) { printf("Received from server %s\n",content); i=recv(sd,content,30,0); } send(sd,"exit",5,0); } close(sd); return 0; }