1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| Imports System.Net.Sockets
Imports System.Text
#Region "Déclarations & structures"
Private m_UdpClient As UdpClient
Private Delegate Sub m_DelegateUdpReceive(ByVal str As String, ByVal sIP As String)
Private UDPPORTNO As Integer
Private result As List(Of String)
Private sRemoteIP As String = Nothing
a mettre dans FORM LOAD
UDPPORTNO = TON NUMERO DE PORT
Invalidate()
m_UdpClient = New UdpClient(UDPPORTNO) 'UDP port create..
m_UdpClient.BeginReceive(AddressOf UdpReceiveMessage, Nothing) 'BeginReceive
Exit Property
end sub
#Region " Méthodes privées "
Private Sub UdpReceiveMessage(ByVal IAr As IAsyncResult)
Dim receiveBytes() As Byte
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
receiveBytes = m_UdpClient.EndReceive(IAr, RemoteIpEndPoint)
Dim strReceived As String = Nothing
strReceived = Encoding.Default.GetString(receiveBytes)
Dim para() As Object = {strReceived, RemoteIpEndPoint.Address.ToString}
Me.Invoke(New m_DelegateUdpReceive(AddressOf Me.prRecerved), para) 'Thread receive winform
m_UdpClient.BeginReceive(AddressOf UdpReceiveMessage, Nothing) 'BeginReceive continue
End Sub
Private Sub prRecerved(ByVal paraSTR As String, ByVal pRemoteIP As String)
Dim localAdd As IPAddress = IPAddress.Parse(Dns.GetHostAddresses(My.Computer.Name)(0).ToString)
Dim myIP As String = Nothing
myIP = localAdd.ToString
Dim udpClient As New UdpClient(myIP, UDPPORTNO + 1)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(paraSTR)
Try
udpClient.Send(sendBytes, sendBytes.Length)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
ta_richtext_box.text = ta_richtext_box.text & paraSTR
End Sub
Private Sub ConnectedUsers()
'UDP users
Dim localAdd As IPAddress = IPAddress.Parse(Dns.GetHostAddresses(My.Computer.Name)(0).ToString)
Dim myIP As String = Nothing
myIP = localAdd.ToString
If myIP = Nothing Then Exit Sub
Dim pNum As Integer
Dim strSplit() As String = Nothing
strSplit = Split(myIP, ".")
Dim tUdpClient As UdpClient
tUdpClient = New UdpClient
Dim sendBytes As Byte() = Encoding.Default.GetBytes("/CON" & myIP.ToString & "$" & "NickName")
Me.Cursor = Cursors.WaitCursor
For pNum = 1 To 254
'192.xxx.xxx.1~254
sRemoteIP = strSplit(0) & "." & strSplit(1) & "." & strSplit(2) & "." & pNum
If sRemoteIP <> myIP Then
Try
Application.DoEvents()
tUdpClient.Connect(sRemoteIP, UDPPORTNO)
tUdpClient.Send(sendBytes, sendBytes.Length)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End If
Next pNum
Erase strSplit
tUdpClient.Close()
tUdpClient = Nothing
Me.Cursor = Cursors.Default
End Sub
end region |
Partager