All pastes #879978 Raw Edit

Unnamed

public text v1 · immutable
#879978 ·published 2008-01-29 18:49 UTC
rendered paste body
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
import java.nio.channels.*;
import java.lang.*;

public class hw1_srv_servicer implements Runnable {

	private Socket mySocket;
	private Boolean OnWindows;
	private String client_name;
	private String client_num;
	
	public hw1_srv_servicer(Socket csk, int cnum)
	{
		mySocket = csk;
		client_num = Integer.toString(cnum);
		client_name = mySocket.getInetAddress() + ":" + mySocket.getPort();
		
		String OsName = System.getProperty("os.name");
		if (OsName.indexOf("Windows") != -1 || OsName.indexOf("windows") != -1)
			OnWindows = true;
		else
			OnWindows = false;
	}
	
	public void run() {
		System.out.println("Connection Accepted from " + client_name + ". Assigned Number " + client_num);
		try {

			DataInputStream inFromClient = new DataInputStream(mySocket.getInputStream());
				DataOutputStream toClient = new DataOutputStream(mySocket.getOutputStream());
				toClient.writeChars("$READY$");
				toClient.flush();
				//String[] cmd = new String[3];
				String execcmd;
				byte[] buf = new byte[2048]; //2k should be good;
				int numbytes;
				String inCmd;
				int nullcount = 0;
				
				while (true)
				{
					System.out.println(client_num + ": Waiting for Command.");
					inCmd = inFromClient.readLine();
					System.out.println(client_num + ": Got Command.(" + inCmd + ")");
				
					// blank command, skip.
					if (inCmd == "" || inCmd == null)
					{
						nullcount++;
						if (nullcount > 10)
							break;
						continue; 
					}

					// got a command, reset null count
					nullcount = 0;
					if (inCmd == "QUIT")
						break;
					
					if (inCmd == "STOP SERVER")
					{
						// stop the server loop
						System.out.println(client_num + ":\"STOP SERVER\" received.");
						System.out.flush();
						hw1_server.KeepServerGoing = false;
						break;
					}
					
					if (OnWindows)
					{
						execcmd = "cmd.exe /C dir \"" + inCmd + "\" ";
					}
					else
					{
						execcmd = "ls \"" + inCmd +  "\" ";
					}
					System.out.println(client_num + ": Requested Directory: " + inCmd);
					System.out.flush();
					
					Runtime rt = Runtime.getRuntime();

					System.out.println(client_num + ": Executing \"" + execcmd + "\".");
					System.out.flush();
					
					Process proc = rt.exec(execcmd);
					BufferedInputStream child_stdout = new BufferedInputStream(proc.getInputStream());
					BufferedInputStream child_stderr = new BufferedInputStream(proc.getErrorStream());
				
					try {
						int exitVal = proc.waitFor();				
					}
					catch (Exception e)
					{
						System.out.println(client_num + ": Child Process Interrupted!");
						System.out.println(e);
						System.out.flush();
					}
					System.out.println();
					numbytes = child_stdout.read(buf, 0, 2048);
					toClient.write(buf, 0, numbytes);
					toClient.flush();
					
					// write a end-of-data marker
					toClient.writeChars("\n$END$\n");
					toClient.flush();
				}
		}
		catch (IOException e)
		{
			System.out.println(e);
		}
		System.out.println("Connection from " + client_name + "( Num " + client_num + ") Closed.");
		
	}

}