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

The following server-client program takes a username and a password and returns

ID: 3690317 • Letter: T

Question

The following server-client program takes a username and a password and returns the user’s current balance. Write the client-side of this program. Allow the user to enter their own Username and Password (assume all possible input is valid / no data validation) to send to the client and print the balance received from the server to the client’s output. Remember to catch necessary exceptions. DO NOT modify the Server class. (40 points)

import java.io.*;

import java.net.*;

import java.util.*;

public class NewClient

{

    //Write your code here!

   

}

import java.io.*;

import java.net.*;

import java.util.*;

public class NewServer

{

    static ServerSocket server;

    static Socket client;

    static PrintStream writer;

    static Scanner reader;

   

    static String username;

    static String password;

    static double balance;

   

    public static void main

                (String[] args)

    {

        try

        {

            server = new

                ServerSocket(9999);

            client = server.accept();

            writer = new PrintStream

          (client.getOutputStream());

            reader = new Scanner

           (client.getInputStream());

           

            getBalance();

    }

    catch (IOException IOex)

    {

            IOex.printStackTrace();

    }

    }

   

    public void getBalance()

    {

        username = reader.next();

        password = reader.next();

        if (password.equals(“guest”))

            balance = 100.00;

        else

            balance = 10.00;

        writer.println(balance);

    }

}

import java.io.*;

import java.net.*;

import java.util.*;

public class NewClient

{

    //Write your code here!

   

}

import java.io.*;

import java.net.*;

import java.util.*;

public class NewServer

{

    static ServerSocket server;

    static Socket client;

    static PrintStream writer;

    static Scanner reader;

   

    static String username;

    static String password;

    static double balance;

   

    public static void main

                (String[] args)

    {

        try

        {

            server = new

                ServerSocket(9999);

            client = server.accept();

            writer = new PrintStream

          (client.getOutputStream());

            reader = new Scanner

           (client.getInputStream());

           

            getBalance();

    }

    catch (IOException IOex)

    {

            IOex.printStackTrace();

    }

    }

   

    public void getBalance()

    {

        username = reader.next();

        password = reader.next();

        if (password.equals(“guest”))

            balance = 100.00;

        else

            balance = 10.00;

        writer.println(balance);

    }

}

Explanation / Answer

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;
import java.util.*;
public class Client
{

private static Socket socket;

public static void main(String args[])
{
try
{
       Scanner s=new Scanner(System.in);
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 uname=s.next();
           String pwd=s.next();
String sendMessage = uname + pwd+" ";
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("total balance is : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}