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();
}
}
}