Saturday, 20 October 2012

Accept n numbers through one program send it to the server program check their for prime numbers send only the prime numbers to client-Java Socket program

tcpserver1.java

import java.io.*;
import java.net.*;
class tcpserver1
{
public static void main(String args[])throws Exception
{
ServerSocket ss = new ServerSocket(6856);
Socket cs=ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintStream ps = new PrintStream(cs.getOutputStream());
int[] a=new int[15];
int[] b= new int [15];
int k=0,n,i,j,f=0;
n=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
{
for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
f=1;
break;
}
}
if(f==0)
{
if(a[i]!=1)
{
b[k]=a[i];
k++;
}
}
f=0;
}
ps.println(k);
for(i=0;i<k;i++)
ps.println(b[i]);

}
}

tcpclient1.java

import java.io.*;
import java.net.*;
class tcpclient1
{
public static void main(String args[])throws Exception
{
Socket cs = new Socket("localhost",6856);

BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintStream ps = new PrintStream(cs.getOutputStream());
int[] a=new int[15];
int[] b= new int [15];
int k,n,i;
System.out.println("Enter the limit");
n=Integer.parseInt(kb.readLine());
ps.println(n);
System.out.println("Enter the Numbers");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(kb.readLine());
for(i=0;i<n;i++)
ps.println(a[i]);
k=Integer.parseInt(br.readLine());
for(i=0;i<k;i++)
b[i]=Integer.parseInt(br.readLine());
System.out.println("Prime nos\n");
for(i=0;i<k;i++)
System.out.println(b[i]);

}
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More