Repository navigation
Expand file tree
/
Copy pathCSSv1.java
More file actions
executable file
·130 lines (105 loc) · 4.36 KB
/
Copy pathCSSv1.java
File metadata and controls
executable file
·130 lines (105 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* The CSSv1 class is a threaded socket server that accepts commands from a client,
* runs the appropriate process, then returns the result to the client.
* The processes are Linux commands that the server executes on its underlying OS.
*
*/
import java.io.*;
import java.net.*;
import java.util.Scanner;
class ServerThread extends Thread {
Socket socket;
InputStream input; // get the raw input stream
OutputStream output; // get the raw output stream
BufferedReader reader; // where to receive input
PrintWriter writer; // where to write output
public ServerThread( Socket s ) {
socket = s;
try {
input = socket.getInputStream(); // get the raw input stream
output = socket.getOutputStream(); // get the raw output stream
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
reader = new BufferedReader(new InputStreamReader(input)); // where to receive input
writer = new PrintWriter(output, true); // where to write output
}
@Override
public void run() {
String cmd = ""; // the command received from client
String result = ""; // the output of the server
System.out.println( "New client connected" ); // success message
try {
cmd = reader.readLine(); // get a line of input
/* The iterative (single-threaded) server must support the following client requests:
- Date and Time - the date and time on the server
- Uptime - how long the server has been running since last boot-up
- Memory Use - the current memory usage on the server
- Netstat - lists network connections on the server
- Current Users - list of users currently connected to the server
- Running Processes - list of programs currently running on the server
*/
Process p;
switch( cmd ) { // switch on client command
// cmd stays the same
case "date":
case "uptime":
case "netstat":
case "users":
break;
// cmd needs to change
case "memory":
cmd = "free";
break;
case "ps":
cmd = "ps -a";
break;
// invalid cmd
default:
writer.println( "Error: invalid command" );
}
p = Runtime.getRuntime().exec(cmd);
// reads the input stream from the external process
BufferedReader stdInput = new BufferedReader(
new InputStreamReader(
p.getInputStream()
)
);
// append
String s = stdInput.readLine();
while( s != null ) {
if( ! result.equals( "" ) ) {
result += '\n';
}
result += s;
s = stdInput.readLine();
}
System.out.println( cmd );
System.out.println( result );
writer.println( result ); // write line to output stream
socket.close();
} catch( IOException ex ) {
System.out.println( "Server exception: " + ex.getMessage() );
ex.printStackTrace();
}
}
}
public class CSSv1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print( "Port: " );
int port = Integer.parseInt( keyboard.nextLine() );
try( ServerSocket serverSocket = new ServerSocket( port ) ) {
System.out.println("Server is listening on port " + port);
// Repeat indefinitely...
while( true ) {
Socket socket = serverSocket.accept(); // accept the request from client
new ServerThread( socket ).start();
}
} catch( IOException ex ) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}