Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA SocketManager Ask all the following of the user: A port to run its server on (something like 8800 or 8888) An IP of another

  1. JAVA
    1. SocketManager
      1. Ask all the following of the user:
        1. A port to run its server on (something like 8800 or 8888)
        2. An IP of another server (local computer is 127.0.0.1)
          1. NOTE: If running on two computers, to get the IP address of a computer, in a command prompt or powershell, simply type ipconfig and look for the IPV4 address or IPV6 whichever comes first. It will probably look something like this: 192.168.0.3
        3. The port of that other server.
      2. After starting up the server, it should enter a never-ending loop that does the following:
        1. Asks the user for three numbers separated by commas. So the user would enter something like this:

4,7,22

  1. Sends that raw text to the server using the SocketClient class as we did in class.
    1. IMPORTANT: The items sent should be comma-delimited all concatenated as one string.

  1. SocketServer
    1. When the server receives a message from a client, it should try to parse the comma-delimited message into an array.
      1. HINT: Use the sReceivedMessage.split(",") to get a String array of the comma-delimited items.
    2. Then turn each array string into three int variables.
      1. Use Integer.parseInt() to do this on each item in the array.
      2. Then sum these numbers into a result.

  1. Reply with the sum of these numbers.

import java.util.Scanner; public class SocketManager { public static void main(String[] args){ //get port for this server to listen on. System.out.print("Enter port for this server to listen on: "); int iThisServerPort = new Scanner(System.in).nextInt(); SocketServer oServer = new SocketServer(iThisServerPort); System.out.println(""); Thread oServerThread = new Thread(oServer); //calling run method oServerThread.start(); //get details of server to connect to. System.out.print("Enter IP address of server to connect to: "); System.out.println(""); String sOtherServerIP = new Scanner(System.in).nextLine(); System.out.print("Enter port of server to connect to: "); int iOtherServerPort = new Scanner(System.in).nextInt(); while(true) { System.out.print("Enter message to sent to server: "); String sMessage = new Scanner(System.in).nextLine(); SocketClient oClient = new SocketClient(); String sReply = oClient.connectForOneMessage(sOtherServerIP, iOtherServerPort, sMessage); System.out.println("[client] Remote server reply: " +sReply); } } } 

import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class SocketServer implements Runnable { private int thisServerPort; public SocketServer(int iPort){ thisServerPort = iPort; } //constructor public void run(){ try(ServerSocket oServerSocket = new ServerSocket(thisServerPort)){ System.out.println("Server is listening on port " + thisServerPort); while(true){ Socket oSocket = oServerSocket.accept(); System.out.println("[server]: new client connected: " + oSocket.getRemoteSocketAddress()); oSocket.sReceivedMessage.split(","); InputStream input = oSocket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); OutputStream output = oSocket.getOutputStream(); PrintWriter writer = new PrintWriter(output, true); //Get one time message from client. String sReceivedMessage = reader.readLine(); System.out.println("[server]: Server received message: " + sReceivedMessage); //Send message back to calling client writer.println("Server received message: "+ sReceivedMessage); writer.flush(); } }catch (IOException ex){ System.out.println("[server]: Server exception: " + ex.getMessage()); //ex.printStackTrace(); } } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

Discuss the goals of financial management.

Answered: 1 week ago