0% found this document useful (0 votes)
61 views

Computer Network File

The document describes 5 programs using various networking concepts in C programming: 1. A TCP socket program to send date and time between a server and client. 2. A UDP socket simple DNS program with a server and client. 3. A raw socket program for packet capturing and filtering with a server and client. 4. An RPC program to call a remote date procedure on a server from a client. 5. A sliding window protocol program for sending data between a sender and receiver.

Uploaded by

Aviral Chaurasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Computer Network File

The document describes 5 programs using various networking concepts in C programming: 1. A TCP socket program to send date and time between a server and client. 2. A UDP socket simple DNS program with a server and client. 3. A raw socket program for packet capturing and filtering with a server and client. 4. An RPC program to call a remote date procedure on a server from a client. 5. A sliding window protocol program for sending data between a sender and receiver.

Uploaded by

Aviral Chaurasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PROGRAM-1

AIM: Program Using TCP Socket Date & Time


Requirements: PC, Visual Code Software, CMD Terminal
Programing Used- C Programming
Server Side Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#define max 30
#define PORT 2100
int main()
{
int sersoc,clisoc,conn,len,wri;
char str[max];
pid_t pid;
time_t ticks;
socklen_t clilen;
struct sockaddr_in servaddr,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 SIDE CODE:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdlib.h>
#define CLI_PORT 2100
#define BUFF_SIZE 30
int main(int argc,char **argv)
{
int clisoc,re;
char recbuff[BUFF_SIZE];
struct sockaddr_in cliaddr;
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(CLI_PORT);
cliaddr.sin_addr.s_addr=inet_addr(argv[1]);
if((clisoc=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("Socket Errror");
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 WINDOW

Result: Thus the above program of TCP Socket are successfully executed on Terminal
Window.
PROGRAM-2

AIM: Program Using UPD Socket of Simple DNS.


Requirements: PC, Visual Code Software, CMD Terminal
Programing Used- C Programming
Client Side
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
main()
{
struct sockaddr_in server,client;
int s,n;
char b1[100],b2[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=3000;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
n=sizeof(server);
printf("\nEnter canonical address: ");
scanf("%s",b2);
sendto(s,b2,sizeof(b2),0,(struct sockaddr *)&server,n);
recvfrom(s,b1,sizeof(b1), 0,NULL,NULL);
printf("%s \n",b1);
}

Server Side

// Server Program : dnss.c

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
main()
{
FILE *fp;
struct sockaddr_in server,client;
int s,n;
char b1[100],b2[100],a[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=3000;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(s,(struct sockaddr *)&server,sizeof(server));
n=sizeof(client);
while(1)
{
strcpy(b2,"");
fp=fopen("dns.txt","r");
recvfrom(s,b1,sizeof b1, 0,(struct sockaddr *)&client,&n);
while(!feof(fp))
{
fscanf(fp,"%s",a);
if(strcmp(a,b1)==0)
{
fscanf(fp,"%s",b2);
break;
}
}
if(strcmp(b2,"")==0)
{
strcpy(b2,"Not found...");
}
fclose(fp);
sendto(s,b2,sizeof b2,0,(struct sockaddr *)&client,n);
}
}
OUTPUT WINDOW

Result: Thus the above program of UDP Socket are successfully executed on Terminal
Window
PROGRAM-3

AIM: Program Using Raw Sockets Packet Capturing & Filtering


Requirements: PC, Visual Code Software, CMD Terminal
Programing Used- C Programming
Server Side:
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<errno.h>
#define SIZE 256
int main()
{
intsrFd,CIFD,len;
structsockaddr_inserver,client;
charbuf[SIZE],buf1[SIZE];
intn,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,(structsockaddr *)&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,(structsockaddr *)&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 Side Programming
#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;
structsockaddr_in client;
charbuf[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,(structsockaddr *)&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);
}
printf(“\n Destination file name from server %s”,buf);
buf[n]=NULL;
close(CIFD);
exit(0);
}

OUTPUT TERMINAL WINDOW

RESULT:
Thus raw sockets for packet capturing and filtering has been executed and verified
successfully.
PROGRAM-4

AIM: Program Using RPC

Requirements: UBUNTU OS, rpcgen Protocol Compiler, Terminal

Programming Used- C Programming- rdate.c - client program for remote date


service */

#include stdio.h
#include rpc/rpc.h
#include stdlib.h
#include "date.h"
int main(int argc, char *argv[]) {
CLIENT *cl;
char *server;
long *lres;
if (argc != 2) {
fprintf(stderr, "usage: %s hostname\n", argv[0]);
exit(1);
}
server = argv[1];
/* create client handle */
if ((cl = clnt_create(server, DATEPROG, DATEVERS, "udp")) == NULL) {
/* couldn't establish connection with server */
printf("can't establish connection with host %s\n", server);
exit(2);
}
/* first call the remote procedure bindate() */
if (( lres = bindate_1(NULL, cl)) == NULL){
printf(" remote procedure bindate() failure\n");
exit(3);
}
printf("time on host %s = %ld\n", server, *lres);
clnt_destroy(cl); /* done with handle */
return 0;
}
/*********************************************************************/
/* dateproc.c - remote procedures; called by server stub */
#include stdio.h
#include stdlib.h
#include rpc/rpc.h
#include "date.h"
/* return the binary date and time */
/* In Linux: long * bindate_1_svc(void* arg1, struct svc_req *arg2) {
*/

/* In Dec Unix: long * bindate_1() {


*/

long * bindate_1() {
static long timeval; /* must be static */
timeval = time((long *) 0);
return (&timeval);
}

Commands:
rpcgen date.x
gcc -c date_clnt.c
gcc -c date_svc.c
gcc -c dateproc.c
gcc -c rdate.c
gcc -o client date_clnt.o rdate.o
gcc -o server date_svc.o dateproc.o

RESULT:
Thus the above code is executed successfully on LINUX Terminal Window with Command
Line.
PROGRAM-5

AIM: Program of Sliding Window Protocol using C Language


Requirements: PC, Visual Code Software, CMD Terminal
SENDER
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
int main()
{
int sock,bytes_received,connected,true=1,i=1,s,f=0,sin_size;
char send_data[1024],data[1024],c,fr[30]=" ";
struct sockaddr_in server_addr,client_addr;
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Socket not created");
exit(1);
}
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1)
{
perror("Setsockopt");
exit(1);
}
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(17000);
server_addr.sin_addr.s_addr=INADDR_ANY;
if(bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("Unable to bind");
exit(1);
}
if(listen(sock,5)==-1)
{
perror("Listen");
exit(1);
}
fflush(stdout);
sin_size=sizeof(struct sockaddr_in);
connected=accept(sock,(struct sockaddr *)&client_addr,&sin_size);
while(strcmp(fr,"exit")!=0)
{
printf("Enter Data Frame %d:(Enter exit for End): ",i);
scanf("%s",fr);
send(connected,fr,strlen(fr),0);
recv(sock,data,1024,0);
if(strlen(data)!=0)
printf("I got an acknowledgement : %s\n",data);
fflush(stdout);
i++;
}
close(sock);
return (0);
}

RECEIVER SIDE

#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
int main()
{
int sock,bytes_received,i=1;
char receive[30];
struct hostent *host;
struct sockaddr_in server_addr;
host=gethostbyname("127.0.0.1");
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Socket not created");
exit(1);
}
printf("Socket created");
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(17000);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if(connect(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("Connect");
exit(1);
}
while(1)
{
bytes_received=recv(sock,receive,20,0);
receive[bytes_received]='\0';
if(strcmp(receive,"exit")==0||strcmp(receive,"exit")==0)
{
close(sock);
break;
}
else
{
if(strlen(receive)<10)
{
printf("\n Frame %d data %s received\n",i,receive);
send(0,receive,strlen(receive),0);
}
else
{
send(0,"negative",10,0);
}
i++;
}
}
close(sock);
return(0);
}
OUTPUT TERMINAL WINDOW

Result: Thus the above program of Sliding Window Protocol are successfully
executed on Terminal Window.

You might also like