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

Java multi-thread file server and client

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

Problem

My task was to build a multi-thread file server and a client that can upload or download a named file over sockets. It is assumed that the client will finish after its operation and there is no need to supply a file list from the server (although I plan to add that). There is no error check if the client enters a file name that does not exist on the server.

I think that the basic protocol I've set up is ugly at best and would like opinions on a better way to approach this.

I'm quite sure this is barely doing the job, so criticism is welcomed.

Flow:

  • Start Server



  • Start Client, connection made if possible.



  • Client then chooses whether to upload or download a file.



  • Server receives this initial message and takes appropriate action.



FileServer

public class FileServer {

    private static ServerSocket serverSocket;
    private static Socket clientSocket = null;

    public static void main(String[] args) throws IOException {

        try {
            serverSocket = new ServerSocket(4444);
            System.out.println("Server started.");
        } catch (Exception e) {
            System.err.println("Port already in use.");
            System.exit(1);
        }

        while (true) {
            try {
                clientSocket = serverSocket.accept();
                System.out.println("Accepted connection : " + clientSocket);

                Thread t = new Thread(new CLIENTConnection(clientSocket));

                t.start();

            } catch (Exception e) {
                System.err.println("Error in connection attempt.");
            }
        }
    }
}


CLIENTConnection

```
public class CLIENTConnection implements Runnable {

private Socket clientSocket;
private BufferedReader in = null;

public CLIENTConnection(Socket client) {
this.clientSocket = client;
}

@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(
clientSocket.

Solution

The basic client / server code looks pretty good.

If you want clearer vision for "something" in java, you need to start defining the boundary of that "something". In Java the ideal choice is the Class.

Start by refactoring your code to separate what you consider the "Protocol" into a "Protocol" class. Later on, if you find that you want to support more than one "Protocol" write a second "ProtocolTwo" class, rename the "Protocol" class to something like "ProtocolOne" and make a common shared interface between the two called "Protocol".

My imaginings for a "Protocol" class that would work with your code:

Protocol protocol = new Protocol();
Action action = protocol.readAction(sock.getOutputStream());
action.perform();


Of course, it is homework, so you'll get the joy of putting all the important bits into the right places. Good luck, and post back as you get closer to the goal.

Code Snippets

Protocol protocol = new Protocol();
Action action = protocol.readAction(sock.getOutputStream());
action.perform();

Context

StackExchange Code Review Q#20961, answer score: 3

Revisions (0)

No revisions yet.