Advertisement
jadenkore

client

Jan 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. package tcpclientapp;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import java.net.Socket;
  9. import java.util.Scanner;
  10.  
  11. public class TCPClientApp {
  12.     public static void main(String[] args) {
  13.         //Name of the host that we are going to connect to
  14.         String hostName = "127.0.0.1";
  15.         //Make sure that this port is the same as    
  16.         //the server listening port        
  17.         int port =  12345;
  18.        
  19.        
  20.         while(true) {
  21.             Scanner reader = new Scanner(System.in);
  22.             // Reading from System.in
  23.             System.out.println("Please input a command: ");
  24.             String request = reader.nextLine(); // Scans the next token of the input as an int.
  25.             //once finished
  26.             reader.close();
  27.             try {
  28.                 //Use socket to connect to the server    
  29.                 Socket socket = new Socket(hostName, port);    
  30.                 //Access to the output stream      
  31.                 OutputStream os = socket.getOutputStream();
  32.                 //Write lie            
  33.                 PrintWriter pw = new PrintWriter(os);  
  34.                 pw.println(request);            
  35.                 pw.flush(); //Read response.            
  36.                 InputStream is = socket.getInputStream();
  37.                 //Read characters            
  38.                 InputStreamReader isr = new InputStreamReader(is);
  39.                 //Read lines            
  40.                 BufferedReader br = new BufferedReader(isr);            
  41.                 String header = br.readLine();            
  42.                 System.out.println("Response: " +  header );        
  43.                 }
  44.             catch (IOException ex) {
  45.                 ex.printStackTrace();        
  46.             }
  47.            
  48.         }
  49.        
  50.        
  51.        
  52.        
  53.        
  54.         /*
  55.         Boolean valid = true;
  56.         String test = "divide 2 0";
  57.         String cmd ="";
  58.         String arg1 ="";
  59.         String arg2 ="";
  60.         int num1 = 0;
  61.         int num2 = 0;
  62.         String response = "";
  63.         if(test.contains(" ")){
  64.             cmd= test.substring(0, test.indexOf(" "));
  65.            
  66.             if(valid){
  67.                 test = test.substring(test.indexOf(" ")+1);
  68.                
  69.                 if(test.contains(" ")){
  70.                     arg1 = test.substring(0, test.indexOf(" "));
  71.                     arg2 = test.substring(test.indexOf(" ")+1);
  72.                     //Checks if the args convert to numbers otherwise returns error
  73.                     try {
  74.                         num1 = Integer.parseInt(arg1);
  75.                     }
  76.                     catch( Exception e ) {
  77.                         response = "Error: \""+arg1+"\" is not a number";
  78.                         valid = false;
  79.                     }
  80.                     try {
  81.                         num2 = Integer.parseInt(arg2);
  82.                     }
  83.                     catch( Exception e ) {
  84.                         response = "Error: \""+arg2+"\" is not a number";
  85.                         valid = false;
  86.                     }
  87.                 }
  88.                 else{
  89.                     response = "Result: Invalid number of arguments";
  90.                     valid = false;
  91.                 }
  92.             }  
  93.         }
  94.         int res = 0;
  95.         if(cmd.equalsIgnoreCase("add")){
  96.             res = num1+num2;
  97.             response = "The add result is: "+ res;
  98.         }
  99.         else if(cmd.equalsIgnoreCase("subtract")){
  100.             res = num1-num2;
  101.             response = "The substract result is: "+ res;
  102.         }
  103.         else if(cmd.equalsIgnoreCase("multiply")){
  104.             res = num1*num2;
  105.             response = "The multiply result is: "+ res;
  106.         }
  107.         else if(cmd.equalsIgnoreCase("divide")){
  108.             if(num2==0){
  109.                 response = "Error: Divided by zero exception";
  110.             }
  111.             else{
  112.                 res = num1/num2;
  113.                 response = "The divide result is: "+ res;
  114.             }
  115.         }
  116.         else{
  117.             response =  "Error: Invalid command \""+cmd+"\"";
  118.             valid = false;
  119.         }
  120.         System.out.println(response);
  121.         */
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement