An Internet server application which returns the time to Internet clients

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MBeckford05
    New Member
    • Mar 2007
    • 7

    An Internet server application which returns the time to Internet clients

    Hi Everyone,

    I have written the code which gives the system time in a window. But I need also to have the Internet address and the port number of the client in the window. the TimeserverRF considers incoming DatagramPackets as a request for the current time, if the client wishes to receive the current time from TimeserverRF, it has only has to send a DatagramPacket to TimeserverRF.

    I am not sure how to do this writing DatagramPackets .

    I have the TimeClient class application. Timeclient should send the Internet address and the port on which it is running.

    Do You have any ideas.

    // the code for system time.

    import javax.swing.*;

    public class Window1 {

    public static void main(String[] args) {

    JFrame win = new JFrame("TimeSer ver running on port 9999");

    JTextArea tA = new JTextArea(1, 30);

    win.getContentP ane().add(tA);
    win.pack();
    win.setVisible( true);


    String s1; // initialise variables
    TimeData t1; // TimeData object initalise to t1 variable


    t1 = new TimeData(System .currentTimeMil lis()); // start of loop body

    s1 = t1.toTimeString (); // system time held sring s1


    tA.append("Sent "); tA.append(s1); // Prints statement and time in Frame





    }

    }
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    If I understood you correctly, you want to push out the current time to the clients connected?

    Cheers

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Hi!
      Is what you posted everything you have so far? If it is, do you know about Socket-Programming? You should, because you'll need it.

      Here's a very simple example of how Sockets work: An Echo Server and Client. (So, the Echo Client sends the Server a String and the Server sends it back.)
      [code=java]
      // EchoServer.java
      // Run: java EchoServer

      import java.net.*;
      import java.io.*;
      public class EchoServer
      {
      public static void main (String args[]) throws Exception
      {
      // Create a Server on Port 4242
      ServerSocket server = new ServerSocket(42 42);
      Socket socket = null;

      // The Server should run for ever - cancel it with CRTL+C
      while(true)
      {
      // Wait, until a client tries to connect
      socket = server.accept() ;

      // Prepare to read whatever the Client sends
      BufferedReader inStream = new BufferedReader(
      new InputStreamRead er(
      socket.getInput Stream()));
      // Prepare to send back something to the Client
      PrintStream outStream = new PrintStream(
      socket.getOutpu tStream());
      // What does the Client say?
      String input = inStream.readLi ne();
      System.out.prin tln("Echoing " + input);
      // Send it back
      outStream.print ln(input);
      // Close the socket, as we are finished and you want to start again.
      socket.close();
      }
      }
      }
      [/code]
      [code=java]
      // EchoClient.java
      // Run: java EchoClient [hostname] message

      import java.net .*;
      import java.io. *;
      public class EchoClient {
      public static void main (String args[]) throws Exception
      {
      // Create new Connection to the IP given or the local computer on port 4242
      Socket socket = new Socket ((args.length > 1 ? args[0] : "localhost" ), 4242);
      // Prepare to read, what the Server sends back
      BufferedReader inStream = new BufferedReader(
      new InputStreamRead er(
      socket.getInput Stream()));
      // Prepare to send something to the server
      PrintStream outStream = new PrintStream(
      socket.getOutpu tStream());
      // Send the given message (One word!) to the Server
      outStream.print ln(args.length( ) > 1 ? args[1] : args[0]);
      // Print, what the server sends back to you
      System.out.prin tln("Received: " + inStream.readLi ne());
      // Close the socket, as we are finished
      socket.close();
      }
      }
      [/code]This is a very basic example, but I think, it's a good start.

      If your Server should do more, like feature a website or do some wonderful things at some point in time, you may want to check out Tomcat instead. But if it's just this simple task, use Sockets.

      Greetings,
      Nepomuk

      PS.: Please use code tags, when posting code.
      Last edited by Nepomuk; May 6 '08, 03:17 PM. Reason: The OP isn't using code tags

      Comment

      Working...