Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26187787
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-04-17 10:30:50

结构:B/S
中间层socket代码:

package cn.ty.server;

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.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.ty.server.SysUtils;
import cn.ty.server.LogTool;
public class MultiThreadServer {
    private int port = 8831;
    private ServerSocket serverSocket;
    private ExecutorService executorService;


    private final int POOL_SIZE = 10;
    public MultiThreadServer() throws IOException {
        this.serverSocket = new ServerSocket(port);
        executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
        System.out.println("server is ok");
    }
    public void service() {
        while(true) {
            Socket socket = null;
            try {
                socket = this.serverSocket.accept();
                executorService.execute(new Handler(socket));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        try {
            new MultiThreadServer().service();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
}
class Handler implements Runnable {
    private Socket socket;
    private LogTool log = new LogTool();
    public Handler(Socket socket) {
        this.socket = socket;
    }
    private PrintWriter getWriter(Socket socket) throws IOException {
        OutputStream socketout = socket.getOutputStream();
        return new PrintWriter(socketout,true);
    }
    private BufferedReader getReader(Socket socket) throws IOException {
        InputStream socketIn = socket.getInputStream();
        return new BufferedReader(new InputStreamReader(socketIn));
    }
    public String echo(String msg) {
        return "echo:" + msg;
    }
    public void run() {
        try {
            System.out.println("new connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
            BufferedReader br=getReader(socket);
            PrintWriter pw=getWriter(socket);
            String msg=null;
            String serverOut = null;
            while((msg=br.readLine())!=null){
                try {
                    String clientResult[] = msg.split("\\|");
                    if(clientResult[0].equalsIgnoreCase("show") && clientResult[1].equalsIgnoreCase("1")) {
                        serverOut = "serverok|"+showAllProcess('1', clientResult[2]);
                    } else if(clientResult[0].equalsIgnoreCase("show") && clientResult[1].equalsIgnoreCase("0")) {
                        serverOut = "serverok|"+showAllProcess('0', clientResult[2]);
                    } else if(clientResult[0].equalsIgnoreCase("kill") && clientResult[1].equalsIgnoreCase("1")) {
                        if(killProcess('1',clientResult[2])) {
                            serverOut = "serverok|0";
                        } else {
                            serverOut = "serverdown|0";
                        }
                    } else if(clientResult[0].equalsIgnoreCase("kill") && clientResult[1].equalsIgnoreCase("2")) {
                        if(killProcess('2',clientResult[2])) {
                            serverOut = "serverok|0";
                        } else {
                            serverOut = "serverdown|0";
                        }
                    }
                } catch (Exception e) {
                    log.deblog(e.toString());
                    serverOut = "serverdown|0";
                }
                pw.println(serverOut);
                pw.flush();
                if(msg.equals("bye")) break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(socket != null) {
                    socket.close();
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
    * FunName: showAllProcess
    * @Create Date: 2010-03-25
    */

    private String showAllProcess(char flag,String processName) {
        String commandString = "";
        String ostype = "";
        if(flag == '1' && System.getProperty("os.name").toLowerCase().indexOf("windows") == 0) {
            commandString = " ";
            ostype = "win";
        } else if(flag == '0' && System.getProperty("os.name").toLowerCase().indexOf("windows") == 0) {
            commandString = "tasklist";
            ostype = "win";
        } else if(flag == '0' && System.getProperty("os.name").toLowerCase().indexOf("linux") == 0) {
            commandString = "ps aux";
            ostype = "linux";
        } else if(flag == '1' && System.getProperty("os.name").toLowerCase().indexOf("linux") == 0) {
            commandString = "ps aux|grep \"" + processName + "\"";
            ostype = "linux";
        }
        return ostype + "|" + SysUtils.executeSysString(commandString);
    }
    /**
    * FunName: killProcess
    * Description :

阅读(953) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

hkebao2010-04-17 10:32:35

此软件的下载地址: http://blogimg.chinaunix.net/blog/upfile2/100417103159.rar