In: Computer Science
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING
::::::::::::::::::::::::::::::::::::::::::::::::::: CODE :::::::::::::::::::::::::::::::::::::::::::::::::::
_______________________________________
Server.java
_______________________________________
import java.io.*;
import java.net.*;
public class Server {
public static String isPrime(int num) {
if(num<2) {
return
"No";
}
int i=2;
while(i<num) {
if(num%i==0)
{
return "No";
}
i++;
}
return "Yes";
}
public static void main(String[] args){
try{
ServerSocket
ss=new ServerSocket(8080);
//establishes
connection
Socket
s=ss.accept();
DataInputStream
dis=new DataInputStream(s.getInputStream());
DataOutputStream
dout=new DataOutputStream(s.getOutputStream());
int num =
(int)dis.readInt();
// CALLING
FUNCTION AND
// WRITING
RETURN VALUE TO CLIENT
dout.writeUTF(isPrime(num));
dout.flush();
dout.close();
ss.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
_______________________________________
Client.java
_______________________________________
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
try{
Socket s=new
Socket("localhost",8080);
DataInputStream
dis=new DataInputStream(s.getInputStream());
DataOutputStream
dout=new DataOutputStream(s.getOutputStream());
// TAKING
USER INPUT
System.out.print("\nEnter a Number : ");
int num =
scan.nextInt();
// WRITING
INTEGER TO SERVER
dout.writeInt(num);
String ans =
(String)dis.readUTF();
System.out.println("\nNumber "+num+
" Is Prime Number:
"+ans);
dout.flush();
dout.close();
s.close();
}
catch(Exception e){
System.out.println(e);
}
scan.close();
}
}
::::::::::::::::::::::::::::::::::::::::::::::::::: OUTPUT :::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::: CODE in EDITOR :::::::::::::::::::::::::::::::::::::::::::::::::::
_______________________________________
Server.java
_______________________________________
_______________________________________
Client.java
_______________________________________
Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.
Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.
Thank YOU :-)