Java Socket Program to check Prime Number

2/13/2012 code-examplejavasocket

# Summary

This is a socket program (opens new window), it consists of Server side program & client side program. Server will provide web service for validating the number is prime or not and returns true or false.

Client sends the integer number to the server & gets the respond accordingly.

# Steps

  1. Compile both classes
  2. Run server class first
  3. Run client class that communicates with Server

This will work only for single input, inorder to make it work for infinite input as per user, simple changes have to be made in both the codes.

# Code

# Server Code: ServerPrime.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rohansakhale;


/**
 *
 * @author Rohan Sakhale
 */

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

public class ServerPrime {

    public static boolean isPrime(int number){
        boolean isPrimeNum = false;
        int i = (int) Math.ceil(Math.sqrt(number));
        while(i>1)
        {
            if((number != i) && (number % i ==0))
            {
                isPrimeNum = false;
                break;
            }
            else if(!isPrimeNum)
            {
                isPrimeNum = true;
            }
            --i;
        }
        return isPrimeNum;
    }
    public static void main(String [] args) throws Exception
    {
        Socket s;
        int port = 9000;
        ServerSocket ss = new ServerSocket(port);
        System.out.println("Waiting for client");
        s = ss.accept();
        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("Number sent by client: " + num);
        pw.println(ServerPrime.isPrime(num));
        pw.flush();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# Client Code: "ClientPrime.java"

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rohansakhale;

/**
 *
 * @author Rohan Sakhale
 */

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

public class ClientPrime {
    public static void main(String [] args) throws Exception
    {
        int port = 9000;
        Socket s;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        s = new Socket(InetAddress.getLocalHost(),port);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
        BufferedReader brl = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.print("Enter any number: ");
        String str = br.readLine();
        pw.println(str);
        pw.flush();
        String msg = brl.readLine();
        if(msg.equals("true"))
        {
            System.out.println("It is a prime number");
        }
        else
        {
            System.out.println("It is not a prime number");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39