-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
executable file
·49 lines (45 loc) · 1.65 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
import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
private static void CommandPrompt(Scanner inFromUser, Scanner inFromServer, DataOutputStream outToServer) throws Exception
{
String sent;
System.out.print("taskmaster$ ");
/*read from the inputstream*/
String command = inFromUser.nextLine();
if (command.toLowerCase().equals("exit"))
{
System.exit(0);/*exit client program*/
}
else
{
/*write to outputstream*/
outToServer.writeBytes(command + '\n');
/*read from the outputstream*/
sent = inFromServer.nextLine();
//if (command.toLowerCase().equals("status"))
//{
// System.out.println("client Process status: " + sent);
//}
System.out.println("From Server: " + sent);
}
CommandPrompt(inFromUser, inFromServer, outToServer);
}
public static void main(String args[]) throws Exception
{
/*create an object to read values byte by byte from the inputstream*/
Scanner inFromUser = new Scanner(new InputStreamReader(System.in));
/*create a socket instance to create a communication bridge and connect to specified servername using specified port*/
Socket clientSocket = new Socket("localhost", 1900);
/*object to write values to a stream using the socket instance*/
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
/*object to read values from the input stream using socket instance*/
Scanner inFromServer = new Scanner(new InputStreamReader(clientSocket.getInputStream()));
/*call the function to handle data written and read*/
CommandPrompt(inFromUser, inFromServer, outToServer);
/*close communication or socket instance*/
clientSocket.close();
}
}