0

im making a program that you type ifconfig(Linux) or ipconfig(Windows) in the Client, Then it sends to the Server and there execute the shell.

It executes ok, and in the server I can see the ipconfig correctly, but i dont know how to return the result of the server to the client.

Client Side:

public class ShellClient {  
public static void main(String[] args) throws IOException {
        String comand;
        Socket server =null;
        server = new Socket("localhost", 1234);
        while(true){
        System.out.println("Insert ipconfig(windows)/ ifconfig(Linux): ");
        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);

        comando = keyboard.next();

        try{
             System.out.println("Conecting...");
             DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
             DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));

                out.writeUTF(comand);
                out.flush();
                String result = in.readUTF();
                System.out.println(result);


                if (comand == "0"){server.close();
                System.out.println("Finish. Thank You!");
                System.exit(0);}

        }catch(IOException ioe){
            System.err.println(ioe);
        }
      } 
    }
  }

Server Side:

public class ShellServer {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = null;
        Socket client = null;
        String result;
        String comand ;
        String s = null;
        socket = new ServerSocket(1234);  

        System.out.println("The server keep working...");
        client = socket.accept();
        System.out.println("The client is connected");
        DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));

            comand = in.readUTF();
            result = Shell.CheckCommand(comand);
            //out.writeUTF(result);
            //out.flush();

         // Ejcutamos el comando
            Process p = Runtime.getRuntime().exec(result);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                            p.getInputStream()));

            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
        }

            if(comand == "0"){client.close();
                         socket.close();}
            }
        }

Thank You in Advance Guys!

Funereal
  • 653
  • 2
  • 11
  • 32

2 Answers2

1

Server:

When you invoke a process and read from input stream, send what you received to out stream. Now Client doesn't receive anything at all so it has nothing to display. I would use BufferedReader (InputStreamReader) to read from client.

PrintWriter out = new PrintWriter(server.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));

out.println(comand);

String result;
while ((result = in.readLine()) != null )
{
    System.out.println(result);
}

Client: Just receive from server stream what it is sending by e.g.:

PrintWriter out = new PrintWriter(server.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out.println(comand);

String result;
while ((result = in.readLine()) != null )
{
    System.out.println(result);
}
Benjamin
  • 3,217
  • 2
  • 27
  • 42
1

If you do not want to use any threats, then you need a new connection for every request and you can handle only one connection per time

Try this

Client

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;

public class ShellClient {

    public static void main(String[] args) throws IOException {
        String comando;

        while (true) {
            System.out.println("Introduce ipconfig(windows)/ ifconfig(Linux): ");
            @SuppressWarnings("resource")
            Scanner keyboard = new Scanner(System.in);

            comando = keyboard.next();

            System.out.println("Conectando...");
            // We need a new connection for every request
            Socket server = new Socket("localhost", 1234);
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
            DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));

            out.writeUTF(comando);
            out.flush();
            // We close the output stream so the server knows we have finished the request
            server.shutdownOutput();
            if ("0".equals(comando)) {
                System.out.println("Finalizado. Gracias!");
                System.exit(0);
            }
            BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream(), "UTF-8"));
            String line;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            // See above - we need a new connection for every request
            server.close();
        }
    }
}

Server

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ShellServer {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = null;
        Socket client = null;
        String resultado;
        String comando;
        String s = null;
        socket = new ServerSocket(1234);

        System.out.println("El servidor sigue funcionando...");
        while (true) {
            client = socket.accept();
            System.out.println("El cliente se ha conectado");
            DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));

            comando = in.readUTF();
            if ("0".equals(comando)) {
                System.exit(0);
            }
            resultado = Shell.CheckCommand(comando);

            // Ejcutamos el comando
            Process p = Runtime.getRuntime().exec(comando);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            while ((s = stdInput.readLine()) != null) {
                out.write(s.getBytes("UTF-8"));
                out.write('\n');
                // we want to see each line as fast as we can
                out.flush();
                System.out.println(s);
            }
            client.close();            
        }
    }
}
drkunibar
  • 1,327
  • 1
  • 7
  • 7
  • That was just what I needed :) Thank You for the explanation. Now I understand what I was doing wrong :D – Funereal Apr 28 '14 at 07:26