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: 3687728 • 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.

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.*;

import java.net.*;

import java.util.*;


public class NewClient

{

    Socket s;

    DataInputStream reader;

    DataOutputStream writer;

    String userN;

    String userP;

    double balance;

public static void main(String[] args)
{

        try{

           sendData();

        }

        catch(Exception ex){}

}

sendData()

{
     //Send username

        writer.writeUTF(userN);   

        //Send password

         writer.writeUTF(userP);

      //Read balance snet

        balance =reader.getBalance();

}

}