用java获取并传出虚拟机系统实时性能参数(3:socket传递参数)

现在开始把各个虚拟机的信息传到服务器上,这里实际上是一个多线程单向的传输,在网上查找相关资料,推荐http://blog.csdn.net/zhangty0223/article/details/8744223,改出自己的代码,实验证明可用。MySystemMonitor类见我之前写的http://blog.csdn.net/taoxin52/article/details/8687852

server:

package bupt.tx.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import bupt.tx.socket.SocketSetting;

public class Server2MClient {

	public static void main(String[] args) throws IOException {
		@SuppressWarnings("resource")
		ServerSocket server = new ServerSocket(SocketSetting.SERVER_PORT);

		while (true) {
			Socket socket = server.accept();
			invoke(socket);
		}
	}

	private static void invoke(final Socket client) throws IOException {
		new Thread(new Runnable() {
			public void run() {
				BufferedReader in = null;
				PrintWriter out = null;
				try {
					in = new BufferedReader(new InputStreamReader(
							client.getInputStream()));
					out = new PrintWriter(client.getOutputStream());

					while (true) {
						String msg = in.readLine();
						System.out.println(msg);
						out.println("Server received " + msg);
						out.flush();
						if (msg.equals("bye")) {
							break;
						}
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				} finally {
					try {
						in.close();
					} catch (Exception e) {
					}
					try {
						out.close();
					} catch (Exception e) {
					}
					try {
						client.close();
					} catch (Exception e) {
					}
				}
			}
		}).start();
	}
}

client:

package bupt.tx.socket;

//import java.io.BufferedReader;  
//import java.io.InputStreamReader;  
import java.io.PrintWriter;  
import java.net.Socket;
import java.util.concurrent.TimeUnit;

import bupt.tx.systemmonitor.MySystemMonitor;
import bupt.tx.socket.SocketSetting;

public class MClient {

	 public static void main(String[] args) throws Exception { 
		int count = 0;
		
		Socket socket = new Socket(SocketSetting.SERVER_IP, SocketSetting.SERVER_PORT);  
//        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
        PrintWriter out = new PrintWriter(socket.getOutputStream()); 
//        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
 
        while (true) {  
            //String msg = reader.readLine(); 
        	out.println(MySystemMonitor.getIP()); 
            out.flush(); 
            out.println(MySystemMonitor.getTotalInfo()); 
            out.flush(); 
            //if (msg.equals("bye")) { 
            //    break;  
            //} 
            TimeUnit.MINUTES.sleep(1);
            if(count ++ > 10)
            	break;
        } 
        socket.close(); 
    } 
	 
}

setting:

package bupt.tx.socket;

public class SocketSetting {

	final public static int SERVER_PORT = 10000;
	
	//public static int MAXNOHBTIME = 60000;
	
	final public static String SERVER_IP = "10.***.***.***";

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值