Ajp Exp 16 Op
Ajp Exp 16 Op
Ajp Exp 16 Op
Program Code:
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()).
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.InputStreamReader;
public class Server
{
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(2019);
System.out.println("Server Started, waiting for client");
Socket s1 = s.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s1.getInputStream()) );
String user = br.readLine();
String pass = br.readLine();
OutputStream out = s1.getOutputStream();
PrintStream ps = new PrintStream(out);
if(user.equals("Smith") && pass.equals("Shresth")) {
ps.println("Validate Successfully");
}else {
ps.println("Validate Un-Successfull");
}
} }
(ii) Client Code:
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
public class Client
{
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost" , 2019);
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in) );
System.out.print("Enter Username and Password: ");
String user = br.readLine();
String pass = br.readLine();
OutputStream os = s.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println(user);
ps.println(pass);
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()) );
String res = br1.readLine();
System.out.println(res);
}
}
XIII. Exercise:
1. Write a program using Socket and ServerSocket to create Chat Application.
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.*;
import java.net.*;
import java.io.*;
import java.net.*;