Skip to content

Commit 0e2c9be

Browse files
author
Sandy W
committed
Initial commit
0 parents  commit 0e2c9be

File tree

5 files changed

+315
-0
lines changed

5 files changed

+315
-0
lines changed

client/Client.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.*;
2+
import java.io.*;
3+
/**
4+
* @author Ikonija Bogojevic
5+
*
6+
*/
7+
public class Client
8+
{
9+
private static String hostName;
10+
private static Thread thrd = null;
11+
12+
// the list of threads is kept in a linked list
13+
private static LinkedList<Thread> list = new LinkedList<Thread>();
14+
private static ListIterator<Thread> li = null;
15+
16+
public static void main(String[] args) throws IOException
17+
{
18+
int menuSelection = 0;
19+
// load hostName from the command line arguments
20+
hostName = args[0];
21+
22+
// if no hostname is provided, quit
23+
if ((hostName.equals("")) || (hostName == null))
24+
{
25+
System.out.println("User did not enter a host name. Client program exiting.");
26+
System.exit(1);
27+
}
28+
29+
// until the user selects 7, the Exit option, keep looping and
30+
// offering the menu again after running the queries to the server
31+
else while (menuSelection != 7) {
32+
// display the menu and get the user's choice
33+
menuSelection = mainMenu();
34+
35+
// if 7, exit program
36+
if (menuSelection == 7) {
37+
System.out.println("Quitting.");
38+
System.exit(0);
39+
}
40+
41+
// otherwise, create threads in the linked list
42+
// TO-DO: add method to ask user how many threads to make
43+
// for now, just create 100
44+
for (int i = 0; i < 30; i++) {
45+
// make a new thread, tell it the hostname to connect to
46+
// and the command to run
47+
thrd = new Thread(new ClientThread(hostName, menuSelection));
48+
thrd.start(); // start the thread
49+
list.add(thrd); // add the thread to the end of the linked list
50+
51+
}
52+
53+
for (int i = 0; i < 30; i++) {
54+
try {
55+
list.get(i).join();
56+
} catch (InterruptedException e) {
57+
// TODO Auto-generated catch block
58+
e.printStackTrace();
59+
}
60+
}
61+
62+
63+
} // end while
64+
65+
}//end main
66+
//----------------------------------------------------------------------------
67+
public static int mainMenu()
68+
{
69+
int menuSelection = 0;
70+
System.out.println("The menu provides the following choices to the user: ");
71+
System.out.println("1. Host current Date and Time \n2. Host uptime\n"
72+
+ "3. Host memory use \n4. Host Netstat \n5. Host current users "
73+
+ "\n6. Host running processes \n7. Quit ");
74+
System.out.println("Please provide number corresponding to the action you want to be performed:");
75+
Scanner sc = new Scanner(System.in);
76+
menuSelection = sc.nextInt();
77+
System.out.println("Running command: " + menuSelection);
78+
return menuSelection;
79+
80+
}//end mainMenu
81+
82+
}

client/ClientThread.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.io.PrintWriter;
5+
import java.net.Socket;
6+
import java.net.UnknownHostException;
7+
8+
9+
public class ClientThread extends Thread {
10+
int menuSelection;
11+
String hostName;
12+
private static Socket socket = null;
13+
14+
ClientThread(String hostName, int menuSelection) {
15+
this.menuSelection = menuSelection;
16+
this.hostName = hostName;
17+
}
18+
19+
public void run() {
20+
PrintWriter out = null;
21+
BufferedReader in = null;
22+
try
23+
{
24+
//creates a new Socket object and names it socket.
25+
//Establishes the socket connection between the client & server
26+
//name of the machine & the port number to which we want to connect
27+
socket = new Socket(hostName, 4000);
28+
System.out.print("Establishing connection.");
29+
out = new PrintWriter(socket.getOutputStream(), true);//opens a PrintWriter
30+
//on the socket
31+
32+
//opens a BufferedReader on the socket
33+
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
34+
System.out.println("\nRequesting output for the '" + menuSelection + "' command from " + hostName);
35+
// send the command to the server
36+
out.println(Integer.toString(menuSelection));
37+
out.flush();
38+
System.out.println("Sent output");
39+
// read the command from the server
40+
String outputString = "hi";
41+
while (((outputString = in.readLine()) != null) && (!outputString.equals("END_MESSAGE"))) {
42+
System.out.println(outputString);
43+
System.out.println("Input read");
44+
}
45+
System.out.println("closing");
46+
try {
47+
sleep(500);
48+
} catch (InterruptedException e) {
49+
// TODO Auto-generated catch block
50+
e.printStackTrace();
51+
}
52+
out.close();
53+
in.close();
54+
socket.close();
55+
56+
}
57+
catch (UnknownHostException e)
58+
{
59+
System.err.println("Unknown host: " + e);
60+
System.exit(1);
61+
}
62+
catch (IOException e)
63+
{
64+
e.printStackTrace();
65+
}
66+
67+
}
68+
69+
}

server/CommandExecutor.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
/*
6+
* Class containing methods for taking the command string (a string with a digit 1-6)
7+
* received from the server and converting that into its respective Linux shell command,
8+
* then executing it and returning the results.
9+
*/
10+
public class CommandExecutor {
11+
12+
/**
13+
* Runs the shell command after first using parseCommand() to determine which
14+
* command to run.
15+
*
16+
* @param commandString A string containing a single digit, 1-6;
17+
* @return A string containing the results of the shell command.
18+
*/
19+
static String run(String commandString) {
20+
String result = "";
21+
String line;
22+
try {
23+
Process child = Runtime.getRuntime().exec(parseCommand(commandString));
24+
25+
BufferedReader output = new BufferedReader(new InputStreamReader(child.getInputStream()));
26+
while ((line = output.readLine()) != null) {
27+
result = result.concat(line);
28+
result = result.concat("\n");
29+
}
30+
31+
result = result.concat("\n");
32+
result = result.concat("END_MESSAGE");
33+
output.close();
34+
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
39+
return result;
40+
}
41+
42+
/**
43+
* Converts the digit string into its respective shell command.
44+
*
45+
* @param inputString A string containing a single digit, 1-6;
46+
* @return
47+
*/
48+
static String parseCommand(String inputString) {
49+
int inputInt = Integer.parseInt(inputString);
50+
String commandString = "";
51+
switch (inputInt) {
52+
// Date
53+
case 1:
54+
commandString = "date";
55+
break;
56+
57+
// Uptime
58+
case 2:
59+
commandString = "uptime";
60+
break;
61+
62+
// Memory use
63+
case 3:
64+
commandString = "free";
65+
break;
66+
67+
// netstat
68+
case 4:
69+
commandString = "netstat";
70+
break;
71+
72+
// current users
73+
case 5:
74+
commandString = "who";
75+
break;
76+
77+
// running processes
78+
case 6:
79+
commandString = "ps -e";
80+
break;
81+
}
82+
83+
return commandString;
84+
}
85+
}

server/Server.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.io.IOException;
2+
import java.net.ServerSocket;
3+
import java.net.Socket;
4+
import java.util.ArrayList;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
8+
public class Server {
9+
10+
/**
11+
* @param args
12+
*/
13+
public static void main(String[] args) {
14+
AtomicInteger numThreads = new AtomicInteger(0);
15+
// the list of threads is kept in a linked list
16+
ArrayList<Thread> list = new ArrayList<Thread>();
17+
18+
try {
19+
ServerSocket socket = new ServerSocket(4000);
20+
System.out.println("Server listening on port 4000");
21+
22+
while(true) {
23+
Socket client = socket.accept();
24+
Thread thrd = new Thread(new ServerThread(client));
25+
list.add(thrd);
26+
thrd.start();
27+
numThreads.incrementAndGet();
28+
System.out.println("Thread " + numThreads.get() + " started.");
29+
30+
}
31+
}
32+
catch (IOException ioe){
33+
ioe.printStackTrace();
34+
}
35+
}
36+
}

server/ServerThread.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.io.PrintWriter;
5+
import java.net.Socket;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
9+
public class ServerThread extends Thread {
10+
Socket client = null;
11+
12+
public ServerThread(Socket client) {
13+
this.client = client;
14+
}
15+
16+
public void run() {
17+
System.out.print("Accepted connection. ");
18+
19+
try {
20+
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
21+
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
22+
System.out.print("Reader and writer created. ");
23+
24+
String inString = in.readLine();
25+
System.out.println("Read command " + inString);
26+
27+
String outString = CommandExecutor.run(inString);
28+
System.out.println("Server sending: " + outString);
29+
out.print(outString);
30+
out.flush();
31+
32+
out.close();
33+
in.close();
34+
client.close();
35+
System.out.println("Output closed.");
36+
37+
}
38+
catch (IOException e) {
39+
e.printStackTrace();
40+
}
41+
42+
}
43+
}

0 commit comments

Comments
 (0)