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();
}