write a SUN RPC based remote file server program to demonstrate its use. Write a
ID: 3639588 • Letter: W
Question
write a SUN RPC based remote file server program to demonstrate its use. Write a single server (server) and two different client (rget and rput) programs.Server:
This is a stateful remote file server. It implements four remote procedures: open, read, write, and close. All files are read from and written into the directory in which the server is running.
rget:
This client implements fetching a remote file. The usage is:
rget remote_host remote_file local_file
The command will open the remote_file and issue one or more read requests to the server, writing the results to local_file.
rput:
This client implements sending a file to the remote server. The usage is:
rput remote_host local_file remote_file
The command will open the local_file for reading, open remote_file for writing, and read bytes from local_file, sending write requests to the server.
Your remote interface (RPC IDL) is defined as follows:
/* filerpc.x - description of remote file access */
const MAXNAME = 128; /* max length of file name */
const MAXBUF = 512; /* max size of read/write buffer */
typedef string filename<MAXNAME>; /* filename type */
typedef int handle; /* remote file handle type */
struct openargs { /* parameters for open */
filename file;
int mode;
};
struct readargs { /* parameters for read */
handle h;
int nbytes;
};
union readresults switch(int status) { /* return values from read */
case 0: opaque buf<MAXBUF>;
case 1: int error;
};
struct writeargs { /* parameters for write */
handle h;
opaque buf<MAXBUF>;
};
union writeresults switch(int status) { /* return values from write */
case 0: int nbytes;
case 1: int error;
};
program FRPC_PROG {
version FRPC_VERS {
handle OPEN(openargs) = 1; /* open file */
bool CLOSE(handle) = 2; /* close file */
readresults READ(readargs) = 3; /* read bytes */
writeresults WRITE(writeargs) = 4; /* write bytes */
} = 1;
} = 0x1NNNNNNN; /* program number goes here */
The only part of this file you should modify is the program number on the last line.
server
Your server implements the remote procedures defined in above IDL file.
open
• The server maintains a table of file descriptors, one for each open file. An open request returns a file handle that is an index into this table. The table is limited to supporting a maximum of ten open files. Any additional open requests should be rejected.
• Open accepts a file name and a mode for opening the file.
o A file name is limited to a maximum of MAXNAME characters (symbol defined in above IDL) and cannot contain the character '/'.This restricts all files to the current directory.
o The mode for open is any of the valid modes for the open system call. Typically these will be O_RDONLY for reading or O_WRONLY|O_CREAT for create & write.
• If there is no free file handle available, the open request will fail immediately, returning a -1.
• If the open system call succeeds, a free table entry in the table of file descriptors is populated with the file descriptor returned by open and the handle (index of the entry) is returned to the requester.
read
• A read request contains a file handle (obtained from open) and a number indicating the maximum number of bytes to read from the file.
• If the file handle is invalid (does not correspond to an open file or is out of range), an error code of 1000 is returned.
• Up to MAXBUF bytes can be read. If the nbytes field is greater than that, MAXBUF bytes will be used instead.
• If the read system call succeeds, then the buffer is returned along with the number of bytes that were actually read (which may be 0 for end of file). The return parameter is a variable sized buffer, so you need to set the buf_len member to the number of bytes and the buf_val member to the buffer. The status element of the return structure is set to 0 to indicate success.
• If the read system call fails, the status element of the return structure is set to 1 to indicate failure. Instead of a return buffer, the error element is set to the value of errno (error code from the last system call; you need to #include <errno.h> in your server code).
write
• A write request contains a file handle (obtained from open) and a variable size buffer containing the bytes to be written to the file. Since this is a variable sized buffer, your client programs will need to set the buf_len member to the number of bytes and the buf_val member to the data to be written.
• If the file handle is invalid (does not correspond to an open file or is out of range), an error code of 1000 is returned.
• If the write system call succeeds, the number of bytes with the number of bytes that were actually written is returned in the nbytes field. The status field of the return structure is set to indicate success.
• If the write system call fails, the status element of the return structure is set to 1 to indicate failure. The error element is set to the value of errno.
close
• A close request contains a file handle (obtained from open) and returns TRUE or FALSE.
• If the file handle is invalid (does not correspond to an open file or is out of range), a value of FALSE is returned.
• Otherwise, the corresponding file descriptor is closed with the close system call and a TRUE is returned.
rget
The rget client fetches a file from the server using the abovementioned remote procedure calls.
• The command accepts three parameters: the name of the host running the server, the remote file name, and the local file name.
• The remote file is opened for reading (O_RDONLY). If this succeeds, the local file is created.
• Data from the remote file is read a block at a time and written to the local file.
rput
The rput client fetches a file from the server using the abovementioned remote procedure calls.
• The command accepts three parameters: the name of the host running the server, the local file name, and the remote file name.
• The local file is opened. If this succeeds, the remote file is then opened for creation (O_WRONLY|O_CREAT).
• Data from the local file is read a block at a time and written to the remote file.