Ajp Exp 16 Op

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

X.

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()).

(i) Server Code:

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.

(i) Server Code:

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 serverSocket = new ServerSocket(2019);
System.out.println("Server Started, waiting for client");
Socket clientSocket = serverSocket.accept();
BufferedReader clientInput = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
OutputStream outputStream = clientSocket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
BufferedReader serverInput = new BufferedReader(new InputStreamReader(System.in));
do {
String clientMessage = clientInput.readLine();
System.out.println("Client Send: " + clientMessage);
System.out.print("Server: ");
String serverMessage = serverInput.readLine();
System.out.print("\n\n");
printStream.println(serverMessage);
} while (true);
}
}
(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 ClientSide {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 2019);
System.out.println("Client Started, waiting for server response..");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
OutputStream outputStream = socket.getOutputStream();
BufferedReader serverInput = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintStream printStream = new PrintStream(outputStream);
do {
System.out.print("Client: ");
String message = userInput.readLine();
printStream.println(message);
String response = serverInput.readLine();
System.out.println("Server Send: " + response + "\n\n");
} while (true);
}
}
2. Write a program to develop prime number Server.

(i) Server Code:

import java.io.*;
import java.net.*;

public class PrimeNumberServer {


public static void main(String[] args) {
int port = 12345;
try (ServerSocket serverSocket = new ServerSocket(port)) {
while (true) {
try (Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String inputLine = in.readLine();
int number = Integer.parseInt(inputLine);
boolean isPrime = isPrime(number);
if (isPrime) {
out.println(number + " is a prime number.");
} else {
out.println(number + " is not a prime number.");
}
} catch (IOException e) {
System.out.println("Error handling client: " + e.getMessage());
}
}
} catch (IOException e) {
System.out.println("Server error: " + e.getMessage());
}
}

private static boolean isPrime(int number) {


if (number <= 1) return false;
if (number <= 3) return true;
if (number % 2 == 0 || number % 3 == 0) return false;
for (int i = 5; i * i <= number; i += 6) {
if (number % i == 0 || number % (i + 2) == 0) return false;
}
return true;
}
}
(ii) Client Code:

import java.io.*;
import java.net.*;

public class PrimeNumberClient {


public static void main(String[] args) {
String serverAddress = "localhost";
int port = 12345;
try (Socket socket = new Socket(serverAddress, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in))) {
System.out.print("Enter a number: ");
String number = userInput.readLine();
out.println(number);
String response = in.readLine();
System.out.println("Server response: " + response);
} catch (IOException e) {
System.out.println("Client error: " + e.getMessage());
}
}
}

You might also like