0
votes

I am implementing a multithreaded UDP client-server dictionary. I think I have implemented it correctly but I don't know how to test it properly. If anyone has the time, can you kindly have a quick look at my code?

This is how I usually run my program:

java DictServer <port> <dictionary file name>
java DictClient localhost <port> <word to search>

This the output of the server (client has been run 3 times here):

Server Started
Number of threads active: 1
Number of threads active: 2
Number of threads active: 3
Number of threads active: 4
Number of threads active: 5
Thread-0 just run.
Number of threads active: 5
Thread-1 just run.
Number of threads active: 5
Thread-3 just run.
Number of threads active: 5

As you can see, the output seems fine. I am keeping the thread number at maximum (5) because it is meant to be a "Worker Pool Model". However in UDP there is no 'active connection' only packets sent and received. Once the client gets its packet, the thread is closed. This happens very fast so I can't actually test multiple clients connecting simultaneously. Any suggestions?

I also use a setter to update the number of threads. But I invoke it using
"DictServer.decNumThreads()", is this bad?

My Code:

Server Class:

public class DictServer {

private static int threads = 0;

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

    // Connection Parameters
    DatagramSocket socket = null;
    int maxThreads = 5;             // Max threads at any time

    // Retrieve user input
    int serverPort = Integer.parseInt(args[0]);     // Convert string to int
    String dictionaryFile = args[1];

    try {
        // Setup socket
        socket = new DatagramSocket(serverPort);
        System.out.println("Server Started");

        while(true) {
            if(threads < maxThreads) {
                ServerThread server = new ServerThread(socket, dictionaryFile);
                new Thread(server).start();
                threads++;
                System.out.println("Number of threads active: " + threads);
            }               
        }
    }
    catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    finally {
        if(socket != null) 
            socket.close();
    }
}

// Setter for number of active threads
public static void decNumThreads() {
    threads--;
}
}

Threading class:

public class ServerThread implements Runnable {

private DatagramSocket socket = null;
private String dictionaryFile;

// Constructor
public ServerThread(DatagramSocket socket, String dictionaryFile) {
    this.socket = socket;
    this.dictionaryFile = dictionaryFile;
}

@Override
public void run() { 


    byte[] word = new byte[1000];
    byte[] definition = new byte[1000];

    try {
        // Get client request
        DatagramPacket request = new DatagramPacket(word, word.length);
        socket.receive(request);

        // Retrieve definition from dictionary file
        if(word != null) 
            definition = getDefinition(new String(word), dictionaryFile);

        // Put reply into packet, send packet to client
        DatagramPacket reply = new DatagramPacket(definition, definition.length, request.getAddress(), request.getPort());
        socket.send(reply);

    }
    catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    System.out.println(Thread.currentThread().getName() + " just run.");
    DictServer.decNumThreads();
}
1
You have written some code and you want us to test it? Automate your client/s and so create your own test system. When you get a problem, call back with code, symptoms, error messages/exceptions and what you have done to that point to debug it. - Martin James
As I noted above, I have already ran the client and received the correct output. I just had no idea how to test the threading function of my server so I asked here for some insight. I posted my code because I thought it would help me get an answer. I will try to automate my clients. - pakmon

1 Answers

0
votes

Your first while (true) loop that creates the threads is futile. Once it has reached the maximum number of threads to start, it just sits there burning the CPU at 100% usage.