SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    monitorying program

    Hi all, i wonder if gurus here could help me with this worrying task. Basically, im trying to develop a client - server program. The function of the client program is to sit in the main utility and probe the server program sitting in the client machines waiting to accept connections. i.e the client program communicates with the client in regular intervals to check its working condition. Client sends a message if conection cannot be established thus enabling the main utility (operated by the administrator) to identify the problematic host.

    I have started by adapting the basic client - server program in textbooks

    CLIENT PROG (running on main utility)
    ------------------
    import java.net.*;
    import java.io.*;

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

    Socket s1;
    InputStream s1In;
    DataInputStream dis;

    //opening a connection to host(IP) a
    s1 = new Socket("192.168.2.4", 5432);
    s1In = s1.getInputStream();
    dis = new DataInputStream(s1In);
    String st = new String(dis.readUTF( ));
    System.out.println(st);

    dis.close();

    SERVER PROG: (running on clients on the network)
    ---------------
    import java.net.*;
    import java.io.*;

    public class SimpleServer {
    public static void main(String args[]) {
    //server sic
    ServerSocket s = null;
    Socket s1;
    String sendString = "Connected Successfully!";
    OutputStream os;
    DataOutputStream dos;


    try {
    s = new ServerSocket(5432);
    } catch (IOException e)
    {
    }

    while (true)
    {
    try {

    // wait here and listen for a connection

    s1 = s.accept();
    //Get a communication stream associated with the socket

    os = s1.getOutputStream();

    dos = new DataOutputStream (os);

    //Send your string! (UTF provides machine independence)

    dos.writeUTF(sendString);
    // close the connection, but not the server socket
    dos.close();
    os.close();

    s1.close();
    } catch (IOException e) {
    // ignore
    } } }
    }

    //to get host info
    /*try
    {
    InetAddress localaddr = InetAddress.getLocalHost();

    System.out.println ("Local IP Address : " + localaddr );
    System.out.println ("Local hostname : " + localaddr.getHostName());
    }
    catch (UnknownHostException e)
    {
    System.err.println ("Can't detect localhost : " + e);
    }*/
    s1In.close();
    s1.close();
    }
    }

    QUESTION:Im stuck on trying to define a number of hosts ( say 4) which return their IP, name etc when selected by the main utility (running the client program) and which program to define the clients..any responses will be greatly appreciated. thanks in advance.


    edit: code with comments will be very useful
    Java newbie

  2. #2
    SitePoint Evangelist borna's Avatar
    Join Date
    Jun 2006
    Location
    Iran, Tehran
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should take IP of users at each machine, see here,

    put them in session
    Code:
    session.setAttribute("IP",VALUE);
    and recall it appropriately,
    Code:
    session.getAttribute("IP");
    Last edited by borna; Dec 9, 2006 at 02:17.
    -1/2=999?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •