-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
93 lines (78 loc) · 3.2 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package BusinessObjects;
/**
* CLIENT February 2019 DL 08/03/19
*
* This Client program asks the user to input commands to be sent to the server.
*
* There are only two valid commands in the protocol: "Time" and "Echo"
*
* If user types "Time" the server should reply with the current server time.
*
* If the user types "Echo" followed by a message, the server will echo back the
* message. e.g. "Echo Nice to meet you"
*
* If the user enters any other input, the server will not understand, and will
* send back a message to the effect.
*
* NOte: You must run the server before running this the client. (Both the
* server and the client will be running together on this computer)
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Client client = new Client();
client.start();
}
public void start() {
Scanner in = new Scanner(System.in);
try {
Socket socket = new Socket("localhost", 8080); // connect to server socket, and open new socket
System.out.println("Client: Port# of this client : " + socket.getLocalPort());
System.out.println("Client: Port# of Server :" + socket.getPort());
System.out.println("Client: This Client is running and has connected to the server");
String message = "please enter command:";
System.out.print(message);
String command;
OutputStream os = socket.getOutputStream();
PrintWriter socketWriter = new PrintWriter(os, true);
Scanner socketReader = new Scanner(socket.getInputStream());
labelB:
while (in.hasNextLine()) {
command = in.nextLine(); // read a command from the user
String[] lineWords = command.split(" ");
String subCommand = lineWords[0].toUpperCase();
socketWriter.println(command);
switch (subCommand) {
case "TESTMOVIES":
case "FINDMOVIEBYID":
case "FINDMOVIEBYTITLE":
case "FINDMOVIEBYYEAR":
case "FINDMOVIEBYDIRECTOR":
case "TOPTENMOVIES":
String m = socketReader.nextLine();
System.out.println("movies:\"" + m + "\"");
break;
case "DELETEMOVIE":
case "UPDATEMOVIETITLE":
case "INSERTNEWMOVIE":
String str = socketReader.nextLine();
System.out.println("Message:" + str);
break;
default:
break labelB;
}
System.out.print(message);
}
socketWriter.close();
socketReader.close();
socket.close();
} catch (IOException e) {
System.out.println("Client message: IOException: " + e);
}
}
}