Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple client/server socket program in Java. The client is a Java GUI t

ID: 3838116 • Letter: C

Question

Create a simple client/server socket program in Java. The client is a Java GUI that speaks to a server socket running on a desktop machine. The server side is a Java program with no GUI, just a console program with a main. The server is going to serve up prime numbers whose bit length is specified in the client and sent to the server.

Write a Java server socket that waits for a single connection from a client. When the server receives a connection it reads a bit-length n sent by the client and then generates an n-bit prime integer (that is probably prime) and sends it back to the client. After it sends the integer back to the client, the server socket should close the current connection and wait for another connection.

The client should present a Java GUI to the user that allows them to enter an integer into a text box and then click a button GenPrime that connects to the server, waits for a prime number from the server, and then outputs the prime number into a text area.

Explanation / Answer

server :

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server

{

    private static Socket socket;

    public static void main(String[] args)

    {

        try

        {

            int port = 25000;

            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop

            while(true)

            {

                //Reading the message from the client

                socket = serverSocket.accept();

                InputStream is = socket.getInputStream();

                InputStreamReader isr = new InputStreamReader(is);

                BufferedReader br = new BufferedReader(isr);

                String number = br.readLine();

                System.out.println("Message received from client is "+number);

                //Multiplying the number by 2 and forming the return message

                String returnMessage;

                try

                {

                    int numberInIntFormat = Integer.parseInt(number);

                    int returnValue = numberInIntFormat*2;

                    returnMessage = String.valueOf(returnValue) + " ";

                }

                catch(NumberFormatException e)

                {

                    //Input was not a number. Sending proper message back to client.

                    returnMessage = "Please send a proper number ";

                }

                //Sending the response back to the client.

                OutputStream os = socket.getOutputStream();

                OutputStreamWriter osw = new OutputStreamWriter(os);

                BufferedWriter bw = new BufferedWriter(osw);

                bw.write(returnMessage);

                System.out.println("Message sent to the client is "+returnMessage);

                bw.flush();

            }

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                socket.close();

            }

            catch(Exception e){}

        }

    }

}

client :

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.InetAddress;

import java.net.Socket;

public class Client

{

    private static Socket socket;

    public static void main(String args[])

    {

        try

        {

            String host = "localhost";

            int port = 25000;

            InetAddress address = InetAddress.getByName(host);

            socket = new Socket(address, port);

            //Send the message to the server

            OutputStream os = socket.getOutputStream();

            OutputStreamWriter osw = new OutputStreamWriter(os);

            BufferedWriter bw = new BufferedWriter(osw);

            String number = "2";

            String sendMessage = number + " ";

            bw.write(sendMessage);

            bw.flush();

            System.out.println("Message sent to the server : "+sendMessage);

            //Get the return message from the server

            InputStream is = socket.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

            String message = br.readLine();

            System.out.println("Message received from the server : " +message);

        }

        catch (Exception exception)

        {

            exception.printStackTrace();

        }

        finally

        {

            //Closing the socket

            try

            {

                socket.close();

            }

            catch(Exception e)

            {

                e.printStackTrace();

            }

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote