Friday, April 17, 2020

Project Explanation videos

Course project title: LAN based examination system using java socket programming.

Description:
In LAN based examination system we have implemented socket programming, where a Client connects, sends request to server and the server accept request, verify the client’s username and password, then sends mcq based exam-test to client using socket connection. After completion of test, calculation of total marks for exam is also done at server side and server sends the marks of exam immediately to that particular client. For the implementation of entire project we have used JAVA socket programming, because java is a general-purpose, concurrent, object-oriented, class-based.

Project Report Link: LAN based examination system using java socket programming.
Project Code Link: https://drive.google.com/open?id=1Vsv-vnXue2D20K2VfLno6dwT3z5smiSa

So, this last blog will explain the LAN based examination in detail with running the entire java code for the project through the videos. The links for the explanation video by project authors are given below.

Topic-1: Introduction
This video is by author Tanmay Chandan.(Division: TY-L,    Roll no: 09)
Link : https://youtu.be/UpA9z0m496Y


Topic-2: How server and client work
This video is by author Bushra Husain. (Division: TY-L,    Roll no: 27)
Link: https://drive.google.com/file/d/1u_USRaJwsXCJdUrAqB5OSlbUTfZ_bua1/view


Topic-3: Creation of server and client, execution of project code.
This video is by author Rutuja Ingale.(Division: TY-L,    Roll no: 28)
Link: https://youtu.be/CvZHPR4F7ZU


Presentation Slide Link: Real Time Application Of Object Oriented Programming.

Thankyou!!

Saturday, March 28, 2020

Java code for GUI of test-exam


Java code for creation of GUI using JFrame for client to give test-exam and submit it.


GUI of Test-exam


package client;

import java.awt.Color;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;



public class Test extends JFrame {

 

public Test()
{

this.setVisible(true);
this.setLayout(null);

this.setTitle("Test");
this.setLocation(650, 250);
this.setSize(800, 800);


JLabel lbl1;
lbl1=new JLabel("Question :");
lbl1.setBounds(100, 70, 100, 30);
this.add(lbl1);//imp
 
JTextField qField=new JTextField();
qField.setBounds(200,65, 250, 30);
     this.add(qField);

             
             JLabel lbl2;
lbl2=new JLabel("Options :");
lbl2.setBounds(100, 160, 100, 30);
this.add(lbl2);//imp

     JRadioButton rd1=new JRadioButton("a");
     rd1.setBounds(200, 200, 100, 30);
      
     this.add(rd1);
     
     
     JRadioButton rd2=new JRadioButton("b");
     rd2.setBounds(200, 250, 100, 30);
     this.add(rd2);
     
             JRadioButton rd3=new JRadioButton("c");
     rd3.setBounds(200, 300, 100, 30);
     this.add(rd3);
             
             JRadioButton rd4=new JRadioButton("d");
     rd4.setBounds(200, 350, 100, 30);
     this.add(rd4);
     
     ButtonGroup bg=new  ButtonGroup();
     bg.add(rd1);
     bg.add(rd2);
             bg.add(rd3);
             bg.add(rd4);
             
             JButton next = new JButton("NEXT");
             next.setBounds(600,600,100,50);
             this.add(next);

}


public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();


}

}

Monday, March 23, 2020

Java code for GUI for Login Page

Java code for creation of GUI for LAN based examination system's client side using JFrame.

GUI of Login page


package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class InterfaceClass extends JFrame implements ActionListener{
   String username;
        String password;
        JTextField txtuser;
JPasswordField pass;
JLabel luser;
JLabel lpass;
JButton blogin;
        JFrame fm;

    
public void LoginPage()
{
        
                fm = new JFrame();
fm.setVisible(true);
fm.setLocation(700, 350);
fm.setLayout(null);
fm.setSize(550,300);
fm.setTitle("Login");

luser = new JLabel("User Name : ");
fm.add(luser);
luser.setBounds(130,60,150,30);

txtuser = new JTextField();
fm.add(txtuser);
txtuser.setBounds(270,60,150,30);

                lpass = new JLabel("Password   : ");
fm.add(lpass);
lpass.setBounds(130,100,150,30);

pass = new JPasswordField();
fm.add(pass);
pass.setBounds(270,100,150,30);

blogin = new JButton("Login");
fm.add(blogin);
blogin.setBounds(200,200,150,30);
// blogin.addActionListener(new ActionListener() {
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    
//                    bloginAction(e);
//                  //  username = txtuser.getName();
//               //     password = pass.getPassword().toString();
//                }
//                });
blogin.addActionListener(this);
}

    @Override
    public void actionPerformed(ActionEvent e) {
        username = txtuser.getName();
        password = pass.getPassword().toString();   
    
        System.out.println("Inactionper");}
}

Friday, March 20, 2020

Creation of GUI for client

A graphical user interface starts with a top-level container which provides a home for the other components of the interface, and dictates the overall feel of the application. We have used the JFrame class, which is used to create a simple top-level window for a Java application.

To use the Jframe you need to follow following steps:

1. Import the Graphical Components

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;

Java comes with a set of code libraries designed to help programmers quickly create applications. They provide access to classes that perform specific functions, to save you the bother of having to write them yourself. The two import statements above let the compiler know that the application needs access to some of the pre-built functionality contained within the "AWT" and "Swing" code libraries.
AWT stands for “Abstract Window Toolkit.” It contains classes that programmers can use to make graphical components such as buttons, labels and frames. Swing is built on top of AWT, and provides an additional set of more sophisticated graphical interface components. With just two lines of code, we gain access to these graphical components, and can use them in our Java application.


2. Create the Application Class
Enter the class definition that will contain our Java application code. 
public class InterfaceClass extends JFrame implements ActionListener{.............}
All the rest of the code goes between the two curly brackets. The InterfaceClass class is like the covers of a book, it shows the compiler where to look for the main application code.

3. Create the Function that Makes the JFrame
Group all the Java code that deals with creating the window into one function.
Enter the LoginPage() function definition:
private void LoginPage()() {..........}
All the code to create the window for Login page goes between the function’s curly brackets. Anytime the  LoginPage() function is called, the Java application will create and display a window using this code.
Now, let's look at creating the window using a JFrame object. Type in the following code, remembering to place it between the curly brackets of the LoginPage() function:
Jframe fm = new JFrame();
What this line does is create a new instance of a JFrame object called "fm". You can think of "fm" as the window for our Java application.
The JFrame class will do most of the work of creating the window for us. It can be used to set  its attributes, such as its general appearance, its size, what it contains, and more.

4. Add a JLabel to the JFrame
Since an empty window has little use, let's now put a graphical component inside it. Add the following lines of code to the  LoginPage() function to create a new JLabel object
                 JLabel luser = new JLabel("User Name : ");
luser.setBounds(130,60,150,30);

A JLabel is a graphical component that can contain an image or text. It is filled with the text “User Name :” and its size has been set by setBounds.
Now that we have created the JLabel, add it to the JFrame:
fm.add(luser);
this will add our JLabel to th JFrame.
In the next blog we have shared the entire java code of database for LAN based examination system.

Thankyou!!

Monday, March 16, 2020

How to handle multiple clients

Why to use threads in network programming?
The reason is simple, we don’t want only a single client to connect to server at a particular time but many clients simultaneously. We want our architecture to support multiple clients at the same time. For this reason, we must use threads on server side so that whenever a client request comes, a separate thread can be assigned for handling each request. With our basic server-client program, the request which comes even a nano-second first would be able to connect to the server and the other request would be rejected as no mechanism is provided for handling multiple requests simultaneously. To overcome this problem, we use threading in network programming.

Socket Programming provides one to one as well as one to many client server communication. In various application we need to connect multiple client with server. For example, In LAN examination system that we are going to discuss in, we need to connect multiple students with server.

One of the way to handle multiple clients is via multithreading . As most of the platform provides one to many model in which one kernal level thread is created and many threads depending on requirement can be created by that kernal level thread.


Fig. Implementation of multiple clients.

Server class :
 
The main server implementation is easy and similar to the previous article. The following points will help understand Server implementation : 
1. The server runs an infinite loop to keep accepting incoming requests.
2. When a request comes, it assigns a new thread to handle the communication part.
3. The sever also stores the client name into a vector, to keep a track of connected devices. The vector stores the thread object corresponding to the current request. The helper class uses this vector to find the name of recipient to which message is to be delivered. As this vector holds all the streams, handler class can use it to successfully deliver messages to specific clients.
4. Invoke the start() method.

            try 
            {
                // socket object to receive incoming client requests
                s = ss.accept();
                  
                System.out.println("A new client is connected : " + s);
                  
                // obtaining input and out streams
                DataInputStream dis = new DataInputStream(s.getInputStream());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                  
                System.out.println("Assigning new thread for this client");
  
                // create a new thread object
                Thread t = new ClientHandler(s, dis, dos);
  
                // Invoking the start() method
                t.start();
                  
            }
            catch (Exception e){
                s.close();
                e.printStackTrace();
            }

ClientHandler class : Similar to previous article, we create a helper class for handling various requests. This time, along with the socket and streams, we introduce a name variable. This will hold the name of the client that is connected to the server. The following points will help understand ClientHandler implementation :
1. Whenever the handler receives any string, it breaks it into the message and recipient part. It uses Stringtokenizer for this purpose with ‘#’ as the delimiter. Here it is assumed that the string is always of the format:
2. Message #recipent.
3. It then searches for the name of recipient in the connected clients list, stored as a vector in the server. If it finds the recipients name in the clients list, it forwards the message on its output stream with the name of the sender prefixed to the message.
 
try {
              
                // receive the input from client
                received = dis.readUTF();
                  
                if(received.equals("Exit"))
                
                    System.out.println("Client " + this.s + " sends exit...");
                    System.out.println("Closing this connection.");
                    this.s.close();
                    System.out.println("Connection closed");
                    break;
                }
                  
                // creating Date object
                Date date = new Date();
                  
                // write on output stream based on the
                // answer from the client
                switch (received) {
                  
                    case "ans1" :
                        toreturn = fordate.format(date);
                        dos.writeUTF(toreturn);
                        break;
                          
                    case "ans2" :
                        toreturn = fortime.format(date);
                        dos.writeUTF(toreturn);
                        break;
                          
                    default:
                        dos.writeUTF("Invalid input");
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
         }