这是我做的一个网络通信的代码,只要客户发一个“list”给服务器,服务器将把本机所运行的进程信息发给客户。
不过不知道哪里出了问题,服务器好像收不到客户发来的“list”,请帮我看下
服务代码:package reconserver;import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;public class Ser {
ServerSocket socket=null;
Vector mthreads=new Vector();
Ser(){
try{
socket=new ServerSocket(8000);
}catch(Exception e){
JOptionPane.showMessageDialog(new Frame(),"Build socket failed!",
"Failed",JOptionPane.ERROR_MESSAGE);
}
try{
while(true){
Socket s=socket.accept();
System.out.println("accept"+" "+s.getInetAddress());
ServerThread st=new ServerThread(s,mthreads);
st.setID(0);
mthreads.addElement(st);
new Thread(st).start();
}
}catch(Exception e){}
}
public static void main(String[] args) {
Ser ser = new Ser();
}
}
class ServerThread implements Runnable{
Socket soc=null;
Vector mthreads;
DataInputStream min=null;
DataOutputStream mout=null;
int mid;
public ServerThread(Socket s,Vector threads){
soc=s;
mthreads=threads;
try{
min=new DataInputStream(soc.getInputStream());
mout=new DataOutputStream(soc.getOutputStream());
}catch(Exception e){}
}
public void run(){
try{
while(true){
String s=min.readUTF();
System.out.println(s+"fddsd");
if(!s.equals(null)){
String send=handmessage(s);
ServerThread st=(ServerThread)mthreads.elementAt(0);
st.write(send);
}
}
}catch(Exception e){}
}
String handmessage(String s){
int t=s.indexOf("/");
if(t>=0){ }
else{
if(s.equals("list")){
System.out.println("dfsdffdf");
String list=listprocess();
return list;
}
}
return null;
}
String listprocess(){ //取得系统进程
String s=null;
Runtime time = Runtime.getRuntime();
String msg="";
String send="";
try {
Process process = time.exec("cmd exe /c tasklist /v /nh /fo csv");
InputStream stream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
stream)); s = reader.readLine();
while (s != null) {
if (s != null && !s.equals("")) {
s = s.substring(1, s.length() - 1);
String[] strs = s.split("\",\"");
String[] string = new String[5];
string[0] = strs[0]+" ";
string[1] = strs[1]+" ";
string[2] = strs[3]+" ";
string[3] = strs[4]+" ";
string[4] = strs[6]+",";
msg=string[0]+string[1]+string[2]+string[3]+string[4];
}
send=send+msg;
s = reader.readLine();
} }catch(Exception e){}
return send;
}
public void write(String msg){
synchronized(mout){
try{
mout.writeUTF(msg);
}catch(Exception e){}
}
}
public void setID(int id){
mid=id;
}
}客户代码package recon;import java.net.*;
import java.io.*;
public class getConnection implements Runnable{
Socket soc=null;
DataInputStream min;
DataOutputStream mout;
String message;
String send;
public getConnection(InetAddress IP,String send){
try{
this.send=send;
soc=new Socket(IP,8000);
min=new DataInputStream(soc.getInputStream());
mout=new DataOutputStream(soc.getOutputStream());
new Thread(this).start();
}catch(Exception e){
e.toString();
}
}
public String runable(String s){
return s;
}
public void run(){
try{
while(true){
String s=min.readUTF();
mout.writeUTF("list");
if(!s.equals(null))
//runable(s);
message=s;
}
}catch(Exception e){} }
}
不过不知道哪里出了问题,服务器好像收不到客户发来的“list”,请帮我看下
服务代码:package reconserver;import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;public class Ser {
ServerSocket socket=null;
Vector mthreads=new Vector();
Ser(){
try{
socket=new ServerSocket(8000);
}catch(Exception e){
JOptionPane.showMessageDialog(new Frame(),"Build socket failed!",
"Failed",JOptionPane.ERROR_MESSAGE);
}
try{
while(true){
Socket s=socket.accept();
System.out.println("accept"+" "+s.getInetAddress());
ServerThread st=new ServerThread(s,mthreads);
st.setID(0);
mthreads.addElement(st);
new Thread(st).start();
}
}catch(Exception e){}
}
public static void main(String[] args) {
Ser ser = new Ser();
}
}
class ServerThread implements Runnable{
Socket soc=null;
Vector mthreads;
DataInputStream min=null;
DataOutputStream mout=null;
int mid;
public ServerThread(Socket s,Vector threads){
soc=s;
mthreads=threads;
try{
min=new DataInputStream(soc.getInputStream());
mout=new DataOutputStream(soc.getOutputStream());
}catch(Exception e){}
}
public void run(){
try{
while(true){
String s=min.readUTF();
System.out.println(s+"fddsd");
if(!s.equals(null)){
String send=handmessage(s);
ServerThread st=(ServerThread)mthreads.elementAt(0);
st.write(send);
}
}
}catch(Exception e){}
}
String handmessage(String s){
int t=s.indexOf("/");
if(t>=0){ }
else{
if(s.equals("list")){
System.out.println("dfsdffdf");
String list=listprocess();
return list;
}
}
return null;
}
String listprocess(){ //取得系统进程
String s=null;
Runtime time = Runtime.getRuntime();
String msg="";
String send="";
try {
Process process = time.exec("cmd exe /c tasklist /v /nh /fo csv");
InputStream stream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
stream)); s = reader.readLine();
while (s != null) {
if (s != null && !s.equals("")) {
s = s.substring(1, s.length() - 1);
String[] strs = s.split("\",\"");
String[] string = new String[5];
string[0] = strs[0]+" ";
string[1] = strs[1]+" ";
string[2] = strs[3]+" ";
string[3] = strs[4]+" ";
string[4] = strs[6]+",";
msg=string[0]+string[1]+string[2]+string[3]+string[4];
}
send=send+msg;
s = reader.readLine();
} }catch(Exception e){}
return send;
}
public void write(String msg){
synchronized(mout){
try{
mout.writeUTF(msg);
}catch(Exception e){}
}
}
public void setID(int id){
mid=id;
}
}客户代码package recon;import java.net.*;
import java.io.*;
public class getConnection implements Runnable{
Socket soc=null;
DataInputStream min;
DataOutputStream mout;
String message;
String send;
public getConnection(InetAddress IP,String send){
try{
this.send=send;
soc=new Socket(IP,8000);
min=new DataInputStream(soc.getInputStream());
mout=new DataOutputStream(soc.getOutputStream());
new Thread(this).start();
}catch(Exception e){
e.toString();
}
}
public String runable(String s){
return s;
}
public void run(){
try{
while(true){
String s=min.readUTF();
mout.writeUTF("list");
if(!s.equals(null))
//runable(s);
message=s;
}
}catch(Exception e){} }
}
try{
while(true){
String s=min.readUTF();
mout.writeUTF("list"); // 嘿嘿, 问题在这里, 写了后, 要刷新一下输出流的缓冲区, 强制把缓冲区的内容写给服务器才行.
mout.flush(); // 加上这一句, 应该就可以了. 如果再不行的话, 后议.
if(!s.equals(null))
//runable(s);
message=s;
}
}catch(Exception e){} }
难道程序根本就没有进入run()?
在客户端没有main方法, 没有创建客户端对象来运行客户端程序.
import java.io.*;public class getConnection implements Runnable {
Socket soc = null;
DataInputStream min;
DataOutputStream mout;
String message;
String send; public getConnection(InetAddress IP, String send) {
try {
this.send = send;
soc = new Socket(IP, 8000);
min = new DataInputStream(soc.getInputStream());
mout = new DataOutputStream(soc.getOutputStream());
new Thread(this).start();
} catch (Exception e) {
e.toString();
}
} public String runable(String s) {
return s;
} public void run() {
try {
while (true) {
String s = min.readUTF();
mout.writeUTF("list");
if (!s.equals(null))
// runable(s);
message = s;
}
} catch (Exception e) {
} } public static void main(String[] args) {
try {
new getConnection(InetAddress.getByName("localhost"), "list");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
try {
getConnection get=new getConnection(IPA,"list");
System.out.println(get.message);
jd.setVisible(true);
} catch (Exception e1) {
e1.toString();
}
public void run(){
try{
while(true){
String s=min.readUTF(); //这段程序中的所有输出语句都没有执行
mout.writeUTF("list");
mout.flush(); //不管是用你的还是我的
//message="fdfdcv";
//System.out.println(message);
if(!s.equals(null))
//runable(s);
message="dfdfddddddd";
System.out.println(s);
}
}catch(Exception e){} }
1.你调用client的时候应该这样。new getConnection(IPA,"list").run(); //需要run,否则,你什么时候run你的线程呢?
2.
public void run(){
try{
while(true){
String s=min.readUTF(); //你服务器发过来信息了吗?否则,他会一直停留在这,不会继续执行了。
//如果你真的需要接受,那么你可以设置socket的SetSoTimeout,进行超时处理。
mout.writeUTF("list");
if(!s.equals(null))
//runable(s);
message=s;
}
}catch(Exception e){} }
有问题,再问我,
把S取出来,run()要一直运行,因为我还要利用它。
还有从服务那边传的消息s读出来的时候是不是有长度限制?
因为原来的程序服务端是把所有的进程信息全部写到send中,然后用st.write(send)发给客户
但不知道什么原因客户收不到完整的send,只能收到send的前面很少一部分。所以做点修改,但还是不能解决这个问题
希望帮我看看。
服务runpublic void run(){
try{
while(true){
String s=min.readUTF();
if(!s.equals(null)){
String send = handmessage(s);
send = send.substring(0, send.length() - 2);
String[] str = send.split("\".");
ServerThread st = (ServerThread) mthreads.elementAt(0);
for (int i = 0; i < str.length; i++) { 把send长度减小,一条条发过去
st.write(str[i]);
System.out.println(str[i]+" "+i);
if(i==str.length-1)
st.write(" ");
}
}
}
}catch(Exception e){}客户run()public void run(){
boolean ff=true;
try{
mout.writeUTF(send);
while(ff){
String s=min.readUTF(); //message是全局变量用来接收服务发来的信息
if(!s.equals(null)){
message = message+s;
}else if(s.equals(" ")) //如果服务发来空格代表数据发送完,run()退出
ff=false;
}
System.out.println("1"+message); //问题是根本接收不到服务发来的信息
}catch(Exception e){}
}
public void run(){
boolean ff=true;
try{
mout.writeUTF(send);
while(ff){
String s=min.readUTF();
if(!s.equals(null)){ //楼主这个判断是什么意思?如果s读不到,那么将一直停留在上面那行min.readUTF();一直等,不会出现null的
message = message+s;
}else if(s.equals(" "))
ff=false;
}
System.out.println("1"+message);
}catch(Exception e){}
}我先看到了这点,其他的我再看看。
应该这样写: public void run(){
boolean ff=true;
try{
mout.writeUTF(send);
while(ff){
String s=min.readUTF();
if(!s.equals(" ")){ //判断是否为“ ”
message = message+s;
System.out.println(message);
}else if(s.equals(" ")) //你原来的写法,永远执行不到这句的。
ff=false;
}
System.out.println("1"+message);
}catch(Exception e){} }
1、getConnection.java
...
mout.writeUTF("list");
String s=min.readUTF();//这两句位置是这样的,把你原来的程序中这两句颠倒一下
...
2、Ser.java
public void write(String msg){
synchronized(mout){
try{
mout.writeUTF(msg+"\n");//加个换行,方便看结果,不是必要的
}catch(Exception e){}
}
}
System Idle Process 0 0 28 K NT AUTHORITY\SYSTEM,System 4 0 60 K NT AUTHORITY\SYSTEM,smss.exe 628 0 96 K NT AUTHORITY\SYSTEM,csrss.exe 676 0 9,320 K NT AUTHORITY\SYSTEM,winlogon.exe 700 0 14,160 K NT AUTHORITY\SYSTEM,services.exe 744 0 3,676 K NT AUTHORITY\SYSTEM,lsass.exe 756 0 1,564 K NT AUTHORITY\SYSTEM,svchost.exe 916 0 3,152 K NT AUTHORITY\SYSTEM,svchost.exe 972 0 3,372 K NT AUTHORITY\NETWORK SERVICE,svchost.exe 1320 0 23,396 K NT AUTHORITY\SYSTEM,svchost.exe 1364 0 1,608 K NT AUTHORITY\NETWORK SERVICE,svchost.exe 1552 0 2,140 K NT AUTHORITY\LOCAL SERVICE,spoolsv.exe 1692 0 1,620 K NT AUTHORITY\SYSTEM,explorer.exe 1932 0 18,544 K F0F41F34EBFE4B7\Administrator,RTHDCPL.exe 2040 0 11,420 K F0F41F34EBFE4B7\Administrator,jusched.exe 240 0 376 K F0F41F34EBFE4B7\Administrator,hqtray.exe 368 0 1,724 K F0F41F34EBFE4B7\Administrator,xdict.exe 376 0 5,120 K F0F41F34EBFE4B7\Administrator,TSVNCache.exe 472 0 1,940 K F0F41F34EBFE4B7\Administrator,msdtc.exe 656 0 1,848 K NT AUTHORITY\NETWORK SERVICE,httpd.exe 1628 0 11,072 K NT AUTHORITY\SYSTEM,mDNSResponder.exe 1760 0 1,300 K NT AUTHORITY\SYSTEM,inetinfo.exe 1868 0 7,028 K NT AUTHORITY\SYSTEM,MDM.EXE 2004 0 1,836 K NT AUTHORITY\SYSTEM,sqlservr.exe 512 0 10,840 K NT AUTHORITY\NETWORK SERVICE,mysqld-nt.exe 1944 0 3,188 K NT AUTHORITY\SYSTEM,nvsvc32.exe 1172 0 792 K NT AUTHORITY\SYSTEM,pg_ctl.exe 1188 0 452 K NT AUTHORITY\SYSTEM,Apache.exe 1504 0 4,656 K NT AUTHORITY\SYSTEM,mysqld.exe 428 0 4,400 K NT AUTHORITY\SYSTEM,tcpsvcs.exe 792 0 844 K NT AUTHORITY\SYSTEM,sqlbrowser.exe 2168 0 308 K NT AUTHORITY\NETWORK SERVICE,sqlwriter.exe 2180 0 932 K NT AUTHORITY\SYSTEM,vmware-authd.exe 2212 0 1,428 K NT AUTHORITY\SYSTEM,httpd.exe 2436 0 3,828 K NT AUTHORITY\SYSTEM,postgres.exe 2448 0 1,716 K NT AUTHORITY\SYSTEM,Apache.exe 3676 0 5,152 K NT AUTHORITY\SYSTEM,vmount2.exe 3796 0 1,560 K NT AUTHORITY\SYSTEM,vmnat.exe 3888 0 540 K NT AUTHORITY\SYSTEM,mqsvc.exe 4232 0 1,164 K NT AUTHORITY\SYSTEM,vmnetdhcp.exe 4320 0 380 K NT AUTHORITY\SYSTEM,postgres.exe 4900 0 668 K NT AUTHORITY\SYSTEM,postgres.exe 5476 0 1,020 K NT AUTHORITY\SYSTEM,postgres.exe 5488 0 696 K NT AUTHORITY\SYSTEM,mqtgsvc.exe 5932 0 676 K NT AUTHORITY\SYSTEM,360tray.exe 5516 0 1,768 K F0F41F34EBFE4B7\Administrator,ctfmon.exe 5716 0 748 K F0F41F34EBFE4B7\Administrator,btdna.exe 5744 0 2,152 K F0F41F34EBFE4B7\Administrator,ApacheMonitor.exe 5756 0 1,012 K F0F41F34EBFE4B7\Administrator,privoxy.exe 5772 0 924 K F0F41F34EBFE4B7\Administrator,sqlmangr.exe 5816 0 1,688 K F0F41F34EBFE4B7\Administrator,QQ.exe 6080 0 41,284 K F0F41F34EBFE4B7\Administrator,wscntfy.exe 6108 0 660 K F0F41F34EBFE4B7\Administrator,alg.exe 4500 0 1,180 K NT AUTHORITY\LOCAL SERVICE,TXPlatform.exe 4624 0 2,004 K F0F41F34EBFE4B7\Administrator,firefox.exe 5660 0 129,864 K F0F41F34EBFE4B7\Administrator,XiaoiDesktop.exe 4268 0 5,412 K F0F41F34EBFE4B7\Administrator,eclipse.exe 2364 0 560 K F0F41F34EBFE4B7\Administrator,javaw.exe 5628 0 257,412 K F0F41F34EBFE4B7\Administrator,hh.exe 4944 0 13,992 K F0F41F34EBFE4B7\Administrator,AcroRd32.exe 4660 0 71,952 K F0F41F34EBFE4B7\Administrator,hh.exe 5860 0 15,572 K F0F41F34EBFE4B7\Administrator,SsReader.exe 996 0 21,232 K F0F41F34EBFE4B7\Administrator,dllhost.exe 6836 0 9,856 K F0F41F34EBFE4B7\IWAM_F0F41F34EBFE4B7,dllhost.exe 5376 0 8,564 K NT AUTHORITY\SYSTEM,javaw.exe 260 0 10,020 K F0F41F34EBFE4B7\Administrator,javaw.exe 5244 0 9,568 K F0F41F34EBFE4B7\Administrator,wmiprvse.exe 4688 0 6,256 K NT AUTHORITY\NETWORK SERVICE,cmd.exe 5016 0 2,824 K F0F41F34EBFE4B7\Administrator,tasklist.exe 6316 0 4,880 K F0F41F34EBFE4B7\Administrator,
System Idle Process 0 0 28 K NT AUTHORITY\SYSTEM,System 4 0 60 K NT AUTHORITY\SYSTEM,smss.exe 628 0 96 K NT AUTHORITY\SYSTEM,csrss.exe 676 0 9,320 K NT AUTHORITY\SYSTEM,winlogon.exe 700 0 14,160 K NT AUTHORITY\SYSTEM,services.exe 744 0 3,676 K NT AUTHORITY\SYSTEM,lsass.exe 756 0 1,564 K NT AUTHORITY\SYSTEM,svchost.exe 916 0 3,152 K NT AUTHORITY\SYSTEM,svchost.exe 972 0 3,372 K NT AUTHORITY\NETWORK SERVICE,svchost.exe 1320 0 23,348 K NT AUTHORITY\SYSTEM,svchost.exe 1364 0 1,608 K NT AUTHORITY\NETWORK SERVICE,svchost.exe 1552 0 2,140 K NT AUTHORITY\LOCAL SERVICE,spoolsv.exe 1692 0 1,620 K NT AUTHORITY\SYSTEM,explorer.exe 1932 0 18,544 K F0F41F34EBFE4B7\Administrator,RTHDCPL.exe 2040 0 11,420 K F0F41F34EBFE4B7\Administrator,jusched.exe 240 0 376 K F0F41F34EBFE4B7\Administrator,hqtray.exe 368 0 1,724 K F0F41F34EBFE4B7\Administrator,xdict.exe 376 0 5,120 K F0F41F34EBFE4B7\Administrator,TSVNCache.exe 472 0 1,940 K F0F41F34EBFE4B7\Administrator,msdtc.exe 656 0 1,848 K NT AUTHORITY\NETWORK SERVICE,httpd.exe 1628 0 11,072 K NT AUTHORITY\SYSTEM,mDNSResponder.exe 1760 0 1,300 K NT AUTHORITY\SYSTEM,inetinfo.exe 1868 0 7,028 K NT AUTHORITY\SYSTEM,MDM.EXE 2004 0 1,836 K NT AUTHORITY\SYSTEM,sqlservr.exe 512 0 10,840 K NT AUTHORITY\NETWORK SERVICE,mysqld-nt.exe 1944 0 3,188 K NT AUTHORITY\SYSTEM,nvsvc32.exe 1172 0 792 K NT AUTHORITY\SYSTEM,pg_ctl.exe 1188 0 452 K NT AUTHORITY\SYSTEM,Apache.exe 1504 0 4,656 K NT AUTHORITY\SYSTEM,mysqld.exe 428 0 4,400 K NT AUTHORITY\SYSTEM,tcpsvcs.exe 792 0 844 K NT AUTHORITY\SYSTEM,sqlbrowser.exe 2168 0 308 K NT AUTHORITY\NETWORK SERVICE,sqlwriter.exe 2180 0 932 K NT AUTHORITY\SYSTEM,vmware-authd.exe 2212 0 1,428 K NT AUTHORITY\SYSTEM,httpd.exe 2436 0 3,828 K NT AUTHORITY\SYSTEM,postgres.exe 2448 0 1,716 K NT AUTHORITY\SYSTEM,Apache.exe 3676 0 5,152 K NT AUTHORITY\SYSTEM,vmount2.exe 3796 0 1,560 K NT AUTHORITY\SYSTEM,vmnat.exe 3888 0 540 K NT AUTHORITY\SYSTEM,mqsvc.exe 4232 0 1,164 K NT AUTHORITY\SYSTEM,vmnetdhcp.exe 4320 0 380 K NT AUTHORITY\SYSTEM,postgres.exe 4900 0 668 K NT AUTHORITY\SYSTEM,postgres.exe 5476 0 1,020 K NT AUTHORITY\SYSTEM,postgres.exe 5488 0 696 K NT AUTHORITY\SYSTEM,mqtgsvc.exe 5932 0 676 K NT AUTHORITY\SYSTEM,360tray.exe 5516 0 1,768 K F0F41F34EBFE4B7\Administrator,ctfmon.exe 5716 0 748 K F0F41F34EBFE4B7\Administrator,btdna.exe 5744 0 2,152 K F0F41F34EBFE4B7\Administrator,ApacheMonitor.exe 5756 0 1,012 K F0F41F34EBFE4B7\Administrator,privoxy.exe 5772 0 924 K F0F41F34EBFE4B7\Administrator,sqlmangr.exe 5816 0 1,688 K F0F41F34EBFE4B7\Administrator,QQ.exe 6080 0 41,284 K F0F41F34EBFE4B7\Administrator,wscntfy.exe 6108 0 660 K F0F41F34EBFE4B7\Administrator,alg.exe 4500 0 1,180 K NT AUTHORITY\LOCAL SERVICE,TXPlatform.exe 4624 0 2,004 K F0F41F34EBFE4B7\Administrator,firefox.exe 5660 0 129,864 K F0F41F34EBFE4B7\Administrator,XiaoiDesktop.exe 4268 0 5,412 K F0F41F34EBFE4B7\Administrator,eclipse.exe 2364 0 560 K F0F41F34EBFE4B7\Administrator,javaw.exe 5628 0 257,412 K F0F41F34EBFE4B7\Administrator,hh.exe 4944 0 13,992 K F0F41F34EBFE4B7\Administrator,AcroRd32.exe 4660 0 71,952 K F0F41F34EBFE4B7\Administrator,hh.exe 5860 0 15,572 K F0F41F34EBFE4B7\Administrator,SsReader.exe 996 0 21,232 K F0F41F34EBFE4B7\Administrator,dllhost.exe 6836 0 9,856 K F0F41F34EBFE4B7\IWAM_F0F41F34EBFE4B7,dllhost.exe 5376 0 8,564 K NT AUTHORITY\SYSTEM,javaw.exe 260 0 10,072 K F0F41F34EBFE4B7\Administrator,javaw.exe 5244 0 9,584 K F0F41F34EBFE4B7\Administrator,wmiprvse.exe 4688 0 6,252 K NT AUTHORITY\NETWORK SERVICE,cmd.exe 7732 0 2,824 K F0F41F34EBFE4B7\Administrator,tasklist.exe 7244 0 4,876 K F0F41F34EBFE4B7\Administrator,
System Idle Process 0 0 28 K NT AUTHORITY\SYSTEM,System 4 0 60 K NT AUTHORITY\SYSTEM,smss.exe 628 0 96 K NT AUTHORITY\SYSTEM,csrss.exe 676 0 9,320 K NT AUTHORITY\SYSTEM,winlogon.exe 700 0 14,160 K NT AUTHORITY\SYSTEM,services.exe 744 0 3,676 K NT AUTHORITY\SYSTEM,lsass.exe 756 0 1,564 K NT AUTHORITY\SYSTEM,svchost.exe 916 0 3,152 K NT AUTHORITY\SYSTEM,svchost.exe 972 0 3,372 K NT AUTHORITY\NETWORK SERVICE,svchost.exe 1320 0 23,348 K NT AUTHORITY\SYSTEM,svchost.exe 1364 0 1,608 K NT AUTHORITY\NETWORK SERVICE,svchost.exe 1552 0 2,140 K NT AUTHORITY\LOCAL SERVICE,spoolsv.exe 1692 0 1,620 K NT AUTHORITY\SYSTEM,explorer.exe 1932 0 18,544 K F0F41F34EBFE4B7\Administrator,RTHDCPL.exe
怎么我的这里输出的却只有两个进程(前两个,使用没有减小字符串长度的方法)
分不是问题,只要能帮我解决这个问题。
PS:我的运行环境是jbuilder 2006
我用的eclipse3.2,应该跟IDE没关系的
我都调试过了,程序肯定没问题的
客户端我自己写了个main,因为你最上面发的代码没有
我把代码再给你发一下://Ser.java
package reconserver;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class Ser {
ServerSocket socket=null;
Vector mthreads=new Vector();
Ser(){
try{
socket=new ServerSocket(8000);
}catch(Exception e){
JOptionPane.showMessageDialog(new Frame(),"Build socket failed!",
"Failed",JOptionPane.ERROR_MESSAGE);
}
try{
while(true){
Socket s=socket.accept();
System.out.println("accept"+" "+s.getInetAddress());
ServerThread st=new ServerThread(s,mthreads);
st.setID(0);
mthreads.addElement(st);
new Thread(st).start();
}
}catch(Exception e){}
}
public static void main(String[] args) {
Ser ser = new Ser();
}
}
class ServerThread implements Runnable{
Socket soc=null;
Vector mthreads;
DataInputStream min=null;
DataOutputStream mout=null;
int mid;
public ServerThread(Socket s,Vector threads){
soc=s;
mthreads=threads;
try{
min=new DataInputStream(soc.getInputStream());
mout=new DataOutputStream(soc.getOutputStream());
}catch(Exception e){e.printStackTrace();}
}
public void run(){
System.out.println('d');
try{
while(true){
//System.out.println(min.readUTF().isEmpty());
String s=min.readUTF();
//System.out.println("fddsd");
if(!s.equals(null)){
String send=handmessage(s);
ServerThread st=(ServerThread)mthreads.elementAt(0);
st.write(send);
}
//}catch(Exception e){e.printStackTrace();}
}
}catch(Exception e){e.printStackTrace();}
//finally{System.out.println("god");}
}
String handmessage(String s){
int t=s.indexOf("/");
if(t>=0){ }
else{
if(s.equals("list")){
//System.out.println("dfsdffdf");
String list=listprocess();
return list;
}
}
return null;
}
String listprocess(){ //取得系统进程
String s=null;
Runtime time = Runtime.getRuntime();
String msg="";
String send="";
try {
Process process = time.exec("cmd exe /c tasklist /v /nh /fo csv");
InputStream stream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
stream)); s = reader.readLine();
while (s != null) {
if (s != null && !s.equals("")) {
s = s.substring(1, s.length() - 1);
String[] strs = s.split("\",\"");
String[] string = new String[5];
string[0] = strs[0]+" ";
string[1] = strs[1]+" ";
string[2] = strs[3]+" ";
string[3] = strs[4]+" ";
string[4] = strs[6]+",";
msg=string[0]+string[1]+string[2]+string[3]+string[4];
}
send=send+msg;
s = reader.readLine();
} }catch(Exception e){}
return send;
}
public void write(String msg){
synchronized(mout){
try{
mout.writeUTF(msg+"\n");
}catch(Exception e){}
}
}
public void setID(int id){
mid=id;
}
}//getConnection.java
package recon;
import java.net.*;
import java.io.*;
public class getConnection implements Runnable{
Socket soc=null;
DataInputStream min;
DataOutputStream mout;
String message;
String send;
public getConnection(InetAddress IP,String send){
try{
this.send=send;
soc=new Socket(IP,8000);
min=new DataInputStream(soc.getInputStream());
mout=new DataOutputStream(soc.getOutputStream());
new Thread(this).start();
}catch(Exception e){
e.toString();
}
}
public String runable(String s){
return s;
}
public void run(){
try{ System.out.println(soc.isConnected());
while(true){
mout.writeUTF("list");
String s=min.readUTF();
//System.out.println("dfdddd");
mout.flush();
System.out.print(s);
if(!s.equals(null))
//runable(s);
message=s;
}
}catch(Exception e){
}
}
}//Client.java,我简单的客户端,因为你没把你的客户端发上来,只发了个getConnection,这换成你的应该没问题package recon;
import java.net.*;
public class Client { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
new getConnection(InetAddress.getByName("localhost"),"df");
}catch(Exception e){
e.printStackTrace();
}
}}