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

Now that we’ve got a client set up and a server for it to connect to, we need to

ID: 3672686 • Letter: N

Question

Now that we’ve got a client set up and a server for it to connect to, we need to send data back and forth. Finish the code for both classes to send numbers between the server and the client. Assume all variables and imports are properly set up. Be sure to finish the code for BOTH classes. If you have trouble, check the Oracle API. (30 points)

public class NewClient

{

    //Variables Created

   

    //Main Execution

    public static void main(String[] args)

    {

        //Catch a possible connectivity error

        try

        {

            //Connection set up – establish Input and Output Streams.

            //Note: This is only one possible way to send data.

            reader = new DataInputStream(socket.getInputStream());

            writer = new DataOutputStream(socket.getOutputStream());

           

            //Pseudocode: random number generated.

            int clientNumber = x;

            int serverNumber;

           

            //Send the random number to the server.

            //**WRITE YOUR CODE BELOW THIS LINE**

           

           

            //Receive the random number from the server.

            //**WRITE YOUR CODE BELOW THIS LINE**

           

            

            System.out.println(“Server’s number: “ + serverNumber);

        }

        catch (IOException ex)

        {

            //Inform the user of the error

            System.out.println(“Error connecting to server.”);

        }

    }

}

public class NewServer

{

    //Variables Created

   

    //Main Execution

    public static void main(String[] args)

    {

        //Catch a possible connectivity error

        try

        {

            //Connection set up – establish Input and Output Streams.

            //Note: This is only one possible way to send data.

            reader = new DataInputStream(clientSocket.getInputStream());

           writer = new DataOutputStream(clientSocket.getOutputStream());

           

            //Pseudocode: random number generated.

            int clientNumber;

            int serverNumber = x;

           

            //Receive the random number from the client.

            //**WRITE YOUR CODE BELOW THIS LINE**

           

           

            //Send the random number to the client.

            //**WRITE YOUR CODE BELOW THIS LINE**

           

           

            System.out.println(“Client’s number: “ + clientNumber);

        }

        catch (IOException ex)

        {

            //Inform the user of the error

            System.out.println(“Error connecting to server.”);

        }

    }

}

public class NewClient

{

    //Variables Created

   

    //Main Execution

    public static void main(String[] args)

    {

        //Catch a possible connectivity error

        try

        {

            //Connection set up – establish Input and Output Streams.

            //Note: This is only one possible way to send data.

            reader = new DataInputStream(socket.getInputStream());

            writer = new DataOutputStream(socket.getOutputStream());

           

            //Pseudocode: random number generated.

            int clientNumber = x;

            int serverNumber;

           

            //Send the random number to the server.

            //**WRITE YOUR CODE BELOW THIS LINE**

           

           

            //Receive the random number from the server.

            //**WRITE YOUR CODE BELOW THIS LINE**

           

            

            System.out.println(“Server’s number: “ + serverNumber);

        }

        catch (IOException ex)

        {

            //Inform the user of the error

            System.out.println(“Error connecting to server.”);

        }

    }

}

Explanation / Answer

NewClient.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class NewClient
{
//Variables Created
static DataInputStream reader;
static DataOutputStream writer;
static Socket socket;

//Main Execution
public static void main(String[] args)
{
//Catch a possible connectivity error
try
{
//Connection set up – establish Input and Output Streams.
//Note: This is only one possible way to send data.
socket = new Socket("localhost", 3000);
reader = new DataInputStream(socket.getInputStream());
writer = new DataOutputStream(socket.getOutputStream());

//Pseudocode: random number generated.
int clientNumber = (int) (Math.random() * 100);
int serverNumber;

//Send the random number to the server.
//**WRITE YOUR CODE BELOW THIS LINE**
writer.write(clientNumber);

//Receive the random number from the server.
//**WRITE YOUR CODE BELOW THIS LINE**
serverNumber = reader.read();

System.out.println("Server's number: " + serverNumber);
}
catch (IOException ex) {
//Inform the user of the error
System.out.println("Error connecting to server.");
}
}
}

NewServer.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class NewServer
{
//Variables Created
static DataInputStream reader;
static DataOutputStream writer;
static ServerSocket serverSocket;
static Socket clientSocket;

//Main Execution
public static void main(String[] args)
{
//Catch a possible connectivity error
try
{
//Connection set up – establish Input and Output Streams.
//Note: This is only one possible way to send data.
serverSocket = new ServerSocket(3000);
clientSocket = serverSocket.accept();

reader = new DataInputStream(clientSocket.getInputStream());
writer = new DataOutputStream(clientSocket.getOutputStream());

//Pseudocode: random number generated.
int serverNumber = (int) (Math.random() * 100);

//Receive the random number from the client.
//**WRITE YOUR CODE BELOW THIS LINE**
int clientNumber = reader.read();

//Send the random number to the client.
//**WRITE YOUR CODE BELOW THIS LINE**
writer.write(serverNumber);

System.out.println("Client's number: " + clientNumber);
}
catch (IOException ex)
{
//Inform the user of the error
System.out.println("Error connecting to server.");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote