Java Programming Practical 25
Question 1
Write a program to check credentials of users (Client will send user id and password to server and server will authenticate the client using equals())
CheckServer.java
Java
import java.net.*;
import java.io.*;
class CheckServer
{
public static void main(String[ ] args)
{
try
{
String message;
ServerSocket connectionSocket = new ServerSocket(1234);
Socket dataSocket = connectionSocket.accept();
//Accepting data from client
InputStream in=dataSocket.getInputStream();
DataInputStream din=new DataInputStream(in);
String uname=din.readLine();
String pass=din.readLine();
if(uname.equals("admin")&&pass.equals("Admin"))
message="Authentication Successful";
else
message="Authentication Failed";
//Sending response to client
OutputStream os=dataSocket.getOutputStream();
PrintStream socketOutput = new PrintStream(os);
socketOutput.println("Response from server: "+message);
dataSocket.close( );
connectionSocket.close( );
}
catch(Exception e)
{
e.printStackTrace( );
}
}
}
CheckClient.java
Java
import java.net.*;
import java.io.*;
import java.util.*;
class CheckClient
{
public static void main(String[ ] args)
{
try
{
Socket clientSocket = new Socket("localhost", 1234);
Scanner sc=new Scanner(System.in);
System.out.println("Enter username: ");
String uname=sc.nextLine();
System.out.println("Enter password: ");
String pass=sc.nextLine();
//Sending name to server
OutputStream os=clientSocket.getOutputStream();
PrintStream socketOutput = new PrintStream(os);
socketOutput.println(uname);
socketOutput.println(pass);
//Acception response from server
InputStream in=clientSocket.getInputStream();
DataInputStream din=new DataInputStream(in);
System.out.println(din.readLine( ));
clientSocket.close();
}
catch(Exception e)
{
e.printStackTrace( );
}
}
}
Output

Question 2
Write a program using Socket and ServerSocket to create Chat Application
ChatClient.java
Java
import java.net.*;
import java.io.*;
import java.util.*;
class CReceiver extends Thread
{
Socket cs;
CReceiver(Socket s)
{
cs=s;
}
public void run()
{
InputStream in;
DataInputStream din;
String message;
while(true)
{
try
{
in=cs.getInputStream();
din=new DataInputStream(in);
message=din.readLine();
if(message==null)
System.exit(0);
System.out.println("From server: "+message);
}
catch(Exception ie)
{
System.out.println("Server connection ended");
System.exit(0);
}
}
}
}
public class ChatClient
{
public static void main(String[ ] args)
{
try
{
Socket dataSocket = new Socket("localhost",1234);
OutputStream os=dataSocket.getOutputStream();
Scanner sc=new Scanner(System.in);
PrintStream pr=new PrintStream(os);
String reply;
CReceiver rr=new CReceiver(dataSocket);
rr.start();
while(true)
{
reply=sc.nextLine();
pr.println(reply);
if(reply.equalsIgnoreCase("exit"))
System.exit(0);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
ChatServer.java
Java
import java.net.*;
import java.io.*;
import java.util.*;
class SReceiver extends Thread
{
Socket cs;
SReceiver(Socket s)
{
cs=s;
}
public void run()
{
InputStream in;
DataInputStream din;
String message;
while(true)
{
try
{
in=cs.getInputStream();
din=new DataInputStream(in);
message=din.readLine();
if(message==null)
System.exit(0);
System.out.println("From client: "+message);
}
catch(Exception ie)
{
System.out.println("Client connection ended");
System.exit(0);
}
}
}
}
public class ChatServer
{
public static void main(String[ ] args)
{
try
{
ServerSocket connectionSocket = new ServerSocket(1234);
System.out.println("Server started");
Socket dataSocket = connectionSocket.accept();
System.out.println("Client connected");
Scanner sc=new Scanner(System.in);
OutputStream os=dataSocket.getOutputStream();
PrintStream pr=new PrintStream(os);
SReceiver rr=new SReceiver(dataSocket);
rr.start();
String reply;
while(true)
{
reply=sc.nextLine();
pr.println(reply);
if(reply.equalsIgnoreCase("exit"))
System.exit(0);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output
