Results 1 to 7 of 7
  1. #1
    mactoni is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Beginner with Socket client NTP calculate seconds from 1900

    Hi all,

    I'm a beginner with Java and we are studying sockets (server/clients). I already finished a client with http protocolo and now we have to develope one with NTP protocol.
    The question is that i have to display the number of "seconds" passed from 1900 with an integer of 32 bits result. The code i have modified from a JAVA book is the following and it works perfectly but the answer shows the date/time with the format HH:mm:ss.

    Can you help me to go on with it? Please my apologizes but my knowledge with java programming is very low.

    Thanks in advance.

    I post the code extractes from JAVA manual.

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


    public class Timereport {

    public final static int PORT=37; // Port per el NTP
    public final static String HOST="nist.time.nosc.us";
    // He escollit aquest però hi han molts actius
    public static void main(String [] args){
    String Servidor=HOST;
    int port=PORT;
    if(args.length > 0){
    Servidor=args[0];
    }
    if(args.length > 1){
    port=Integer.parseInt(args[1]);
    }
    long difference=2208988800l;
    InputStream raw=null;
    try
    {
    Socket socket=new Socket(Servidor, port);
    raw=socket.getInputStream();
    long secondsSince1900=0;
    for(int i=0; i<4; i++){
    secondsSince1900=(secondsSince1900 << 8)|raw.read();
    }
    long secondsSince1970=secondsSince1900-difference;
    long msSince1970=secondsSince1970*1000;
    Date time=new Date(msSince1970);
    System.out.println("It is "+time+" at "+Servidor);
    }catch(UnknownHostException uhEx){
    System.err.println(uhEx);
    }catch(IOException ioEx){
    System.err.println(ioEx);
    }finally{
    try{
    if(raw!=null)
    raw.close();
    }catch(IOException ex){
    }
    }


    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Beginner with Socket client NTP calculate seconds from 1900

    Are you asking how to convert one format to another format?
    Can you post the input data and the output you want to get from it and explain what math needs to be done to do that?
    Copy the program's output and add some comments saying what is wrong.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mactoni is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Beginner with Socket client NTP calculate seconds from 1900

    Yes that's right. I want to convert the format HH:MM:SS to an integer of 32 bits. The input data works pretty well with the NIST server selected and the current output is:

    Java Code:
    run:
    It is Mon Nov 04 [B][COLOR="#FF0000"]21:05:49[/COLOR][/B] CET 2013 at nist.time.nosc.us
    BUILD SUCCESSFUL (total time: 1 second)
    And must be an integer like: 23234343322 seconds

    Thanks again.

    Java Code:
    import java.net.*;  
    import java.io.*;  
    import java.util.*;
    
    
    public class Timereport {
        
        public final static  int PORT=37; // Port per el NTP 
        public final static String HOST="nist.time.nosc.us"; 
        // He escollit aquest però hi han molts actius
        public static void main(String [] args){  
            String Servidor=HOST;  
            int port=PORT;  
            if(args.length > 0){  
                Servidor=args[0];  
            }  
            if(args.length > 1){  
                port=Integer.parseInt(args[1]);  
            }  
            long difference=2208988800l;  
            InputStream raw=null;  
            try  
            {  
            Socket socket=new Socket(Servidor, port);  
            raw=socket.getInputStream();  
            long secondsSince1900=0;  
            for(int i=0; i<4; i++){  
                secondsSince1900=(secondsSince1900 << 8)|raw.read();  
            }  
            long secondsSince1970=secondsSince1900-difference;  
            long msSince1970=secondsSince1970*1000;  
            Date time=new Date(msSince1970);
            System.out.println("It is "+time+" at "+Servidor);
            }catch(UnknownHostException uhEx){  
                System.err.println(uhEx);  
            }catch(IOException ioEx){  
                System.err.println(ioEx);  
            }finally{  
                try{  
                    if(raw!=null)  
                        raw.close();  
                }catch(IOException ex){  
                }  
            }  
      
      
        }  
      
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Beginner with Socket client NTP calculate seconds from 1900

    I'm not sure what the input to the conversion is. Could you post a line with the input and another with the desired output.
    An example of the data, not actual values:
    input: 4/11/13 12:22:05
    output: 324565645
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mactoni is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Beginner with Socket client NTP calculate seconds from 1900

    Quote Originally Posted by Norm View Post
    I'm not sure what the input to the conversion is. Could you post a line with the input and another with the desired output.
    An example of the data, not actual values:
    input: 4/11/13 12:22:05
    output: 324565645
    Ok maybe it's my fault i didn't explain well the problem. With the code i have posted the program send a request to the server ""nist.time.nosc.us"" through the port 37 and the
    output is the current date and time: It is Mon Nov 04 21:05:49. It works well.

    But the output must be an integer something like 233443434343 (sample) with the number of seconds passed from 1900 until the current time.

    Sorry for my explanation and thanks for your collaboration

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Beginner with Socket client NTP calculate seconds from 1900

    But the output must be
    If a conversion needs to be done from one format to another, what does the data look like that is input to the conversion program? If a String put it in "s to show that. If an int it will be just decimal digits.

    What is the input for conversion to an int value?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    mactoni is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Beginner with Socket client NTP calculate seconds from 1900

    Great Norm!!! I have it!!! Thanks so muchhhh!

    long milliseconds = time.getTime();
    System.out.println("Seconds:"+ milliseconds/1000 +" at "+Servidor);

Similar Threads

  1. Replies: 4
    Last Post: 09-09-2010, 11:15 AM
  2. Replies: 3
    Last Post: 04-05-2010, 01:08 PM
  3. Need help with calculate the number of seconds
    By ProUnbeatable in forum New To Java
    Replies: 5
    Last Post: 10-11-2008, 03:07 AM
  4. Replies: 1
    Last Post: 06-30-2008, 07:14 AM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Tags for this Thread

Posting Permissions

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