Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- import java.net.*;
- // Essential Libraries
- /**
- *
- * @author Deo Anthony Madrid
- */
- public class main{
- String helpMenu(String [] tokens)
- {
- int tokenSize = tokens.length;
- // Help menu for specified token
- String [] cmd = new String[tokenSize];
- String [][] helpArray = new String[][] {
- {"help", "\r\n help gives you a description of the specified command.\n"},
- {"time", "\r\n time gives you the current UNIX time from a UDP based server.\n"},
- {"rand", "\r\n rand gives you a randum number generated from a TCP based server.\n"},
- {"dict", "\r\n dict gives you a detailed description of a phrase/word.\n"},
- {"exit", "\r\n exit - ends the client session\n"}
- };
- for(int t = 1 ;t < tokenSize; t++){
- if(tokens[t]!=null){
- for(int i=0;i<5;i++){
- if(tokens[t].equals(helpArray[i][0])){
- cmd[t]=helpArray[i][1];
- }
- }
- }
- else{
- break;
- }
- t++;
- }
- if(tokenSize == 1) return "Available commands are: \r\n help \r\n time \r\n rand \r\n dict \r\n exit";
- else
- {
- StringBuffer sb = new StringBuffer();
- for(int s = 1; s < cmd.length; s++) {
- sb.append(cmd[s]);
- }
- String str = sb.toString();
- if(str.equals("null")){
- return("No Help for that Command!");
- }
- else{
- return str;
- }
- // return "This is the help command method";
- }
- }
- String dayTime(String [] args) throws IOException
- {
- // Start a daytime client to connect to server
- System.out.println("UDP DaytimeClient Running");
- String hostname = "localhost";
- int port = 40156;
- InetAddress address = InetAddress.getByName(hostname);
- DatagramSocket socket = new DatagramSocket();
- byte[] buffer = new byte[512];
- DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port);
- socket.send(request);
- byte[] receive = new byte[66666];
- Date date = new Date();
- String strDate = (date.toString());
- byte[] dateMsg = strDate.getBytes();
- DatagramPacket DpReceive = null;
- DpReceive = new DatagramPacket(receive, receive.length);
- // Step 3 : recieve the data in byte buffer.
- socket.receive(DpReceive);
- System.out.println("Client: " + DpReceive);
- // display time and date using toString()
- receive = new byte[66666];
- return (date.toString());
- // return "This is the time command method";
- }
- String randNum(String [] tokens)
- {
- int min = 1;
- int max = Integer.parseInt(tokens[1]);
- //Generate random int value from 1 to user specified max
- System.out.println("Random value in int from "+min+" to "+max+ ":");
- int randNum = (int)Math.floor(Math.random()*(max-min+1)+min);
- String output = Integer.toString(randNum);
- return output;
- }
- String dicT(String [] tokens)
- {
- // To be run on VM with working dict function installed
- ProcessBuilder processBuilder = new ProcessBuilder();
- processBuilder.command("dict",tokens[1]);
- try {
- Process process = processBuilder.start();
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(process.getInputStream()));
- String line;
- while ((line = reader.readLine()) != null) {
- System.out.println(line);
- }
- int exitCode = process.waitFor();
- System.out.println("\nExited with error code : " + exitCode);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return "This is the dict command method";
- }
- main(InputStream in, PrintStream out) throws IOException {
- while(true)
- {
- Scanner scanner=new Scanner(in);
- String line;
- String[] tokens;
- out.print("Enter a cmd: help, time, rand, dict, or exit: ");
- out.flush();
- while(scanner.hasNextLine()) {
- //Input stream for user input commands
- line=scanner.nextLine();
- tokens=line.split(" ");
- switch(tokens[0].toLowerCase())
- {
- case "help": out.println(helpMenu(tokens));
- break;
- case "time": out.println(dayTime(tokens));
- break;
- case "rand": out.println(randNum(tokens));
- break;
- case "dict": out.println(dicT(tokens));
- break;
- case "exit": out.println("Ending Client Session");
- System.exit(0);
- default: out.println("Bad command: " + tokens[0]);
- }
- out.print("Next command: ");
- out.flush();
- }
- }
- }
- public static void main(String[] args) throws IOException {
- // Main Method
- new main(System.in,System.out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment