Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Pr16 (Implement Chat Server Using Serversocket)

The document contains code for a client-server program to check if a number is prime or not. The client sends a number to the server socket. The server receives the number, checks if it is prime using an isPrime method, and sends the result ("true" or "false") back to the client. The client then prints whether the number is prime or not based on the received response.

Uploaded by

Tanushri Raut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Pr16 (Implement Chat Server Using Serversocket)

The document contains code for a client-server program to check if a number is prime or not. The client sends a number to the server socket. The server receives the number, checks if it is prime using an isPrime method, and sends the result ("true" or "false") back to the client. The client then prints whether the number is prime or not based on the received response.

Uploaded by

Tanushri Raut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CLIENT:

import java.io.*;
import java.net.*;
class MyClient
{
private MyClient(){}
public static void main(String args[]) throws IOException
{
Socket s= new Socket("localhost",2019);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter username & 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);
}
}

SERVER:
import java.io.*;
import java.net.*;
class MyServer
{
private MyServer(){}
public static void main(String args[]) throws IOException
{
ServerSocket s= new ServerSocket(2019);
System.out.println("Server stated");
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 pw=new PrintStream(out);
if(user.equals("abc")&&pass.equals("1234"))
pw.println("Validate");
else
pw.println("Invalidate");
}
}
SERVER:
class MyServer2
{
private MyServer2(){}
public static boolean isPrime(int number)
{
boolean isPrimeNum=false;
int i=(int)Math.ceil(Math.sqrt(number));
while(i>1)
{
if((number!=1)&&(number%1==0))
{
isPrimeNum=false;
break;
}
else if(!isPrimeNum)
{
isPrimeNum=true;
}
--i;
}
return isPrimeNum;
}
public static void main(String args[]) throws Exception
{
ServerSocket ss= new ServerSocket(3000);
Socket s=ss.accept();
System.out.println("Waiting for client");
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
int num=Integer.parseInt(br.readLine());
System.out.println("Numberb sent by client:"+num);
pw.println(MyServer2.isPrime(num));
pw.flush();
}
}
CLIENT:
import java.io.*;
import java.net.*;
class MyClient2
{
private MyClient2(){}
public static void main(String args[]) throws IOException
{
Socket s= new Socket("localhost",3000);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("Enter any number");
String str=br.readLine();
pw.println(str);
pw.flush();
String msg=br1.readLine();
if(msg.equals("true"))
System.out.println("Prime");
else
System.out.println("NOT Prime");
}
}

You might also like