Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tcpclientapp;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Scanner;
- public class TCPClientApp {
- public static void main(String[] args) {
- //Name of the host that we are going to connect to
- String hostName = "127.0.0.1";
- //Make sure that this port is the same as
- //the server listening port
- int port = 12345;
- while(true) {
- Scanner reader = new Scanner(System.in);
- // Reading from System.in
- System.out.println("Please input a command: ");
- String request = reader.nextLine(); // Scans the next token of the input as an int.
- //once finished
- reader.close();
- try {
- //Use socket to connect to the server
- Socket socket = new Socket(hostName, port);
- //Access to the output stream
- OutputStream os = socket.getOutputStream();
- //Write lie
- PrintWriter pw = new PrintWriter(os);
- pw.println(request);
- pw.flush(); //Read response.
- InputStream is = socket.getInputStream();
- //Read characters
- InputStreamReader isr = new InputStreamReader(is);
- //Read lines
- BufferedReader br = new BufferedReader(isr);
- String header = br.readLine();
- System.out.println("Response: " + header );
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- /*
- Boolean valid = true;
- String test = "divide 2 0";
- String cmd ="";
- String arg1 ="";
- String arg2 ="";
- int num1 = 0;
- int num2 = 0;
- String response = "";
- if(test.contains(" ")){
- cmd= test.substring(0, test.indexOf(" "));
- if(valid){
- test = test.substring(test.indexOf(" ")+1);
- if(test.contains(" ")){
- arg1 = test.substring(0, test.indexOf(" "));
- arg2 = test.substring(test.indexOf(" ")+1);
- //Checks if the args convert to numbers otherwise returns error
- try {
- num1 = Integer.parseInt(arg1);
- }
- catch( Exception e ) {
- response = "Error: \""+arg1+"\" is not a number";
- valid = false;
- }
- try {
- num2 = Integer.parseInt(arg2);
- }
- catch( Exception e ) {
- response = "Error: \""+arg2+"\" is not a number";
- valid = false;
- }
- }
- else{
- response = "Result: Invalid number of arguments";
- valid = false;
- }
- }
- }
- int res = 0;
- if(cmd.equalsIgnoreCase("add")){
- res = num1+num2;
- response = "The add result is: "+ res;
- }
- else if(cmd.equalsIgnoreCase("subtract")){
- res = num1-num2;
- response = "The substract result is: "+ res;
- }
- else if(cmd.equalsIgnoreCase("multiply")){
- res = num1*num2;
- response = "The multiply result is: "+ res;
- }
- else if(cmd.equalsIgnoreCase("divide")){
- if(num2==0){
- response = "Error: Divided by zero exception";
- }
- else{
- res = num1/num2;
- response = "The divide result is: "+ res;
- }
- }
- else{
- response = "Error: Invalid command \""+cmd+"\"";
- valid = false;
- }
- System.out.println(response);
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement