udpReceivingClient = New UdpClient(mnPort)
Module mod_Search_UDP Public mnPort As Int32 = 3040 'Port number to send/recieve data on 'Public Const msBroadcastAddress As String = "255.255.255.255" 'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client 'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort) Public udpReceivingClient As UdpClient 'Client for handling incoming data Public udpSendingClient As UdpClient 'Client for sending data Public receivingThread As Thread 'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up Public mbiClosing As Boolean = False 'Used to close clients if form is closing Public mbiCloseRxClient As Boolean = False Public Sub InitializeSender() Dim soc As Socket Dim lsPort As String Dim lnPort As Int32 = 3040 Const lsBroadcastAdd As String = "255.255.255.255" 'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort) 'udpSendingClient = New UdpClient(endPoint) udpSendingClient = New UdpClient(lsBroadcastAdd, lnPort) udpSendingClient.EnableBroadcast = True soc = udpSendingClient.Client lsPort = (CType(soc.LocalEndPoint, IPEndPoint).Port.ToString()) mnPort = Convert.ToInt32(lsPort) End Sub Public Sub InitializeReceiver() 'Create UdpClient class and bind it to the local port number provided 'Try udpReceivingClient = New UdpClient(mnPort) 'udpReceivingClient.EnableBroadcast = True mbiCloseRxClient = True 'Catch ex As Exception ' MsgBox(ex.ToString) 'End Try Dim start As ThreadStart = New ThreadStart(AddressOf MT_Receiver) receivingThread = New Thread(start) receivingThread.IsBackground = True receivingThread.Start() End Sub Public Sub MT_Receiver() Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, mnPort) 'Listen for incoming data from any IP address on the specified port Dim lbData() As Byte 'Buffer for storing incoming bytes Dim llRet As UInt16 'udpListen.Poll(1000, Net.Sockets.SelectMode.SelectRead) While (True) 'Setup an infinite loop If mbiClosing = True Then 'Exit sub if form is closing Exit Sub End If llRet = udpReceivingClient.Available If llRet > 0 Then lbData = udpReceivingClient.Receive(endPoint) 'Receive incoming bytes 'If udpListen.Available Then ' udpListen.Receive(lbData, 256, Net.Sockets.SocketFlags.None) 'End If 'If lbData Is Nothing Then 'Else frmSearchUDP.MT_Validate_Msg(lbData) End If End While End Sub Public Sub MT_Send_UDP(ByVal lbTxBuffer() As Byte) InitializeSender() If mbiCloseRxClient = True Then receivingThread.Abort() udpReceivingClient.Client.Dispose() udpReceivingClient.Close() End If Try udpSendingClient.Send(lbTxBuffer, lbTxBuffer.Length) Catch ex As Exception MsgBox(ex.ToString) End Try udpSendingClient.Client.Dispose() udpSendingClient.Close() InitializeReceiver() 'Try ' udpReceivingClient.BeginReceive(AddressOf MT_RX_Callback, Nothing) 'Catch ex As Exception ' MsgBox(ex.ToString) 'End Try End Sub End Module
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)