java socket 怎么发送命令字符串 让服务器执行 然后获得执行结果 如题最好 有代码贴出来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.BufferedWriter;import java.io.OutputStreamWriter;import java.net.ServerSocket;import java.net.Socket;public class UseNet{}class MyServer{ public static void main(){ ServerSocket ss = null; BufferedReader br = null; Socket sk = null; try{ System.out.println("create server socket...."); ss = new ServerSocket(IUseNet.PORT); System.out.println("wait for a connection...."); while(true){ sk = ss.accept(); System.out.println("get a socket object..."); br = new BufferedReader(new InputStreamReader(sk.getInputStream())); String line = null; while((line = br.readLine()) != null){ System.out.println(line); } } } catch(Exception ex){ System.out.println(ex.getMessage()); } finally{ try{ if(br != null){ br.close(); } if(ss != null){ ss.close(); } if(sk != null){ sk.close(); } } catch(Exception ex){ System.out.println(ex.getMessage()); } } }}class MyClient{ public static void main(String [] args){ Socket sk = null; PrintWriter pw = null; try{ sk = new Socket(IUseNet.IP,IUseNet.PORT); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()))); pw.println(args[0]); pw.flush(); } catch(Exception ex){ System.out.println(ex.getMessage()); } finally{ try{ if(sk != null){ sk.close(); } if(pw != null){ pw.close(); } } catch(Exception ex){ System.out.println(ex.getMessage()); } } }}interface IUseNet{ public static final String IP = "192.168.0.14"; public static final int PORT = 9999;}很长很长很长时间以前写的练习 不知道过时没有 这种问题让JAVA2000老师来说最合适 我要的是:给服务器端发送命令字符串的,比如ipconfig 让服务器执行,然后将结果返回给客户端 你发给服务器的时候 服务器 首先 判断 下 客户端发来的 字符串 是否 是 ipconfig 如果是的话 执行 什么 不是的话 在执行什么 可以去看看java里面用到run.process 没人做过这方面的吗? 用socket连接一台设备、给一些指令让他执行,然后返回一些数据给客户端的。 你把我的方法改一下就行了 首先是接受命令信息 然后执行Process process = Runtime.getRuntime().exec("命令"); process.waitfor(); 我这里有个JSP页面可以控制服务器的(前提是该JSP在服务器) 你可以参考下光是调用似乎可以 不过像这种ipconfig 返回信息LZ需要注意下这个问题重点是接受到信息 既然信息接受到了 另一个事情也就解除了嘛 要的话留邮箱 我发给你 谷歌一下看这个Process类怎么用。。 弱问一个swing的helloworld问题 谁可以提供Blowfish--ECB的源码,要求加密后的结果为16进制字节数组。 如何使用JMF来播放MP3文件 java 1.4中如何获取环境变量~!!~急~! 请问各位大哥,这个效果用java是如何实现的?请指教啊!小妹这厢有礼了!~~急啊!~ 一个纳闷的JTextArea 问题(再线等待) ◎◎◎◎◎◎ 散分提问,在线等 ◎◎◎◎◎◎ 请问:怎样删除文件中的一行? 如何让JCOMBOBOX有聚焦事件? 请教一个关于二维数组的问题 一道关于值传递的题? Java初学dos配置
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;import java.net.ServerSocket;
import java.net.Socket;public class UseNet{
}
class MyServer{
public static void main(){
ServerSocket ss = null;
BufferedReader br = null;
Socket sk = null;
try{
System.out.println("create server socket....");
ss = new ServerSocket(IUseNet.PORT);
System.out.println("wait for a connection....");
while(true){
sk = ss.accept();
System.out.println("get a socket object...");
br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{
try{
if(br != null){
br.close();
}
if(ss != null){
ss.close();
}
if(sk != null){
sk.close();
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}
class MyClient{
public static void main(String [] args){
Socket sk = null;
PrintWriter pw = null;
try{
sk = new Socket(IUseNet.IP,IUseNet.PORT);
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())));
pw.println(args[0]);
pw.flush();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{
try{
if(sk != null){
sk.close();
}
if(pw != null){
pw.close();
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}
interface IUseNet{
public static final String IP = "192.168.0.14";
public static final int PORT = 9999;
}
很长很长很长时间以前写的练习 不知道过时没有 这种问题让JAVA2000老师来说最合适
用socket连接一台设备、给一些指令让他执行,然后返回一些数据给客户端的。
你把我的方法改一下就行了 首先是接受命令信息 然后执行Process process = Runtime.getRuntime().exec("命令");
process.waitfor();
我这里有个JSP页面可以控制服务器的(前提是该JSP在服务器) 你可以参考下光是调用似乎可以 不过像这种ipconfig 返回信息LZ需要注意下这个问题重点是接受到信息 既然信息接受到了 另一个事情也就解除了嘛 要的话留邮箱 我发给你