HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Multithreaded Client-Server file downloading application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileapplicationdownloadingclientservermultithreaded

Problem

A multithreaded client server program to download image files. If you want to execute it on your machine you should change the file paths. Since there are four files to download the client makes the same number of connection attempts. The files sent by the FileServer will get repeated after the fourth connection attempt. In the File Server client app the file saving is done in new threads so as to not hamper the file downloading process. There is little interaction between client-server.

Here is the FileServer...

```
public class FileServer {
private final ExecutorService exec = Executors.newCachedThreadPool();

final String[] fileNames = {
"C:\\Users\\clobo\\Pictures\\Ex1.jpg",
"C:\\Users\\clobo\\Pictures\\Ex2.jpg",
"C:\\Users\\clobo\\Pictures\\Ex3.jpg",
"C:\\Users\\clobo\\Pictures\\Ex4.jpg"

};

public void start() throws IOException {
ServerSocket socket = new ServerSocket(7777);
System.out.println("Waiting for client message...");

while (!exec.isShutdown()) {
try {
for (final String fileName : fileNames){
final Socket conn = socket.accept();

exec.execute(new Runnable() {
public void run() {
sendFile(conn,fileName);

}
});
}
} catch (RejectedExecutionException e) {
if (!exec.isShutdown())
log("task submission rejected", e);
}
}
}

public void stop() {
System.out.println("Shutting down server...");
exec.shutdown();
}

private void log(String msg, Exception e) {
Logger.getAnonymousLogger().log(Level.WARNING, msg, e);
}

public void sendFile(Socket conn, String fileName) {
File myFile = new File(fileName);
if (!myFile.exists()) {
log("File does not exist!"

Solution

Is this code really doing what is advertised?

ObjectOutputStream.write(File) is writing the File object, not the contents of the file. So what we have here is the server pushing the path of a file to the client. The client code then creates a FileDialog, which doesn't wait for the user's input to get a second path on the client? Then a thread on the client runs to copy client(sourcePath) to client(destinationPath).

If you want the server to send the file to the client, then at some point the server needs to open the file, and write the contents into the connection.outputStream.

If two clients were trying to reach the server at the same time, they would each get a random list of file names, depending on the race condition in their own thread pools.

Server.stop, in addition to shutting down the Executor, should also signal start() to quit accepting connections.

Server.start() should probably be spelled "run()", given that it is an unending loop on the main thread. start() would be an acceptable spelling if socket.accept were running on a different thread (for instance, in your Executor).

Context

StackExchange Code Review Q#19113, answer score: 3

Revisions (0)

No revisions yet.