Total members 9952 | Gratitudes |It is currently Sat Feb 11, 2012 2:36 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Topic
 Topic subject: PC health of remote sever (Mini Project)
PostPosted: Thu Oct 16, 2008 6:15 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 57 time

* Project Name:   PC health of remote sever
* Programmer:   Sumit Agarwal
* Type:   Network
* Technology:  Java
* IDE:   Any
* Description:   I have created a socket4444 on a PC which I treat as a server.
Now using my PC I am firing all system and DOS commands on the server and getting response of same on my PC.


Code:
// To be run at server side and will act as a server
// MainServer.java

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

public class MainServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4445);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4445.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

       PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);
        BufferedReader in = new BufferedReader(
      new InputStreamReader(
      clientSocket.getInputStream()));
        String inputLine, outputLine;
        MainProtocol kkp = new MainProtocol();

//        outputLine = kkp.processInput(null);
//        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
      if (inputLine.equals("bye"))
                break;
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
       outputLine = null;

        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

Code:
// Server side component used to execute commands and returning result
// MainProtocol.java


import java.lang.*;
import java.io.*;
public class MainProtocol {
   String sa,sb;
//   String sb,sa;

   public String processInput(String args) {
      try {
      sb = new String();
      sa  = new String();
      Runtime rt=Runtime.getRuntime();
      Process p=rt.exec("cmd /c "+ args);
      InputStream is = p.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      while ((sb = br.readLine()) != null)
      {
      sa += sb ;
      //   sa +="
";
      }

      p.waitFor();
      System.out.println("process "+p.exitValue());
      } catch(Exception e) {
      System.out.println(e.getMessage()+ "sumit");
      }

      return sa ;
   }
}


Code:

// Client side component. Commands are to fired from here only
// MainClient.java

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

public class MainClient {

    public static void main(String[] args) throws IOException {

      Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            kkSocket = new Socket("10.37.32.74", 4445);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new
InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Exception " + e);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
        String fromServer;
      String fromUser;

        while ((fromUser = stdIn.readLine()) != "hi") {
//            System.out.println("Server: " + fromServer);
//            if (fromServer.equals("Bye."))
//                break;

//            fromUser = stdIn.readLine();

       if (fromUser != null) {
//                System.out.println("Client: " + fromUser);
                out.println(fromUser);
      fromServer = in.readLine();
      System.out.println("Server Response: "+ fromServer);
      fromServer = null;
      if (fromUser.equals("bye"))
                break;
       }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}


Author mail : sumit.agarwal@zensar.com

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 java project code  Java  Anonymous  0
 project source code in java  Java  Anonymous  0
 project topic in java  Java  Anonymous  0
 I am doing my BCA final year. Looking out for a mini project  Servlets / JSP  Anonymous  1
 need of java code for doing project  Java  Anonymous  1

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Topic | Next Topic 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team