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

Computer Networks Lab: Name-Kaushik Ghosh ROLL - 34900117048 College-Coochbehar Government Engineering College

This document contains code for three C programs: a file copying program that takes two file paths as arguments and copies the contents of the first file to the second, a program demonstrating fork() to create a parent and child process, and a client-server program using pipes for interprocess communication. The client program takes a file path from the user, sends it to the server, and receives the file contents back, while the server opens and reads the given file before sending the contents back to the client.

Uploaded by

kaushik ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Computer Networks Lab: Name-Kaushik Ghosh ROLL - 34900117048 College-Coochbehar Government Engineering College

This document contains code for three C programs: a file copying program that takes two file paths as arguments and copies the contents of the first file to the second, a program demonstrating fork() to create a parent and child process, and a client-server program using pipes for interprocess communication. The client program takes a file path from the user, sends it to the server, and receives the file contents back, while the server opens and reads the given file before sending the contents back to the client.

Uploaded by

kaushik ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COMPUTER NETWORKS LAB

NAME-KAUSHIK GHOSH
ROLL -34900117048
COLLEGE-COOCHBEHAR GOVERNMENT ENGINEERING COLLEGE

APRIL 29, 2020


#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#define BUF_SIZE 1024

int main(int argc, char *argv[])


{
int sc, dt, r, w;
char *buffer[BUF_SIZE];
if(argc !=3){
printf("Number of files are either greater than or less
than 2!");
exit(1);
}

sc = open(argv[1],O_RONLY);
if(sc==-1){
printf("\nError opening file %s. errno =
%d\n",argv[1],errno);
exit(1);
}

dt = open(argv[2], O_WONLY|O_CREAT|O_TRUNC,S_IRUSR | S_IWUSR |


S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(dt==-1){
printf("\nError opening file %s. errno =
%d\n",argv[2],errno);
exit(1);
}

/*Start copy paste*/


while((r = read(sc,buffer,BUFFER_SIZE)) > 0)
{
if(wite(dt,buffer,r) != r)
printf("\nError in witing data to %s\n",argv[2]);
}

if(r == -1)
printf("\nError in reading data from %s\n",argv[1]);

if(close(sc) == -1)
printf("\nError in closing file %s\n",argv[1]);

if(close(dt) == -1)
printf("\nError in closing file %s\n",argv[2]);

exit(0);
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main( int argc,char *argv[])


{
int cpid = 0;
int ct1 = 0,ct2 = 0;

int f=9,g=7,h,i;

h = (f+g);
i =(f-g);

printf("Before it Forks\n");

cpid = fork();

if(cpid == 0)
{
printf("This is a child Process\n");
while(ct1<5)
{
printf("Child Process:%d\n",ct1);
printf("Subtraction is:%d \n",i);
sleep(1);
ct1++;
}
}else{
printf("This is a Parent Process\n");
while(ct2<10)
{
printf("Parent Process:%d\n",ct1);
printf("Addition is:%d \n",h);
sleep(1);
ct2++;
}

}
return 0;
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
int pp1[2],pp2[2];
pid_t p;
if(pipe(pp1)==-1 || pipe(pp2)==-1)
{
fprintf(stderr,"Pipe Operation failed");
exit(1);
}

p=fork();
if(p<0)
{
printf("fork's return value less than zero.");
exit(1);
}
else if(p>0){
printf("Client side running.////.\n");

char fp[100],fc [1000];


printf("Enter file path:\n");
scanf("%s",fp);

close(pp1[0]);
write(pp1[1], fp, strlen(fp)+1);
close(pp1[1]);

wait(NULL);

close(pp2[1]);
read(pp2[0],fc,1000);
printf("The file contents are:\n%s",fc);
close(pp2[0]);
}
close(pp1[1]);
read(pp1[0],file_path,strlen(fp)+1);
close(pp1[0]);

FILE *fptr;
fptr=fopen(fp,"r");
if(fptr==NULL)
{
printf("The file path is nullable..");
exit(1);
}

int i=0;
c=fgetc(fptr);
while(c!=EOF)
{
fc[i]=c;
c=fgetc(fptr);
i++;
}

close(pp2[0]);
write(pp2[1],file_content,i+2);
close(pp2[1]);
printf("Contents sent.\n");
}
return 0;
}

Server program:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>

int main()
{
int fd_sock;
char data_rec[1024];
struct sockaddr_in sv_add, cl_addr;
socklen_t cl_ln;

if((fd_sock=socket(AF_INET, SOCK_DGRAM,0))<0)
{
perror("Sorry,Socket creation Failed.");
exit(1);
}
printf("Socket()Created Success\n");
memset(&sv_add,0,sizeof(sv_add));
memset(&cl_addr,0,sizeof(cl_addr));

sv_add.sin_family = AF_INET;
sv_add.sin_addr.s_addr = inet_addr("127.0.0.1");
sv_add.sin_port = htons(8080);

if(bind(fd_sock,(struct
sockaddr*)&sv_add,sizeof(sv_add))<0)
{
perror("Binding failed.");
exit(1);
}
printf("Bind() sucessful.\n");

printf("Running on port 8080.\n");


int n;

cl_ln = sizeof (cl_addr);


while(1)
{
n = recvfrom(fd_sock, data_rec, 1024, 0,(struct sockaddr
*) &cl_addr, &cl_ln);

char * response = "Message Rcved";


printf("\n Rcved data from client
%s:%d\n%s\n",inet_ntoa(cl_addr.sin_addr), ntohs(cl_addr.sin_port),
data_rec);
sendto(fd_sock, response, strlen(response), 0, (struct sockaddr
*)& cl_addr, cl_ln);
}
close(fd_sock);
return 0;
}

Client Program:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])


{
int fd_sock, i;
struct sockaddr_in sv_add;
char data[1024];

if((fd_sock=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("Socket creation failed");
exit(1);
}
printf("socket() created.");

memset(&sv_add, 0, sizeof sv_add);

sv_add.sin_family=AF_INET;
sv_add.sin_port =htons(8080);
sv_add.sin_addr.s_addr = inet_addr("127.0.0.1");

memset(sv_add.sin_zero, '\0' , sizeof sv_add.sin_zero);


socklen_t socket_len = sizeof(sv_add);

while(1)
{
printf("\nEnter message to be sent:\nP.S- Please type
exit and press enter to exit.\n");
scanf("%s", data);
if(strcmp(data,"exit")==0)
{
printf("Exiting from loop.\n");
break;
}

if(sendto(fd_sock,data,strlen(data),0,(struct
sockaddr*)&sv_add,socket_len)<0)
{
perror("Error in sendto()");
exit(0);
}

i = recvfrom(fd_sock, data, 1024, 0, NULL, NULL);

printf("Acknowledgement(Although it's one way sir but


it's for my happiness and debugging'') : %s\n\n", data);

close(fd_sock);
return 0;
}

You might also like