-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSClient.java
More file actions
115 lines (93 loc) · 3.56 KB
/
JSClient.java
File metadata and controls
115 lines (93 loc) · 3.56 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Femi Ayoola
*/
public class JSClient
{
private String serverName = "localhost";
private int serverPort = 4446;
private Socket socket = null;
BufferedReader inputStream; //Input: Server--> Client
BufferedReader inputStream2; //Input: User ---> Client
DataOutputStream outputStream; //Output: Client--> Server
//Needed Variables
String clientMessage;
String serverResponse;
public JSClient() throws Exception
{
//Make Connection
CSC();
//Send Messages Client<-->Server
JSClient();
}
public void CSC() throws Exception
{
try
{
//Creating Socket Object to Connect to Server
socket = new Socket("localhost",serverPort);
System.out.println("Connected to server " + socket.getRemoteSocketAddress());
//Input Stream To Get Data From The Server
inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Input Stream To Get Data From The Client User
inputStream2 = new BufferedReader(new InputStreamReader(System.in));
//Output Stream To Send Data To The Server
outputStream = new DataOutputStream(socket.getOutputStream());
}
catch(Exception e)
{
//Print Error
e.printStackTrace();
//Display To User That The Connection Has Failed
System.out.println("Connection Error ... Exiting");
//Closing the Client Program
System.exit(0);
}
}
public void JSClient()
{
//Prompting The User To Enter A String Message, Telling The User That 'stop' Is The Key Word To Shutdown The Connection
System.out.println("Enter 'client' to view list of job or 'stop' to break the connection");
try
{
//Getting Client Message From User
clientMessage = inputStream2.readLine();
//Looping Until Keyword Is Typed
while(clientMessage.compareTo("stop")!=0)
{
//Sending The Client Message Via Bytes
outputStream.writeBytes(clientMessage+"\n");
//Clearing Out The Buffer From Next Message
outputStream.flush();
try (ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
Object object = objectInputStream.readObject();
List<createjob> Job = (List<createjob>) object;
for (int i = 0; i < Job.size(); i++) {
System.out.println(Job.get(i));
}
}
//socket.close();
catch (IOException | ClassNotFoundException e) {
System.out.println("Error : " + e.getMessage());
}
}
//Closing All Open Streams
inputStream.close();
inputStream2.close();
outputStream.close();
socket.close();
//Displaying That The Connection Has Been Closed To The Client
System.out.println("Connection Has Been Closed");
}
catch(Exception e)
{
}
}
public static void main(String[] args) throws Exception {
JSClient JcClient = new JSClient();
}
}