-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
65 lines (54 loc) · 1.98 KB
/
Copy pathClient.java
File metadata and controls
65 lines (54 loc) · 1.98 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
package Client;
import Backend.SensorPackage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
//Used instead of the web service to find a parking spot in a given area
public class Client {
public static void main(String[] args) {
int i=0;
int spot=-1;
SensorPackage sp = null;
Scanner reader = null;
//Surrogate restfull webservice
try {
while (true) {
reader = new Scanner(System.in);
System.out.println("Enter the number of the area you want a parking spot in: ");
int n = reader.nextInt();
Socket s = null;
ObjectOutputStream out;
ObjectInputStream in;
//connect to the proper area
s = new Socket("localhost", 7000 + n);
in = new ObjectInputStream(s.getInputStream());
out = new ObjectOutputStream(s.getOutputStream());
out.flush();
out.writeObject("check");
String str = (String) in.readObject();
if (!str.equals("Fail")) {
String[] split = str.split(" ");
sp= new SensorPackage(split[0], Integer.parseInt(split[1]), true);
System.out.println("request in area " + n + " now gives : " + sp.getStreetName() + " " + sp.getSpot());
} else {
System.out.println("No available spot found!");
}
out.close();
in.close();
s.close();
sp = null;
//To prevent unreachable code
if (i == 10)
break;
}
} catch (IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e){
e.printStackTrace();
}
reader.close();
return;
}
}