





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lab report was submitted to Sir Rahul Das at Birla Institute of Technology and Science as lab assignment for Networking course. Its main points are: Markov, Process, Stochastic, FSM, Simple, Example, Weather, Raining
Typology: Exercises
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CODE:
CLIENT.C
// define a file pointer to open and read the file FILE *pc; int n; // define the name of file char *name="pc.txt"; // create buffer for sending data through socket char buffer[100]; . . . // open the file “pc.txt” pc=fopen("pc.txt","r"); // send file name write(sk,name,strlen(name)); // send the content of file do { n=fread(buffer,1,100,pc); write(sk,buffer,n); }while(n>0); // close the opened file fclose(pc);
SERVER.C
// create string to store filename char *name; char buff[100]; // create string to store command char *command; // allocate space for strings to avoid stackoverflow error command = (char *)malloc(100); name = (char *)malloc(7);
int n;
// create File pointer to open a file FILE *ptr; . . . // initialize command and name string as null for(i = 0;i < strlen(command);i=i+1){ command[i] = 0; } for(i=0;i<strlen(name);i=i+1){ name[i] = 0; } // read the name specified by client n=read(sd,name,6); printf("\r\n name is : %s",name); // create a command "cp welc.txt ./servdir/<filename.extension>" command = strncat(command,"cp welc.txt ./servdir/",strlen("cp welc.txt ./servdir/")); // printf("\r\n%s",command); command = strncat(command,name,6); printf("\r\n command used to save file : %s",command); // open a dummy file "welc.txt" ptr=fopen("welc.txt","w"); // write received data in welc.txt do { n=read(sd,buff,100); fwrite(buff,1,n,ptr); }while(n>0);
fclose(ptr); // copy the dummy file to the specified local folder with client desired name system(command); // free allocated memory free(command); free(name); // authenticate file saving process printf("\r\nthe file is saved in the path : /servdir/%s",name);
LAB TASK 3
TO DO:
PROCEDURE:
CODE:
Following changes were made in the server source file:
#include <stdio.h> /* standard C i/o facilities / #include <unistd.h> / Unix System Calls / #include <stdlib.h> / standard library / #include <sys/types.h> / system data type definitions / #include <sys/socket.h> / socket specific definitions / #include <netinet/in.h> / INET constants and stuff / #include <arpa/inet.h> / IP address conversion stuff */ #include <string.h> #include <malloc.h>
/* Server main routine - this is an iterative server
int main() { int ld,sd; struct sockaddr_in skaddr; struct sockaddr_in from;
int addrlen,length; char buff[1024]; char pdata[1024]; int n,m; FILE *ptr; FILE *dptr; ptr=fopen("./servdir/servdata.log","a");
fprintf(ptr,"\r\nNew Log session\r\n"); /* create a socket IP protocol family (PF_INET) TCP protocol (SOCK_STREAM) */
if ((ld = socket( PF_INET, SOCK_STREAM, 0 )) < 0) { perror("Problem creating socket\n"); exit(1);
/* establish our address address family is AF_INET our IP address is INADDR_ANY (any of our IP addresses) the port number is assigned by the kernel */
skaddr.sin_family = AF_INET; skaddr.sin_addr.s_addr = htonl(INADDR_ANY); skaddr.sin_port = htons(0);
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) { perror("Problem binding\n"); exit(0); }
/* find out what port we were assigned and print it out */
length = sizeof( skaddr ); if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) { perror("Error getsockname\n"); exit(1); } printf("The Server passive socket port number is %d\n",ntohs(skaddr.sin_port));
/* put the socket into passive mode (waiting for connections) */
if (listen(ld,5) < 0 ) { perror("Error calling listen\n"); fprintf(ptr,"Error calling listen\n"); exit(1); }
/* now process incoming connections forever ... / fclose(ptr); while (1) { ptr=fopen("./servdir/servdata.log","a"); fprintf(ptr,"Ready for a connection...\n"); addrlen=sizeof(skaddr); if ( (sd = accept( ld, (struct sockaddr) &from, &addrlen)) < 0) { perror("Problem with accept call\n"); fprintf(ptr,"Problem with accept call\n");
exit(1); }
fprintf(ptr,"Got a connection - processing...\n");
/* Determine and print out the address of the new server socket */
length = sizeof( skaddr ); if (getsockname(sd, (struct sockaddr ) &skaddr, &length)<0) { perror("Error getsockname\n"); fprintf(ptr,"Error getsockname\n"); exit(1); } fprintf(ptr,"The active server port number is %d\n",ntohs(skaddr.sin_port)); fprintf(ptr,"The active server IP ADDRESS is %s\n",inet_ntoa(skaddr.sin_addr)); / print out the address of the client */
fprintf(ptr,"The client port number is %d\n",ntohs(from.sin_port)); fprintf(ptr,"The client IP ADDRESS is %s\n",inet_ntoa(from.sin_addr));
/* read and send to stdout until the client closes the connection*/ do { m = read(sd,pdata,1024); if(m >0){ //printf("\r\nSize : Read from socket :%d :%s",m,pdata); system("date > data.log"); dptr = fopen("data.log","r"); n = fread(buff,1,1024,dptr); //printf("Size : Current Date : %d : %s",n,buff); fwrite(buff,1,n,ptr); fclose(dptr); } fwrite(pdata,1,m,ptr); fwrite("\r\n",1,strlen("\r\n"),ptr); }while(m>0);
fprintf(ptr,"\r\nDone with connection - closing\n\n\n"); fclose(ptr); close(sd); } }
RESULTS:
CLIENT
[se1032@localhost G4]$ ./client 127.0.0.1 34800 hassan [se1032@localhost G4]$ ./client 127.0.0.1 34800 saleem [se1032@localhost G4]$ ./client 127.0.0.1 34800 sammer^H [se1032@localhost G4]$ ./client 127.0.0.1 34800 farooq [se1032@localhost G4]$ ./client 127.0.0.1 34800 shahzad [se1032@localhost G4]$ ./client 127.0.0.1 34800 afzal [se1032@localhost G4]$ ./client 127.0.0.1 34800 zaeem [se1032@localhost G4]$ ./client 127.0.0.1 34800 kumail [se1032@localhost G4]$ ./client 127.0.0.1 34800 hassan_shahifd [se1032@localhost G4]$
LAB TASK 4
PROCEDURE:
CODE :