forked from tishrof/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer1.java
More file actions
48 lines (36 loc) · 1.63 KB
/
Server1.java
File metadata and controls
48 lines (36 loc) · 1.63 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
import java.io.*;
import java.net.*;
class Server {
private static final String NEWLINE = System.getProperty("line.separator");
public static void main(String args[]) throws IOException {
try {
String str;
ServerSocket server = new ServerSocket(6555);
Socket serverSocket = server.accept();
BufferedReader serverInputStream = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
PrintWriter out = new PrintWriter(serverSocket.getOutputStream(), true);
str = serverInputStream.readLine();
System.out.println("Command recvied = " + str);
if (str.equals("0")) {
out.println(execCmd("dir"));
} else {
out.println(execCmd("date /t"));
}
} catch (IOException e) {
System.out.println("Error" + e);
}
}
public static String execCmd(String cmd) throws java.io.IOException {
StringBuilder result = new StringBuilder(80);
Runtime rt = Runtime.getRuntime();
String[] commands = { "cmd.exe", "/c", "cd \"F:\\IIIT V BOOKS\\Distributed\\Lab3\" && " + cmd };
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
result.append(s).append(NEWLINE);
}
System.out.println(result.toString());
return result.toString();
}
}