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() ).
Ans:
Server:
Client:
Output:
Server:
Client:
2) Write a program using Socket and ServerSocket to create Chat Application.
Ans:
ServerSide:
ClientSide:
Output:
Ans:
Server:
/* Client Will Send UserID and Password to 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 ValidateServer { 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("abc") && pass.equals("1234")) { ps.println("Validate Successfully"); } else { ps.println("Validate Un-Successfull"); } } }
Client:
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 ValidateClient { 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); } }
Output:
Server:
Client:
2) Write a program using Socket and ServerSocket to create Chat Application.
Ans:
ServerSide:
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 ServerSide { 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(); // Client Send BufferedReader br = new BufferedReader( new InputStreamReader(s1.getInputStream()) ); OutputStream out = s1.getOutputStream(); PrintStream ps = new PrintStream(out); BufferedReader br1 = new BufferedReader( new InputStreamReader(System.in) ); do{ String res = br.readLine(); System.out.println("Client Send: "+res); System.out.print("Server: "); String msg = br1.readLine(); System.out.print("\n\n"); ps.println(msg); } while(true); } }
ClientSide:
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 ClientSice { public static void main(String[] args) throws IOException { Socket s = new Socket("localhost",2019); System.out.println("Client Started, waiting for server response.."); BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); OutputStream os = s.getOutputStream(); BufferedReader br1 = new BufferedReader( new InputStreamReader(s.getInputStream()) ); PrintStream ps = new PrintStream(os); do{ System.out.print("Client: "); String msg = br.readLine(); ps.println(msg); String res = br1.readLine(); System.out.println("Server Send:"+res+"\n\n"); } while(true); } }
Output: