SRS – Server Side Code Part 1

“A classroom response system (sometimes called a personal response system, student response system, or audience response system) is a set of hardware and software that facilitates teaching activities such as the following. A teacher poses a multiple-choice question to his or her students via an overhead or computer projector.
Each student submits an answer to the question using a handheld transmitter (a “clicker”) that beams a radio-frequency signal to a receiver attached to the teacher’s computer. Software on the teacher’s computer collects the students’ answers and produces a bar chart showing how many students chose each of the answer choices.
The teacher makes “on the fly” instructional choices in response to the bar chart by, for example, leading students in a discussion of the merits of each answer choice or asking students to discuss the question in small groups.” This text is taken from Vanderbilt University official website. Read More from here

Student Response system are getting popularity for in class assessment. Its easy to analyze and ensure student involvement in class room activities. Idea behind such application is to provide easy platform to answer a question posed by class teacher. Student can install android app and connect with teacher computer (all devices must be on same network ) via local network. Teacher side application displays server local IP address and port number. Student can connect via ip address by adding in mobile application. Once connected with server, teacher posed question will be transferred on student devices with multiple choices. Student response recorded on teacher side machine called server machine. Later teacher can visualize results.

We posted code in four different articles. You can read and obtain code from links given below :-
SRS – Server Side Code 1
SRS- Server Side Code Part2
SRS – Server Side Code Part 3
SRS – Android Client Code Part 4

Student Response System – Connection Manager Class

package serverMain;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import questions.*;
import services.Services;

public class ConnManager implements Runnable {
	ServerSocket serverSocket = null;
	Socket socket = null;
	DataInputStream dataInputStream = null;
	DataOutputStream dataOutputStream = null;
	int SERVERPORT;

	Services serv;
	Question question = null;
	GUI GI;

	public ConnManager(GUI gui, Services serv) {
		this.GI = gui;
		this.serv = serv;

		// if(this.serv!=null)
		question = serv.getCurrQuestion();

		if (question != null)
			GI.setQuestionText(question.getHTML());

		try {
			gui.ip.setText(getLocalIpAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public String getQuestion() {
		return this.question.getClientString();
	}

	// GETS THE IP ADDRESS OF YOUR SERVER
	public String getLocalIpAddress() throws UnknownHostException {
		return Inet4Address.getLocalHost().getHostAddress();
	}

	@Override
	public void run() {
		Socket socket = null;
		try {
			serverSocket = new ServerSocket(SERVERPORT);
			GI.port.setText("" + serverSocket.getLocalPort());
			// GI.status.setText("connected");
		} catch (IOException e) {
			e.printStackTrace();
		}
		while (!Thread.currentThread().isInterrupted()) {

			try {

				socket = serverSocket.accept();
				GI.setLogText("client connected" + socket.getInetAddress() + "\n");
				OutputThread output = new OutputThread(socket);
				new Thread(output).start();
				InputThread input = new InputThread(socket);
				new Thread(input).start();

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	class InputThread implements Runnable {

		private Socket clientSocket;

		private BufferedReader input;

		public InputThread(Socket clientSocket) {

			this.clientSocket = clientSocket;

			try {
				this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		public void run() {
			try {
				while (!Thread.currentThread().isInterrupted()) {

					String read = input.readLine();
					if (read != null) {
						GI.setLogText(read + "\n");
						serv.recordAnswer(read, clientSocket.getInetAddress().toString());
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	class OutputThread implements Runnable {

		private Socket clientSocket;
		private PrintWriter output;

		public OutputThread(Socket clientSocket) {

			this.clientSocket = clientSocket;

			try {
				output = new PrintWriter(this.clientSocket.getOutputStream(), true);

			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		public void run() {
			output.println(getQuestion());
			output.flush();
			try {
				GI.setLogText("Message Sent " + getLocalIpAddress() + "\n");

			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
	}

}

Student Response System – GUI Designed using netbeans GUI Editor

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package serverMain;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.*;

import org.jfree.ui.RefineryUtilities;

import questions.*;
import services.FileService;
import services.Services;
import visualize.VisualizResults;

/**
 *
 * @author Gulraiz
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates and open the template
 * in the editor.
 */

public class GUI extends JFrame implements WindowListener, ActionListener {

	/**
	 * 
	 */

	Thread conThr;
	Services serv;
	FileService fserv;
	ConnManager con;
	private static final long serialVersionUID = 1L;

	/**
	 * Creates new form GUI2
	 */
	public GUI() {

		initComponents();
		serv = new Services();

		// read data from serialized file
		fserv = new FileService("e:/", serv);
		if (fserv.isFileAvailable())
			serv = fserv.deserializeServices();

		// Window action listner
		addWindowListener(this);
		question.addActionListener(this);
		backup.addActionListener(this);
		next_qu.addActionListener(this);
		connect.addActionListener(this);
		visualize.addActionListener(this);
		// Connecting server on specified port
		con = new ConnManager(this, serv);
		conThr = new Thread(con);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// 
	private void initComponents() {

		jSeparator4 = new javax.swing.JSeparator();
		jToolBar1 = new javax.swing.JToolBar();
		ip = new javax.swing.JTextField();
		jSeparator1 = new javax.swing.JToolBar.Separator();
		port = new javax.swing.JTextField();
		jSeparator3 = new javax.swing.JToolBar.Separator();
		status = new javax.swing.JTextField();
		jSeparator2 = new javax.swing.JToolBar.Separator();
		connect = new JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		question_box = new JLabel();
		jScrollPane2 = new javax.swing.JScrollPane();
		log = new JLabel();
		visualize = new javax.swing.JButton();
		question = new javax.swing.JButton();
		next_qu = new javax.swing.JButton();
		backup = new javax.swing.JButton();
		export = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jToolBar1.setRollover(true);

		ip.setColumns(15);
		ip.setText("ip");
		ip.setBackground(new Color(240, 240, 240));
		jToolBar1.add(ip);
		jToolBar1.add(jSeparator1);

		port.setColumns(10);
		port.setBackground(new Color(240, 240, 240));
		port.setText("8887");
		jToolBar1.add(port);
		jToolBar1.add(jSeparator3);

		status.setColumns(10);
		status.setText("Disconnected");
		status.setBackground(new Color(240, 240, 240));
		jToolBar1.add(status);
		jToolBar1.add(jSeparator2);

		connect.setText("Connect");
		connect.setActionCommand("Connect");
		connect.setFocusable(false);
		connect.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		connect.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		jToolBar1.add(connect);

		question_box.setBackground(new java.awt.Color(240, 240, 240));
		question_box.setBorder(javax.swing.BorderFactory.createTitledBorder("Current Question"));
		jScrollPane1.setViewportView(question_box);

		log.setBackground(new java.awt.Color(240, 240, 240));
		log.setBorder(javax.swing.BorderFactory.createTitledBorder("Log"));
		jScrollPane2.setViewportView(log);

		visualize.setText("Visualize Results");
		visualize.setActionCommand("visualize");
		question.setText("Add Questions");

		next_qu.setText("Get Question");

		backup.setText("Backup Data");

		export.setText("Export Results");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
						Short.MAX_VALUE)
				.addGroup(layout.createSequentialGroup().addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addComponent(backup, javax.swing.GroupLayout.DEFAULT_SIZE, 120,
												Short.MAX_VALUE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(export, javax.swing.GroupLayout.DEFAULT_SIZE, 120,
												Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(next_qu, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(question, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								// .addGap(8, 8, 8)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(visualize, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
						// .addGap(18, 18, 18))
						.addGroup(layout.createSequentialGroup().addComponent(jScrollPane1).addGap(14, 14, 14)))
						.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 309,
								javax.swing.GroupLayout.PREFERRED_SIZE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 38,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jScrollPane2)
								.addGroup(layout.createSequentialGroup().addComponent(jScrollPane1,
										javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
								.addGap(7, 7, 7)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(question, javax.swing.GroupLayout.DEFAULT_SIZE, 48,
														Short.MAX_VALUE)
												.addComponent(visualize, javax.swing.GroupLayout.DEFAULT_SIZE, 48,
														Short.MAX_VALUE))
										.addComponent(next_qu, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(backup, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(export, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGap(9, 9, 9)))));

		pack();
	}// 

	/**
	 * @param args
	 *            the command line arguments
	 */
	public void loadGI() {
		/* Set the Nimbus look and feel */
		// 
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
		 * html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		// 

	}

	// Variables declaration - do not modify
	private JButton backup;
	private JButton connect;
	private JButton export;
	public JTextField ip;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JToolBar.Separator jSeparator1;
	private JToolBar.Separator jSeparator2;
	private JToolBar.Separator jSeparator3;
	private JSeparator jSeparator4;
	private JLabel question_box;
	private JLabel log;
	private JToolBar jToolBar1;
	public JTextField port;
	private JButton question;
	private JButton next_qu;
	public JTextField status;
	private JButton visualize;
	// End of variables declaration

	public void setQuestionText(String str) {
		question_box.setText(str);
	}

	public void setLogText(String txt) {
		String str = "";
		if (log.getText().isEmpty())
			str = "";
		log.setText(str + log.getText() + "
" + txt); } @Override public void windowActivated(WindowEvent arg0) { } @Override public void windowClosed(WindowEvent arg0) { } @Override public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Add Questions")) { QuestionWindow gui = new QuestionWindow(this.serv); gui.loadWindow(); gui.setVisible(true); } else if (e.getActionCommand().equals("Get Question")) { String s = (String) JOptionPane.showInputDialog(this, "Select a category:\n" + "\"for example Java class...\"", "Customized Dialog", JOptionPane.PLAIN_MESSAGE, null, this.serv.getQuestionKeys(), null); if (s != null) { ArrayList qlist = this.serv.getQuestions(s); int randNum = 0 + (int) (Math.random() * qlist.size()); con.question = qlist.get(randNum); this.serv.setCurrQuestion(con.question); setQuestionText(con.question.getHTML()); } } else if (e.getActionCommand().equals("visualize")) { VisualizResults chart = new VisualizResults("Student Response Statistics", this.serv); } else if (e.getActionCommand().equals("Connect")) { con.SERVERPORT = Integer.parseInt(port.getText()); connect.setText("Disconnect"); connect.setActionCommand("Disconnect"); status.setText("connected : Session id is :" + this.serv.createSession()); if (!conThr.isAlive()) conThr.start(); } else if (e.getActionCommand().equals("Disconnect")) { connect.setText("Connect"); connect.setActionCommand("Connect"); status.setText("Disconnected"); if (conThr.isAlive()) conThr.stop(); } else fserv.serializeServices(); } private void update(GUI gui) { // TODO Auto-generated method stub } }

No Responses