Problem Statement:
Develop a client server application for to authenticate users. Let the client side use an
applet to catch username and password and send to the server. The server verifies the authenticity and returns a message. Show this message at the client side.
Input and Output Requirements:
The client accepts username and password through the keyboard. The server authenticates this and sends a message to the client.
Algorithm: server side
Begin
Create an object of the server socket class and listen for incoming client connection in a specified port.
When a client connection is established, create input and output streams for this connection.
While (true)
a. Read username and password from the input stream.
b. Authenticate the username and the password.
c. If username and password match then return the message “valid username and password” else return the message “invalid username and password”.
d. Close server and client socket.
End
Algorithm: client side
Begin
Establish a connection to the server program. Use IP address and port number of the server.
Create input and output streams for the established connection.
Read username and password from keyboard and send to server.
Display the message returned from the server.
Close the connection.
End
Program:
//Server side import java.net.*; import java.net.Socket; import java.io.*; import java.lang.String; class Serverlogap implements Runnable{ Socket client; BufferedReader instream; DataOutputStream outstream; String username,password; Serverlogap(Socket sock) { Thread t; client=sock; t=new Thread(this); t.start(); username="MCA"; password="123"; } public static void main(String args[]) { ServerSocket sersock=null; System.out.println("Server Started"); try { sersock=new ServerSocket(4000); } catch(IOException ie) { System.out.println("Unable to create Server Socket"); } while(true) { Socket sock; try { sock=sersock.accept(); new Serverlogap(sock); } catch(IOException e) { System.out.println("Unable to establish connection"+e); } } } public void run() { String un,pw; try { instream=new BufferedReader(new InputStreamReader(client.getInputStream())); outstream=new DataOutputStream(client.getOutputStream()); } catch(IOException e) { System.out.println("Unable to create input/output streams"); } try { un=instream.readLine().trim(); pw=instream.readLine().trim(); System.out.println("Received Password "+pw); if((un.equals(username))&&(pw.equals(password))) { outstream.writeBytes("Connected to Server.........\n"); System.out.println("Connected to client..........."+client.getInetAddress().getHostAddress()); } else { outstream.writeBytes("Invalid Username/Password\n"); } outstream.write(20); } catch(IOException e) { System.out.println("Error in Reading "+e); } try { client.close(); } catch(IOException e) { System.out.println("Error in closing "+e); } } } |
//Client side //<APPLET CODE="Clientlogapplet.class" WIDTH=400 HEIGHT=100> </APPLET> import java.net.*; import java.net.Socket; import java.io.*; import java.net.UnknownHostException; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Clientlogapplet extends Applet implements ActionListener { Label usnm=new Label("User Name"); Label psw=new Label("Password"); TextField uname=new TextField(20); TextField pword=new TextField(20); String str=""; Socket client=null; BufferedReader inp=null; DataOutputStream output=null; public void init() { pword.setEchoChar('*'); Button Submit=new Button("Submit"); setLayout(new GridLayout(3,2,5,5)); add(usnm); add(uname); add(psw); add(pword); add(Submit); Submit.addActionListener(this); try { InetAddress addr=InetAddress.getByName("localhost"); client=new Socket(addr,4000); } catch(IOException e) { System.out.println("Unable to connect to Server"); } try{ inp=new BufferedReader(new InputStreamReader(client.getInputStream())); output=new DataOutputStream(client.getOutputStream()); } catch(IOException e){ System.out.println("Unable to open input/output streams"); } } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand()=="Submit") { try{ str=uname.getText(); output.writeBytes(str); output.write(13); output.flush(); str=pword.getText(); output.writeBytes(str); output.write(10); output.flush(); str=inp.readLine(); System.out.println(str); } catch(IOException e){ System.out.println("Error Reading Streams"); } } } } |
Output:
Server Side:
Server Started
Received Password 123
Connected to client………..127.0.0.1
Client Side
Connected to server……………………………………..
Leave a Reply