gpt4 book ai didi

Java 执行 ipconfig ,客户端-服务器

转载 作者:行者123 更新时间:2023-12-02 05:53:32 25 4
gpt4 key购买 nike

我正在制作一个程序,您在客户端中键入 ifconfig(Linux) 或 ipconfig(Windows),然后它发送到服务器并在那里执行 shell。

执行正常,在服务器中我可以正确看到ipconfig,但我不知道如何将服务器的结果返回给客户端。

客户端:

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);
}
}
}
}

服务器端:

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();}
}
}

提前谢谢你们!

最佳答案

如果您不想使用任何威胁,那么您需要为每个请求建立一个新连接,并且每次只能处理一个连接

试试这个

客户端

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();
}
}
}

服务器

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();
}
}
}

关于Java 执行 ipconfig ,客户端-服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com