代码整理——tempServer(运行自动关机)

import java.net.*;
import java.io.*;

public class TempServer {
	
	public static final int PORT = 9527;
	
	public static void main(String[] args) throws IOException
	{
		ServerSocket s = new ServerSocket(PORT);
		System.out.println("Started: " + s);
		try{
			Socket socket = s.accept();
			try{
				System.out.println("Connect accepted:" + socket);
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
				while(true)
				{
					String str = in.readLine();
					if(str.equals("END"))
					{
						String cmd = "shutdown -s -c \"goodbye boy\" -f -t 7200";                          
						Runtime run = Runtime.getRuntime();
						try{
							Process p = run.exec(cmd);
							BufferedInputStream in2 = new BufferedInputStream(p.getInputStream());             
							BufferedReader inBr = new BufferedReader(new InputStreamReader(in2));   
							String lineStr;
							while ((lineStr = inBr.readLine()) != null)
							{
								if (p.waitFor() != 0) {                    
								 if (p.exitValue() == 1)                         
									 System.err.println("命令执行失败!");                 
								}
								System.out.println(lineStr);
							}
							System.out.println("执行命令成功!");
							inBr.close();               
							in.close();
						}catch(Exception e) {                 
								e.printStackTrace();            
							}  
						break;
					}
					System.out.println("Echoing: " + str);
					out.println(str);
				}
			}finally
			{
				System.out.println("closing...");
				s.close();
			}
		}finally
		{
			s.close();
		}
	}
}

import java.net.*;
import java.io.*;

public class tempClient {
	public static void main(String[] args) throws IOException
	{
		InetAddress addr = InetAddress.getByName("WWW-870CC6B23E4");
		System.out.println("addr = " + addr);
		Socket socket = new Socket(addr,9527);
		try
		{
			System.out.println("socket = " + socket);
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
			for(int i=0;i<10;i++)
			{
				out.println("howdy "+i);
				String str = in.readLine();
				System.out.println(str);
			}
			out.println("END");
		}finally{
			System.out.println("closing...");
			socket.close();
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值