Server Side



/*


	Author:ZT
	Date : Sep 29,2017
	Purpose:Developing tools for remote process





*/

/*


	To start the program for example : java GreetingServer  Port No
					   java GreetingSever  66
  now the server waiting(Listening) for any message coming from 
CLIENT   Side
where you are supposed you start and send the message


*/

package a.b.c;


// File Name GreetingServer.java
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.*;


public class GreetingServer extends Thread {
   private ServerSocket serverSocket;

	static PrintWriter out1=null; static FileOutputStream  fos=null; 
   
   public GreetingServer(int port) throws IOException {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(100000);
   }

   public void run()  {
	//try{
		File f1=new File("myt.txt");
		//FileOutputStream  
		
	 int i=0;
	 System.out.println("Waiting for client on port " + 
               serverSocket.getLocalPort() + "...");
	try{
		fos=new FileOutputStream(f1);
		 out1=new PrintWriter(fos);

	}catch(Exception r){

		System.out.println(r.getMessage());
	}
      while(true) {
	i++;
          try {
		
		//out1.println("hello Server");
		//out1.close();
           
            Socket server = serverSocket.accept();
            
           //  System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStream());
            String  msg1=in.readUTF();
            System.out.println(msg1);
		out1.println(msg1);
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
           // out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
           //    + "\nGoodbye!");
            server.close();
		

///
	if(msg1.equalsIgnoreCase("notepad")){

		out1.close();
		try{
			

			Process proc=Runtime.getRuntime().exec("notepad.exe myt.txt");
			// Process proc=Runtime.getRuntime().exec("mspaint.exe");

		}catch(Exception e){

			JOptionPane.showMessageDialog(null,e.getMessage());

		}


	}  // end IF 

	if(msg1.equalsIgnoreCase("mspaint")){

		// out1.close();
		try{
			
 			//Process proc3=Runtime.getRuntime().exec("notepad.exe tt.txt");
			  Process proc2=Runtime.getRuntime().exec("mspaint.exe");

		}catch(Exception e){

			JOptionPane.showMessageDialog(null,e.getMessage());

		}

	} // end if 
///

            
         }catch(SocketTimeoutException s) {
            System.out.println("Socket timed out!"); //out1.close();
            break;
         }catch(IOException e) {
            e.printStackTrace(); //out1.close();
            break;
         }

	finally{

		try{
			//out1.close();
		}catch(Exception ee){

			System.out.println(ee.getMessage());

		}


	}

	
      }
	out1.close();
	
   }
   
   public static void main(String [] args) {
      int port = Integer.parseInt(args[0]);
      try {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e) {
         e.printStackTrace();
      }
   }
}