You need to write 2 separate programs, a client & server. The client makes a con
ID: 3839609 • Letter: Y
Question
You need to write 2 separate programs, a client & server. The client makes a connection to the server and transmits a series of numbers. The server listens for client communication and when it has received the series of numbers, it adds them together and closes.
Program requirements:
1. The client needs to know the server IP address and port number. Determine these from the server and make sure if you run the server, followed by the client that they can communicate. If you run both on the same server, any port above 1024 is valid.
2. Client code: prompt the user for how many numbers to add together, then prompt them for each number. Conversion from string to integer must be done on the client.
3. Server code: Display each integer immediately after it is entered on the client. When the client is nished sending numbers, the server code should add the numbers together, display the total, and then shutdown. (Hint: If you establish a new connection on the server for every number transmitted, the server may need a pipe to communicate numbers from the child processes to the parent process. If you transmit all of the numbers in a single connection, you probably will not need to do this.)
4. Close all sockets on the server and client when nished. Make sure all child processes are closed.
$ ./client localhost 7500
How many numbers should I add together? 2
Enter a number: 99
Enter a number: 45
$ ./server 7500
Received number 1: 99
Received number 2: 45
Here are the numbers added together: 144
Explanation / Answer
Analyzing your problem statement I will code in java to enable the functionality
Please find the below working code
**************
Client.java
package com.sagar.oracle;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String arg[]) throws IOException, ClassNotFoundException {
int portNum = 11113;
int x;int y;
Socket socket = new Socket("localhost", portNum);
// Integer Object to send to Server.
System.out.println("How many numbers Should I add together");
Scanner s1= new Scanner(System.in);
System.out.println("Enter a number");
x= s1.nextInt();
System.out.println("Enter a number");
y = s1.nextInt();
Integer num = new Integer(x);
Integer num1=new Integer(y);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
out.writeObject(num);
out.writeObject(num1);
String response = (String) in.readObject();
System.out.println("Server message: " + response);
//socket.close();
}
}
************
Server1.java
package com.sagar.oracle;
//Keeping imports easy to remember.
import java.io.*;
import java.net.*;
public class Server1 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Port number to bind server to.
int portNum = 11113;
int z;
int l,result;
// Socket for server to listen at.
ServerSocket listener = new ServerSocket(portNum);
System.out.println("Server is now running at port: " + portNum);
// Simply making Server run continously.
while (true) {
try {
// Accept a client connection once Server recieves one.
Socket clientSocket = listener.accept();
// Creating inout and output streams. Must creat out put stream first.
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
// Reading in Integer Object from input stream.
int[] i = (int[]) in.readObject();
z = i[0]; l =i[1];
result = z + l;
//Sending response back to client
String response = "Integer Object Received.";
out.writeObject(response);
// Outputting recieved Integer Object.
System.out.println("Numbers added together: " + i);
out.close();
in.close();
clientSocket.close();
break;
} finally {
// Closing Server Socket now.
listener.close();
}
}
}
}
*******************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.