16,554
社区成员
发帖
与我相关
我的任务
分享
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'实例化一个Socket
Dim socket1 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
socket1.Connect("129.6.15.28", 37)
Dim buf As Byte() = New Byte(3) {}
socket1.Receive(buf, buf.Length, SocketFlags.None)
Dim ulTime As UInt32 = CType(BitConverter.ToInt32(buf, 0), UInt32)
ulTime = CType(IPAddress.NetworkToHostOrder(CType(ulTime, Integer)), UInt32)
Dim st As New DateTime(1900, 1, 1, 0, 0, 0)
st = st.AddSeconds(ulTime).ToLocalTime()
Dim nettime As New SystemTime()
nettime.wDay = st.Day
nettime.wYear = CShort(st.Year)
nettime.wMonth = CShort(st.Month)
nettime.wHour = CShort(st.Hour)
nettime.wMinute = CShort(st.Minute)
nettime.wSecond = CShort(st.Second)
nettime.wMilliseconds = CShort(st.Millisecond)
Win32API.SetLocalTime(nettime)
End Sub
End Class
Class Win32API
<DllImport("kernel32.dll")> _
Public Shared Function SetLocalTime(ByRef lpSystemTime As SystemTime) As Integer
End Function
End Class
Public Structure SystemTime
Public wYear As Short
Public wMonth As Short
Public wDayOfWeek As Short
Public wDay As Short
Public wHour As Short
Public wMinute As Short
Public wSecond As Short
Public wMilliseconds As Short
End Structure