Network Time Protocol (NTP) |
It is a networking protocol for clock synchronization between computer systems over packet-switched, variable latency data networks. There are several similar protocols such as Simple Network Time Protocol (SNTP), or Time Protocol using TCP on port 37. Es un protocolo de red para sincronizar relojes entre sistemas de computadoras con redes de datos de intercambio de paquetes con latencia variable. Hay varios protocolos similares tales como Simple Network Time Protocol (SNTP), o el Time Protocol usando TCP en el puerto 37. |
Tip |
There are several Time servers, some of them are:
Hay varios servidores de Tiempo, algunos de ellos son:
|
Problem 1 |
Create a Wintempla dialog application to retrieve the time from a time server; you need to remove the comments from the line #define WIN_SOCKETS_SUPPORT in the file stdafx.h. Because of security restrictions at the University, you must execute this program using your personal Internet access. Cree una aplicación de diálogo con Wintempla para consultar el tiempo de un servidor de tiempo; usted tiene que remover los comentarios de la línea #define WIN_SOCKETS_SUPPORT en el archivo stdafx.h. Debido a restricciones de seguridad en la Universidad, usted debe ejecutar este programa usando su acceso personal de Internet. |
LeerTiempo.cpp |
... void LeerTiempo::Window_Open(Win::Event& e) { } void LeerTiempo::btRetrieve_Click(Win::Event& e) { Sys::Socket socket; //__________________________________________________________________ Send Request if (socket.Connect(L"time-a.timefreq.bldrdoc.gov", 37) == SOCKET_ERROR) { this->MessageBox(socket.GetLastErrorDesc(), L"Leer tiempo", MB_OK | MB_ICONERROR); return; } //__________________________________________________________________ Receive Time unsigned __int32 utime; socket.Receive((char*)&utime, 4); //__________________________________________________________________ Convert and Display Sys::Time systime; wstring wdate, wtime; Sys::Convert::InternetTimeToSystemTime(utime, systime); Sys::Convert::TimeToString(systime, NULL, wtime); Sys::Convert::DateToString(systime, NULL, wdate); tbxTime.Text = wtime; tbxDate.Text = wdate; } |
Problem 2 |
Create an Android application called Now with an "Empty Activity" to test the Network Time Protocol. Edit the AndroidManifest.xml file to add the Internet connection permission. If you are using the simulator, you need to share your WiFi connection. Cree una aplicación para Android llamada Now con una "Empty Activity" para probar el Protocol de Tiempo en la Red. Edite el archivo AndroidManifest.xml para agregar el permiso de conexión a Internet. Si usted está usando el simulator, usted necesita compartir su conexión de WiFi. |
MSDOS: cmd.exe |
telnet time-b.nist.gov 13 58265 18-05-27 00:37:34 50 0 0 905.7 UTC(NIST) * Connection to host lost. Press any key to continue... |
AndroidManifest.xml |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.selo.now"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest> |
activity_main.xml |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tbxOutput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
MainActivity.java |
package com.selo.now; import android.os.StrictMode; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import java.net.Socket; import java.io.BufferedReader; import java.io.InputStreamReader; public class MainActivity extends Activity { private TextView tbxOutput = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //___________________________________________ 1. Allow main thread to run sockets (do not use in production) StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build()); tbxOutput = (TextView)findViewById(R.id.tbxOutput); Socket socket = null; try { socket = new Socket("time-b.nist.gov", 37); //socket = new Socket("time-b.nist.gov", 13); // socket = new Socket("120.78.181.57", 37); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while (true) { line = input.readLine(); if (line == null) break; tbxOutput.append(line + "\r\n"); } socket.close(); } catch (Exception e) { tbxOutput.setText(e.toString()); } } } |