PROGRAM 2
/* filename: server.c author: anthony f. ortiz */ /* this is the server. it executes a file command issued by the client */ /* type 'server host port'. */ /* headers */ #include #include #include #include #include #include #include /* constants */ #define MAX_LINEBUFFER 50 #define MAX_QUEUE 5 #define TRUE 1 /* this is the server program. */ void main (int argc, char * argv []) { int sock, sock2, pid, i; char command [MAX_LINEBUFFER] = ""; struct sockaddr_in server; struct hostent * hp, * gethostbyname (); /* create socket. */ sock = socket (AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("opening stream socket."); exit (1); } /* bind socket. */ hp = gethostbyname (argv [1]); if (hp == 0) { printf ("%s, unknown host.\n", argv [1]); exit (2); } bcopy (hp -> h_addr, & server.sin_addr, hp -> h_length); server.sin_port = htons (atoi (argv [2])); server.sin_family = AF_INET; if (bind (sock, (struct sockaddr *) & server, sizeof (server))) { perror ("binding socket name."); exit (1); } /* listen and accept the connection. when connection is established, */ /* get the command from the client and execute it. */ listen (sock, MAX_QUEUE); do { /* accept connection. */ if((sock2 = accept (sock, 0, 0)) < 0) { perror ("accept."); } else { do { /* get the command from the client. */ bzero (command, sizeof (command)); if (read (sock2, command, sizeof (command)) < 0) { perror ("reading message stream."); } /* print command. */ printf ("\n%s\n", command); /* execute command if it isn't equal to 'stop'. */ if (strcmp (command, "stop") != 0) { char * args [5]; /* get arguements. */ i = 0; args [i] = strtok (command, " "); while (args [i] != NULL) { i++; args [i] = strtok (NULL, " "); } i++; args [i] = (char *) 0; /* execute command using exec function. */ pid = fork (); if (pid < 0) { perror ("fork."); } else if (pid == 0) { if (execv (strcat ("/bin/", args [0]), args)) { perror ("exec:"); } } else { wait ((int *) 0); } } } while (strcmp (command, "stop") != 0); /* close the socket. */ close (sock2); } } while (TRUE); /* close the socket. */ close (sock); } /* filename: client.c author: anthony f. ortiz */ /* this is the client. it sends a file command to the server. */ /* type 'client host port'. */ /* headers */ #include #include #include #include #include #include /* constants */ #define MAX_LINEBUFFER 50 #define MAX_FILESIZE 20 #define DISPLAY 49 #define COPY 50 #define RENAME 51 #define REMOVE 52 #define LIST 53 #define EXIT 54 /* print menu. */ void print_menu () { printf ("\nfile menu\n"); printf ("\t1. display a file.\n"); printf ("\t2. copy a file.\n"); printf ("\t3. rename a file.\n"); printf ("\t4. remove a file.\n"); printf ("\t5. list a directory.\n"); printf ("\t6. quit the program.\n"); printf ("enter a number from 1-6: "); } /* display file. */ void display_file (char * command) { char file [MAX_FILESIZE] = ""; printf ("\nenter file name to display. "); scanf ("%s", file); strcat (command, "cat "); strcat (command, file); } /* copy file. */ void copy_file (char * command) { char file1 [MAX_FILESIZE] = ""; char file2 [MAX_FILESIZE] = ""; printf ("\nenter existing file name. "); scanf ("%s", file1); printf ("enter new file name. "); scanf ("%s", file2); strcat (command, "cp "); strcat (command, file1); strcat (command, " "); strcat (command, file2); } /* rename file. */ void rename_file (char * command) { char file1 [MAX_FILESIZE] = ""; char file2 [MAX_FILESIZE] = ""; printf ("\nenter old file name. "); scanf ("%s", file1); printf ("enter new file name. "); scanf ("%s", file2); strcat (command, "mv "); strcat (command, file1); strcat (command, " "); strcat (command, file2); } /* remove file. */ void remove_file (char * command) { char file [MAX_FILESIZE] = ""; printf ("\nenter file name to remove. "); scanf ("%s", file); strcat (command, "rm "); strcat (command, file); } /* list directory. */ void list_directory (char * command) { printf ("\nlist the current directory.\n"); strcat (command, "ls"); } /* exit program. */ void exit_program (char * command) { printf ("\nexit the program.\n"); strcat (command, "stop"); } /* enter choice. */ char enter_choice (char * command) { char choice; fflush (stdin); choice = getchar (); switch (choice) { case (DISPLAY): { display_file (command); break; } case (COPY): { copy_file (command); break; } case (RENAME): { rename_file (command); break; } case (REMOVE): { remove_file (command); break; } case (LIST): { list_directory (command); break; } case (EXIT): { exit_program (command); break; } default: { break; } } return choice; } /* this is the client program. */ void main (int argc, char * argv []) { int sock; struct sockaddr_in server; struct hostent * hp, * gethostbyname (); char choice; /* create socket. */ sock = socket (AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("opening stream socket."); exit (1); } /* connect socket. */ hp = gethostbyname (argv [1]); if (hp == 0) { printf ("%s, unknown host.\n", argv [1]); exit (2); } bcopy (hp -> h_addr, & server.sin_addr, hp -> h_length); server.sin_port = htons (atoi (argv [2])); server.sin_family = AF_INET; if (connect (sock, (struct sockaddr *) & server, sizeof (server)) < 0) { perror ("connecting stream socket."); exit (1); } /* print menu, enter a command, and send the command to the server. */ do { char command [MAX_LINEBUFFER] = ""; /* print menu. */ print_menu (); /* enter choice. */ choice = enter_choice (command); /* send the command to the server. */ if (write (sock, command, sizeof (command)) < 0) { perror ("writing on stream socket."); } } while (choice != EXIT); /* close the socket. */ close (sock); } /* filename: server.out author: anthony f. ortiz */ palazzo% server palazzo 12345 cat file1.txt this is file1. cp file2.txt backup2.txt mv file3.txt rename3.txt rm file1.txt ls backup2.txt client.c mail server client file2.txt rename3.txt server.c stop /* filename: client.out author: anthony f. ortiz */ palazzo% client palazzo 12345 file menu 1. display a file. 2. copy a file. 3. rename a file. 4. remove a file. 5. list a directory. 6. quit the program. enter a number from 1-6: 1 enter file name to display. file1.txt file menu 1. display a file. 2. copy a file. 3. rename a file. 4. remove a file. 5. list a directory. 6. quit the program. enter a number from 1-6: 2 enter existing file name. file2.txt enter new file name. backup2.txt file menu 1. display a file. 2. copy a file. 3. rename a file. 4. remove a file. 5. list a directory. 6. quit the program. enter a number from 1-6: 3 enter old file name. file3.txt enter new file name. rename3.txt file menu 1. display a file. 2. copy a file. 3. rename a file. 4. remove a file. 5. list a directory. 6. quit the program. enter a number from 1-6: 4 enter file name to remove. file1.txt file menu 1. display a file. 2. copy a file. 3. rename a file. 4. remove a file. 5. list a directory. 6. quit the program. enter a number from 1-6: 5 list the current directory. file menu 1. display a file. 2. copy a file. 3. rename a file. 4. remove a file. 5. list a directory. 6. quit the program. enter a number from 1-6: 6 exit the program.
BACK TO CS6560 PAGE.