Time Server : Networks « J2ME « Java






Time Server


/*
J2ME in a Nutshell
By Kim Topley
ISBN: 0-596-00253-X

*/


import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;

public class TimeServer {
    public static void main(String[] args) {
                
        try {
            Calendar cal = Calendar.getInstance();
            DatagramConnection receiver = 
                    (DatagramConnection)Connector.open("datagram://:13");
            byte[] buffer = new byte[256];
            Datagram dgram = receiver.newDatagram(buffer, buffer.length);
            for (;;) {
                dgram.setLength(buffer.length);
                
                // Wait for somebody to call...
                receiver.receive(dgram);
                
                // Get the time and store it in the buffer
                cal.setTime(new Date());
                String time = cal.toString();
                byte[] dataBytes = time.getBytes();
                System.arraycopy(dataBytes, 0, buffer, 0, dataBytes.length);
                
                // Send back the reply
                dgram.setLength(dataBytes.length);
                receiver.send(dgram);
            }
        } catch (IOException ex) {
            System.out.println("IOException: " + ex);
        }
    }
}

           
       








Related examples in the same category

1.MIDlet to invoke a CGI script.
2.MIDlet to invoke a CGI script (POST method is used)
3.Https MIDlet
4.Pass a cookie (stored in rms) between the MIDlet and a Java servlet.
5.Use GET or POST to communicate with a Java servlet.
6.Use Java servlets sessions to tally golf scores.
7.Http Test
8.An example MIDlet to invoke a CGI script.An example MIDlet to invoke a CGI script.
9.Timer ServerTimer Server
10.Http ExampleHttp Example
11.Socket connectionSocket connection
12.Http ConnectionHttp Connection
13.Cookie MIDletCookie MIDlet
14.JargoneerJargoneer
15.Post MIDlet
16.Patchy MIDletPatchy MIDlet
17.MIDlet to invoke a CGI script (GET method).MIDlet to invoke a CGI script (GET method).
18.Fetch Page MidletFetch Page Midlet
19.Invoke Servlet Midlet 2
20.Invoke Servlet Midlet 1
21.MIDlet to invoke a CGI script (POST method is used) (2)MIDlet to invoke a CGI script (POST method is used) (2)
22.Demonstrates the functionality of DatagramConnection framework.Demonstrates the functionality of DatagramConnection framework.
23.Sample to demonstrate Http GET and POST from MIDlet
24.Get file from networkGet file from network
25.Midlet Servlet 2Midlet Servlet 2
26.MIDlet to fetch a page using an HttpConnectionMIDlet to fetch a page using an HttpConnection
27.A simple network clientA simple network client
28.Send client request and Get server responseSend client request and Get server response
29.Socket MIDletSocket MIDlet
30.www.amazon.com Book Ranking MIDletwww.amazon.com Book Ranking MIDlet
31.Http MIDletHttp MIDlet
32.DatagramSenderDatagramSender
33.Datagram Receiver