-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathClient.java
More file actions
51 lines (44 loc) · 1.7 KB
/
Client.java
File metadata and controls
51 lines (44 loc) · 1.7 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
package edu.sdccd.cisc191.template;
import java.net.*;
import java.io.*;
/**
* This program opens a connection to a computer specified
* as the first command-line argument. If no command-line
* argument is given, it prompts the user for a computer
* to connect to. The connection is made to
* the port specified by LISTENING_PORT. The program reads one
* line of text from the connection and then closes the
* connection. It displays the text that it read on
* standard output. This program is meant to be used with
* the server program, DateServer, which sends the current
* date and time on the computer where the server is running.
*/
public class Client {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public CustomerResponse sendRequest() throws Exception {
out.println(CustomerRequest.toJSON(new CustomerRequest(1)));
return CustomerResponse.fromJSON(in.readLine());
}
public void stopConnection() throws IOException {
in.close();
out.close();
clientSocket.close();
}
public static void main(String[] args) {
Client client = new Client();
try {
client.startConnection("127.0.0.1", 4444);
System.out.println(client.sendRequest().toString());
client.stopConnection();
} catch(Exception e) {
e.printStackTrace();
}
}
} //end class Client