views:

47

answers:

2

How can I Submit client side computer user's answers(to a multiple choice question) to the server using JAVA

  1. I have a centralized server and about 1000 client systems.
  2. In these 1000 systems students take multiple choice quiz at at time (in some 2 hrs time).
  3. Now i've to send all these answers of these questions to the server in an asynchronous threaded queue when the student answer each question (all 1000 students)
  4. Also client have to wait if the server connection is a failure, in this case students should be able to continue taking quiz/exam. When I get the connection these answers in queue should be submitted to the server system.

How can I solve this problem? Please suggest/help me in this.

A: 

First, the server has to listen on a certain port for certain requests. Most straightforward choice would be a webserver listening on HTTP requests on port 80. You can use Apache Tomcat as webserver and Java servletcontainer for this and create a Java Servlet which listens on a certain url-pattern.

Then, in your client application you can make use of java.net.URLConnection to fire a HTTP request to the Servlet. You can make use of request parameters to send information to the Servlet. E.g.

String param1 = URLEncoder.encode("param1", "UTF-8");
String param2 = URLEncoder.encode("param2", "UTF-8");
String query = String.format("param1=%s&param2=%s", param1, param2);
String servleturl = "http://example.com/context/servlet";

// To fire GET request, do so:
URLConnection connection = new URL(servleturl + "?" + query).openConnection;
InputStream response = connection.getInputStream();

// Or, to fire POST request, do so:
URLConnection connection = new URL(servleturl).openConnection;
connection.setDoOutput(true);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(query.getBytes("UTF-8"));
writer.close();
InputStream response = connection.getInputStream();

In the Servlet, to gather parameters from a GET request, implement doGet() as follows:

String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");

For POST requests, do the same in doPost() instead.

Hope this helps.

BalusC
A: 

It depends which kind of connections you want you use, if you remember Java has either blocking I/O (java.net.Socket, java.net.ServerSocket) and unblocking I/O throught channels (java.nio.channel.SocketChannel)

In a blocking I/O approach every client would have its own producer/consumer handling, you can have:

  • a thread that consumes answers that are queued locally and tries to dispatch them to the server
  • the GUI that cares about placing items in the answer's queue

whenever your consumer thread is able to send an answer to the server, it removes it from the queue

While on the server you can process answers in a common place:

  • you have a thread that processes answers received from all clients (you can accumulate them in a buffer) and it puts itself in wait when the buffer is empty.
  • every connection from a client has it's own thread that receives answers and places them in the process queue.

This approach is safe but it's quite heavy weight. You can think about opening a connect just for the time of an answer, dispatch it to the server and then close.

Otherwise you can simply use UDP (that is actually better for this kind of problem) but you will have to think about a way to send acknowledgments back to the clients whenever your server receives an answers.

For example:

  • student gives an answer: the answer is added to the client buffer
  • client thread tries to send its buffer over UDP
  • whenever a client receives an acknowledge for an answers it removes it from the buffer
  • it tries periodically to send still unacknowledged answers

On server side:

  • whenever it receives an answer datagram from a client accumulate it on the process buffer
  • you have a thread that processes all the answers on the buffer
  • the server will send back an ackknowledge to the client to warn that answer has been received

But this is still not sure because the ackowledge can be missed on the net.

In any case you could use ObjectInputStream and ObjectOutputStream to pass directly objects between clients and server. So you can think an encapsulated object like

class Answer
{
   int studentId;
   int questionId;
   int answer;
}
Jack
With todays network speed I see no point in using UDP. TCP is sufficiently fast for almost every task.
pajton