slyderkiller
Messages postés145Date d'inscriptionlundi 28 avril 2003StatutMembreDernière intervention16 février 2012
-
27 mars 2008 à 21:30
cs_Jack
Messages postés14006Date d'inscriptionsamedi 29 décembre 2001StatutModérateurDernière intervention28 août 2015
-
27 mars 2008 à 21:52
slt a tt
Je voudrais me connecter a un serveur ntp pour qu'il me transmet l'heure mais je ne connais pas vraiment cette class
je bug sur la connexion il me dit que le serveur a rejeté ma connexion
Imports
System
Imports
System.Text
Imports
System.IO
Imports
System.Net
Imports
System.Net.Sockets
Imports
Microsoft.VisualBasic
Public
Class Form1
Private
Shared
Function ConnectSocket(
ByVal server
As
String,
ByVal port
As
Integer)
As Socket
Dim s
As Socket =
Nothing
Dim hostEntry
As IPHostEntry =
Nothing
' Get host related information.hostEntry = Dns.GetHostEntry(server)
' Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
' an exception that occurs when the host host IP Address is not compatible with the address family
' (typical in the IPv6 case).
Dim address
As IPAddress
For
Each address
In hostEntry.AddressList
Dim endPoint
As
New IPEndPoint(address, port)
Dim tempSocket
As
New Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp)tempSocket.Connect(endPoint)
If tempSocket.Connected
Thens = tempSocket
Exit
For
End
If
Next address
Return s
End
Function
' This method requests the home page content for the specified server.
Private
Shared
Function SocketSendReceive(
ByVal server
As
String,
ByVal port
As
Integer)
As
String
'Set up variables to write to the server.
Dim ascii
As Encoding = Encoding.ASCII
Dim request
As
String =
Date.UtcNow.ToString
Dim bytesSent(8)
As [Int64]
Dim bytesReceived(8)
As [Int64]bytesSent(0) =
CBool(1056964608)bytesSent(5) = 0
bytesSent(6) = 0
bytesSent(7) =
CBool(
Date.UtcNow.ToString)
' Create a socket connection with the specified server and port.
Dim s
As Socket = ConnectSocket(server, port)
If s
Is
Nothing
Then
Return
"Connection failed"
End
If
' Send request to the server.s.Send(bytesSent, bytesSent.Length, 0)
' Receive the server home page content.
Dim bytes
As Int32
' Read the first 256 bytes.
Dobytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
Loop
While bytes > 0
Return bytesReceived(5).ToString
End
Function
Private
Sub Button1_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button1.ClickLabel1.Text = SocketSendReceive(
"time.windows.com", 123)
End
SubEnd
Class