-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
90 lines (68 loc) · 2.25 KB
/
Client.java
File metadata and controls
90 lines (68 loc) · 2.25 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
import java.io.*;
import java.net.*;
public class Client{
static long factorial(int l, int r)
{
int mod = 1000000007;
long fact = 1;
for(long i=l;i<=r;i++)
fact = (fact*i)%mod;
return fact;
}
public static void makeClient(String ip,int port){
try{
Socket socket = new Socket(ip,port);
DataInputStream input = new DataInputStream(System.in);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
Process p;
String bashOut="";
try{
String[] cmd = { "sh", "./script.sh"};
p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String temp = "";
while((bashOut = reader.readLine()) != null) {
System.out.println("TOP parameters : " + bashOut);
temp = temp + bashOut;
}
out.writeUTF(temp);
}catch(Exception e){
System.out.println("Error in bash");
System.out.println(e);
}
String line = " ";
long startTime0 = System.nanoTime();
factorial(0,50);
long endTime0 = System.nanoTime();
long duration0 = (endTime0 - startTime0);
line = Long.toString(duration0);
out.writeUTF(line);
line = " ";
while(!line.equals("Over")){
String resp = inputFromServer.readUTF();
String[] arrOfStr = resp.split(" ");
//System.out.println("Server : " + resp);
//System.out.println(arrOfStr[0] + " " + arrOfStr[1]);
int l = Integer.parseInt(arrOfStr[0]);
int r = Integer.parseInt(arrOfStr[1]);
//System.out.println(l + " " + r);
line = "result from client : " + socket;
long startTime = System.nanoTime();
long result = factorial(l, r);
long endTime = System.nanoTime();
long duration = (endTime - startTime);
line = Long.toString(duration);
line = line + " " + Long.toString(result);
//System.out.println(line);
out.writeUTF(line);
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String args[]){
makeClient("127.0.0.1",5005);
}
}