@TCPClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPClient {
private ArrayList<Integer> list;
private int choice;
Socket clientSocket;
//show the options to user and receive the operand of user.
public void option() throws Exception {
list = new ArrayList<Integer>();
while(choice != 5){
System.out.println("(1) Enter the list of number\n"+"(2) Summation\n"+"(3) Maximun\n"+"(4) Minimun\n"+"(5) Exit");
Scanner input = new Scanner(System.in);
choice = input.nextInt();
switch(choice){
case 1:
Client("inputList");
break;
case 2:
Client("sum"); //calculate sum
break;
case 3:
Client("max"); //find max
break;
case 4:
Client("min"); //find min
break;
default :
break;
}
}
System.out.println("Bye Bye");
}
//send command to the server and receive the result from server
private void Client(String command) throws Exception{
String result;
Socket clientSocket = null;
ObjectOutputStream outToServer_list = null;
BufferedReader inFromServer = null;
//if user selects (1), user inputs some numbers to the list
if(command.equalsIgnoreCase("inputList")){
addToList();
System.out.println(list);
return;
}
//build socket
clientSocket = new Socket("localhost",6789);
//create object output stream
outToServer_list = new ObjectOutputStream(clientSocket.getOutputStream());
//create input stream
inFromServer = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
//if user selects (2)-(4), this code will add the command number to last position of list and sent this list to server.
if(command.equalsIgnoreCase("sum")){
//check the list whether is empty.
if(list.isEmpty()){
System.out.println("Please input some number!!");
return;
}
//command number 2----summation
list.add(2);
//send to server
outToServer_list.writeObject(list);
System.out.println("List with command number: "+list);
//remove the command number
list.remove(list.size()-1);
System.out.println("List: "+list);
//receive the result from server
result = inFromServer.readLine();
System.out.println("Summation: "+ result+'\n');
}
else if(command.equalsIgnoreCase("max")){
if(list.isEmpty()){
System.out.println("Please input some number!!");
return;
}
//command number 3----max
list.add(3);
//send to server
outToServer_list.writeObject(list);
System.out.println("List with command number: "+list);
//remove the command number
list.remove(list.size()-1);
System.out.println("List: "+list);
//receive the result from server
result = inFromServer.readLine();
System.out.println("Maximum: "+ result+'\n');
}
else if(command.equalsIgnoreCase("min")){
if(list.isEmpty()){
System.out.println("Please input some number!!");
return;
}
//command number 4----min
list.add(4);
//send to server
outToServer_list.writeObject(list);
System.out.println("List with command number: "+list);
//remove the command number
list.remove(list.size()-1);
System.out.println("List: "+list);
//receive the result from server
result = inFromServer.readLine();
System.out.println("Minimum: "+ result+'\n');
}
clientSocket.close();
}
//add numbers to list
private void addToList() throws Exception{
System.out.println("please input integer: ");
Scanner input = new Scanner(System.in);
Integer num = 0;
boolean check = false;
try{
num = input.nextInt();
}catch(Exception e){
System.out.println("please input integer!!");
check = true;
}
while(num != 0 ||check){
if(num!=0||!(check)){
list.add(num);
}
System.out.println("please input integer: ");
input = new Scanner(System.in);
try{
num = input.nextInt();
}catch(Exception e){
System.out.println("please input integer!!");
check = false;
}
}
}
//main
public static void main(String[] args) throws Exception {
TCPClient c = new TCPClient();
c.option();
}
}
@end TCPClient
@CalServer.java
import java.io.*;
import java.net.*;
public final class CalServer {
private ServerSocket welcomeSocket;
private Socket connectionSocket;
// Constructor
public CalServer() throws IOException {
welcomeSocket = new ServerSocket(6789);
}
//server
void server(){
System.out.println("Socket built success");
try{
while (true){
System.out.println("waiting for client");
connectionSocket = welcomeSocket.accept();
//create thread.
Thread thread1 = new Thread(new CalRequest(connectionSocket));
thread1.start();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
//close socket
welcomeSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//main
public static void main(String[] args) throws IOException {
new CalServer().server();
}
}
@end CalServer
@CalRequest.java
import java.io.*;
import java.net.*;
import java.util.*;
final class CalRequest implements Runnable{
Socket socket;
private static ArrayList<Integer> list;
// Constructor
public CalRequest(Socket socket) throws Exception
{
this.socket = socket;
list = new ArrayList<Integer> ();
}
// Implement the run() method of the Runnable interface.
public void run()
{
System.out.println(Thread.currentThread().getName());
try {
//run process method
processRequest();
} catch (Exception e1) {
e1.printStackTrace();
}
}
//processing the request from client
@SuppressWarnings("unchecked")
public void processRequest() throws Exception
{
ObjectInputStream inFromClient = new ObjectInputStream(socket.getInputStream());
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
try{
//convert the received object to List.
list = (ArrayList<Integer>) inFromClient.readObject();
}catch(Exception e){
return;
}
//get the command number
int command = list.get(list.size()-1);
//remove command number from list
list.remove(list.size()-1);
//process the summation of list
if (command == 2){
Iterator<Integer> sumInter = list.iterator();
int sum = 0;
while(sumInter.hasNext()){
sum = sum+sumInter.next();
}
String sumStr = Integer.toString(sum);
//send result to client
outToClient.writeBytes(sumStr+'\n');
}
//process the maximum of list
else if(command == 3){
int max = Collections.max(list);
String maxStr = Integer.toString(max);
//send result to client
outToClient.writeBytes(maxStr+'\n');
}
//process the minimum of list
else if(command == 4){
int min = Collections.min(list);
String minStr = Integer.toString(min);
//send result to client
outToClient.writeBytes(minStr+'\n');
}
}
}
@end CalRequest