Prime Number | TCP Protocol | Data Communication and Networking
Client
import java.io.*;
import java.net.*;
class Q3_Client
{
public static void main(String ar[]) throws Exception
{
Socket s=new Socket("localhost",12345);
PrintWriter p=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader ink=new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many numbers Enter ? ");
int num=Integer.parseInt(ink.readLine());
p.println(num);
p.flush();
System.out.println("Enter "+num+" numbers :");
String sarr[]=new String[num];
for(int i=0;i<num;i++)
{
System.out.print("no. "+i+"=");
sarr[i]=ink.readLine();
p.println(sarr[i]);
p.flush();
}
String res;
System.out.println("\nPrime Number::\n");
while((res=in.readLine())!=null)
{
System.out.print(res);
}
s.close();
}
}
Server
import java.io.*;
import java.net.*;
class Q3_Server
{
public static void main(String ar[]) throws Exception
{
ServerSocket s1=new ServerSocket(12345);
System.out.println("Server Started");
Socket s=s1.accept();
PrintWriter p=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String num=in.readLine();
int n=Integer.parseInt(num);
System.out.println("Client want to check "+n+" of Prime numbers");
String sarr[]=new String[n];
int arr[]=new int[n];
int c;
System.out.println("received numbers::\n");
for(int i=0;i<n;i++)
{
sarr[i]=in.readLine();
arr[i]=Integer.parseInt(sarr[i]);
System.out.println("no. "+i+"="+arr[i]);
}
int a[]=new int[n];
int cnt=0;
for (c = 0; c < n ; c++)
{
if((arr[c]%2)!=0 || arr[c]==2){
a[cnt]=arr[c];
cnt++;
}
}
System.out.println("\nlist of Prime numbers :");
String sendarr=new String();
if(cnt==0){
sendarr="Here not any prime number....";
}
else{
for (c = 0; c <cnt; c++)
sendarr+=" ,"+a[c];
}
System.out.println(sendarr);
p.println(sendarr);
p.flush();
s.close();
s1.close();
}
}
No comments
Comment your views on this article