break closef2 printfclientd file closedn i continue else

Break closef2 printfclientd file closedn i continue

This preview shows page 27 - 34 out of 41 pages.

Image of page 27
Image of page 28
if(getcwd(buffer, sizeof(buffer)) != NULL) printf("%s\n",buffer); continue; } /* send the message to server */ nw = sendto(sd, buf, sizeof(buf), 0, (struct sockaddr *) &ser_addr, ser_addr_len); if (nw <= 0) exit(1); /* send error */ printf("client[%d]: send %s\n", i, buf); /* receive the message from server */ bzero(buf, BUFSIZE); nr = recvfrom(sd, buf, BUFSIZE, 0, (struct sockaddr *) &ser_addr, &ser_addr_len); if (buf[nr-1] == '\n') { buf[nr-1] = '\0'; --nr; } if (nr <= 0) exit(1); /* receive error */ printf("client[%d]: receive %s\n", i, buf); } } Server(myftpd.c) /*
Image of page 29
* Test stream I/O (server part) */ #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <dirent.h> #include <unistd.h> #include <signal.h> /* SIGCHLD, sigaction() */ #include <string.h> /* strlen(), strcmp() etc */ #include <errno.h> /* extern int errno, EINTR, perror() */ #include <sys/types.h> /* pid_t, u_long, u_short */ #include <sys/wait.h> /* waitpid */ #include <sys/stat.h> /* umask */ #include <sys/socket.h> /* struct sockaddr, socket(), etc */ #include <netinet/in.h> /* struct sockaddr_in, htons(), htonl(), */ /* and INADDR_ANY */ #define SERV_TCP_PORT 40175 /* server port no */ #define BUFSIZE 1024 void claim_children() { pid_t pid=1; while (pid>0) /* claim as many zombies as we can */ { pid = waitpid(0, (int *)0, WNOHANG); } } void daemon_init(void) {
Image of page 30
pid_t pid; struct sigaction act; if ( (pid = fork()) < 0) { perror("fork"); exit(1); } else if (pid > 0) { printf("Server PID: %d\n", pid); exit(0); /* parent goes bye-bye */ } /* child continues */ setsid(); /* become session leader */ chdir("/"); /* change working directory */ umask(0); /* clear file mode creation mask */ /* catch SIGCHLD to remove zombies from system */ act.sa_handler = claim_children; /* use reliable signal */ sigemptyset(&act.sa_mask); /* not to block other signals */ act.sa_flags = SA_NOCLDSTOP; /* not catch stopped children */ sigaction(SIGCHLD,(struct sigaction *)&act,(struct sigaction *)0); } void serve_a_client(int sd, struct sockaddr_in cli_addr, int cli_addr_len) { int nr, nw, len; int i =0; char buf[BUFSIZE], buffer[BUFSIZE]; char *temp;
Image of page 31
Image of page 32
{ DIR *dp; struct dirent *file; char fileName[BUFSIZE], temp[BUFSIZE]; int n = 0; if((dp = opendir(buffer)) == NULL)/* open directory */ { printf("Error in opening directory %s\n", buffer); exit(1); } if((file = readdir(dp)) < 0) {
Image of page 33
Image of page 34

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture