Vous êtes sur la page 1sur 2

Distributed Systems Programming

Classroom Exercises 2 of 5
These exercises should be completed during supervised lab time and should be
handed in as an appendix to the major assignment at the end of week 12. The
combined total for all the exercises constitutes 25% of the multiphase
assignment mark.

1. Create a small console base java program which acts as a simple ftp
(type) server and client using sockets and a simple message passing
protocol.
a. You need to define a protocol that will instruct the server to:
i. Connect the client to the server.
ii. Display the contents of a directory to the client.
iii. Display the contents of a text file resident on the server to
the console of the client.
iv. End the session
b. Initially test your program on a single machine, once testing is
complete liaise with fellow students to test your machine across
multiple machines.

Useful code snippets

//
// File reading code from
// Code from http://www.javapractices.com/topic/TopicAction.do?Id=42
//

/** Read the contents of the given file. */


String read(String fFileName) throws IOException {

StringBuffer text = new StringBuffer();


String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new File(fFileName), fEncoding);
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
return text;
}

Nigel Edwards DSP Ex1 2010/11


//
// Directory listing code from:
//http://www.java2s.com/Code/Java/File-Input-
Output/ListingtheDirectoryContents.htm
//
public class Dir {
static int indentLevel = -1;

static void listPath(File path) {


File files[];
indentLevel++;

files = path.listFiles();

Arrays.sort(files);
for (int i = 0, n = files.length; i < n; i++) {
for (int indent = 0; indent < indentLevel; indent++) {
System.out.print(" ");
}
System.out.println(files[i].toString());
if (files[i].isDirectory()) {

listPath(files[i]);
}
}
indentLevel--;
}

public static void main(String args[]) {


listPath(new File("c:\\"));
}
}

2. Compare the use of TCP and UDP protocols java program, giving simple
code examples where appropriate.

Nigel Edwards DSP Ex1 2010/11

Vous aimerez peut-être aussi