-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.java
66 lines (60 loc) · 1.79 KB
/
Client.java
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
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Client {
private String ipAddress;
private int port;
private Socket socket = null;
private ObjectInputStream objis = null;
// one and only parameterized constructor
public Client(String ipAddress, int port) {
if (ipAddress == "localhost")
this.ipAddress = "127.0.0.1";
else
this.ipAddress = ipAddress;
this.port = port;
}
// responsible for connection to Server
boolean connectServer() throws UnknownHostException, IOException {
socket = new Socket(ipAddress, port);
System.out.println("Connected to " + ipAddress + ":" + port + " at " + new Date());
return true;
}
// responsible for fetching paper from server stream
boolean fetchPaper() throws ClassNotFoundException, IOException {
objis = new ObjectInputStream(socket.getInputStream());
AttendExam attendExam = (AttendExam) objis.readObject();
attendExam.serve();
return true;
}
// sends request to server
void requestServer() throws ClassNotFoundException, IOException {
connectServer();
fetchPaper();
}
public static void main(String[] agrs) {
if (agrs.length != 2) {
System.out.println("Please enter 2 arguments !");
System.out.println("First as Server address and Second Port number");
System.exit(0);
}
Client client = new Client(agrs[0], Integer.parseInt(agrs[1]));
try {
client.requestServer();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}