- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
[C#] Hướng dẫn lấy thời gian thực server time trên winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn các lấy thời gian từ Server, ở một số địa chỉ server time Internet Services.
[C#] Get Server Time Winform
Trong lập trình ứng dụng, nhiều lúc các bạn muốn hiển thị thời gian thực tế đúng trên server, vì mỗi máy client có thể thời gian chạy sẽ không giống nhau.
Các bạn có thể truy cập vào đường link bên dưới để xem các địa chỉ IP Server Time Internet Services.
Bài viết này mình sẽ hướng dẫn lấy thời gian đó và hiển thị lên Winform.
Lưu ý: là khi lấy thời gian server, các bạn lấy request thời gian phải cách nhau, sau khi bạn lấy thời gian server về có thể sử dụng Timer để chạy tiếp thời gian.
Danh sách các địa chỉ IP Time Server:
Source code C# get Server Time:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ServerTime
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTime_Click(object sender, EventArgs e)
{
lbl_servertime.Text = ServerTime.getServerTime().ToLocalTime().ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
}
internal class ServerTime
{
private static string[] servers = { "132.163.96.1", "132.163.96.2", "129.6.15.28", "129.6.15.29" };
public static DateTime getServerTime()
{
foreach (string server in servers)
{
DateTime result = connectServer(server);
if (result > DateTime.MinValue)
return result;
}
return DateTime.MinValue;
}
private static DateTime connectServer(string host)
{
string time;
try
{
StreamReader read = new StreamReader(new TcpClient(host, 13).GetStream());
DateTime systemTime = DateTime.UtcNow;
time = read.ReadToEnd();
}
catch (Exception)
{
return DateTime.MinValue;
}
if (time.Substring(38, 9) != "UTC(NIST)") return DateTime.MinValue;
if (time.Substring(30, 1) != "0") return DateTime.MinValue;
int jd = int.Parse(time.Substring(1, 5));
int yr = int.Parse(time.Substring(7, 2));
int mo = int.Parse(time.Substring(10, 2));
int dy = int.Parse(time.Substring(13, 2));
int hr = int.Parse(time.Substring(16, 2));
int mm = int.Parse(time.Substring(19, 2));
int sc = int.Parse(time.Substring(22, 2));
if (jd < 15020) return DateTime.MinValue; //trước năm 1900
if (jd > 51544)
yr += 2000;
else
yr += 1900;
return new DateTime(yr, mo, dy, hr, mm, sc);
}
}
}
Thanks for watching!