Properly Disconnecting and Reconnecting an Asynchronous Connection - vb.net

I am currently developing a file sharing application using asynchronous communication in VB.Net. I successfully coded a simple connection between the client and the server. But, I am currently having a bad time doing the disconnection sub routine for both the client and the server. I'm currently stuck in this problem for a couple of days already and have decided to seek help here because I can't find any good ways to do the disconnection thing.
Here's my code:
Server Application
Private Sub FileSharingInitialize()
ServerSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim EndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 7777)
ServerSocket.Bind(EndPoint)
ServerSocket.Listen(0)
End Sub
Private Sub FileSharingOnAccept(ByVal AsyncResult As IAsyncResult)
ClientSocket = ServerSocket.EndAccept(AsyncResult)
FileSharingAddClient(ClientSocket)
ClientSocket.BeginReceive(ByteData, 0, ByteData.Length, SocketFlags.None, New AsyncCallback(AddressOf FileSharingOnReceive), ClientSocket)
End Sub
Delegate Sub _FileSharingAddClient(ByVal Client As Socket)
Private Sub FileSharingAddClient(ByVal Client As Socket)
If InvokeRequired Then
Invoke(New _FileSharingAddClient(AddressOf FileSharingAddClient), Client)
Exit Sub
End If
lvConnectedClients.Items.Add("")
lvConnectedClients.Items(lvConnectedClients.Items.Count - 1).SubItems.Add(Client.LocalEndPoint.ToString)
End Sub
Private Sub FileSharingOnReceive(ByVal AsyncResult As IAsyncResult)
Dim Client As Socket = CType(AsyncResult.AsyncState, Socket)
Client.EndReceive(AsyncResult)
Dim ByteReceived As Byte() = ByteData
Dim LogMessage As String = System.Text.Encoding.ASCII.GetString(ByteReceived)
ReadLogMessage(LogMessage)
End Sub
Delegate Sub _ReadLogMessage(ByVal LogMessage As String)
Private Sub ReadLogMessage(ByVal LogMessage As String)
If InvokeRequired Then
Invoke(New _ReadLogMessage(AddressOf ReadLogMessage), LogMessage)
Exit Sub
End If
DisplayLogMessage(LogMessage)
End Sub
Private Sub DisplayLogMessage(ByVal Log As String)
Dim TimeStamp As String = "[" & DateAndTime.Now.ToString("hh:mm:ss tt dddd, dd MMMM yyyy") & "]: "
Dim LogMessage As String = ""
If txtLog.Text > "" Then
LogMessage = Chr(10) & Chr(13)
End If
LogMessage &= TimeStamp
LogMessage &= Log
txtLog.SelectionStart = Len(txtLog.Text)
txtLog.SelectedText = LogMessage
End Sub
Private Sub frmMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
FileSharingInitialize()
End Sub
Private Sub btnStart_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnStart.MouseClick
ServerSocket.BeginAccept(New AsyncCallback(AddressOf FileSharingOnAccept), Nothing)
DisplayLogMessage("Server started." & Environment.NewLine)
End Sub
On the server part, when I press the stop button, I want to disconnect all the clients from the server. I have tried this code in my start button but it doesn't work. I'm receiving an error that says the socket is not connected. I'm thinking that maybe I am disconnecting the wrong socket since I have also a socket named ClientSocket in the server application. Or maybe, I should do it with a loop to disconnect the clients? What do you think?
Private Sub btnStop_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnStop.MouseClick
ServerSocket.Shutdown(SocketShutdown.Both)
ServerSocket.Close()
ServerSocket = Nothing
End Sub
Client Application
Private Sub btnConnect_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnConnect.MouseClick
ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim Address As IPAddress = IPAddress.Parse(frmNetworkSettings.txtServerIPAddress.Text)
Dim Endpoint As IPEndPoint = New IPEndPoint(Address, CInt(frmNetworkSettings.txtFileSharingPort.Text))
ClientSocket.BeginConnect(Endpoint, New AsyncCallback(AddressOf FileSharingOnConnect), Nothing)
SendLogMessage(Username & " is connected." & Environment.NewLine, ClientSocket)
End Sub
Private Sub FileSharingOnConnect(ByVal AsyncResult As IAsyncResult)
ClientSocket.EndConnect(AsyncResult)
End Sub
Private Sub SendLogMessage(ByVal LogMessage As String, ByVal Client As Socket)
Dim SendByteData As Byte() = System.Text.Encoding.ASCII.GetBytes(LogMessage)
Client.BeginSend(SendByteData, 0, SendByteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSendLogMessage), Client)
End Sub
Private Sub OnSendLogMessage(ByVal AsyncResult As IAsyncResult)
Dim Client As Socket = CType(AsyncResult.AsyncState, Socket)
Client.EndSend(AsyncResult)
End Sub
The problem on the client part is the same on the server. The code to disconnect the client from the server doesn't work. It doesn't send any logs to the server after I press the disconnect button, even when I reconnect afterwards. It seems that the socket has been disposed? If so, how can I prevent it from doing that and reuse the socket when I try to reconnect? Another thing, is that when I press the connect button without starting the server and afterwards start the server, a log is sent to the server that the client connected. It seems that the problem is on the listen method of the server. I don't want that to happen, I just want an exception or something that will alert the user that the client is not connected to the server and do not send a log message to the server afterwards the client connects. I don't really know, but do you think that if I set the backlog of the listen method to zero will prevent that from happening?
Thanks in advance!

Better way to do this is to implement flag system. When one client or server want to disconnect from other send disconnect flag to other end so on other end you code will understand disconnection

Related

How do I make the TCPClient send stuff to the same IP, but different places?

Ok, so, I have this code, that creates a TCPListener and a TCPClient, the code work, just not for me, my server is in my own home, in another machine, so I passed the server software to that and I tried to connect using my client, the client sends the request, the server picks it up, analyses it, and then throw it back, but, when it throws it back, the IP is the same so it's redirected to the server again, and the client never gets a response, This only happen in TCP tho, UDP do the job, but I need TCP to work
Server Code:
Dim Listener As New TcpListener(IPAddress.Any, 8123)
Dim Client As New TcpClient
Dim Message As String = ""
Dim games As New List(Of GameClass)
Dim queue As New Dictionary(Of String, IPEndPoint)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ListenerThread As New Thread(New ThreadStart(AddressOf Listening))
ListenerThread.Start()
End Sub
Private Sub Listening()
Listener.Start()
End Sub
Sub Send(msg As String, ip As IPEndPoint)
Try
Client = New TcpClient(ip)
ListBox1.Items.Add("Sending: " & msg & " to: " & ip.Address.ToString)
'Declare the Client as an IP Address.
'Must be in the Correct form. eg. 000.0.0.0
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(msg)
Writer.Flush()
'Write the Message in the stream
Catch ex As Exception
Try
Client = New TcpClient(New IPEndPoint(IPAddress.Parse("127.0.0.1"), ip.Port))
ListBox1.Items.Add("Sending: " & msg & " to: " & ip.Address.ToString)
'Declare the Client as an IP Address.
'Must be in the Correct form. eg. 000.0.0.0
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(msg)
Writer.Flush()
Catch ex1 As Exception
End Try
End Try
End Sub
Sub Send(msg As String, index As Integer, ip As IPEndPoint)
Try
For Each item In games(index).IPAddresses
If item.Value.Address.ToString = ip.Address.ToString Then
Else
Client = New TcpClient(ip)
'Declare the Client as an IP Address.
'Must be in the Correct form. eg. 000.0.0.0
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(msg)
Writer.Flush()
End If
Next
Catch ex As Exception
Try
Client = New TcpClient(New IPEndPoint(IPAddress.Parse("127.0.0.1"), ip.Port))
ListBox1.Items.Add("Sending: " & msg & " to: " & ip.Address.ToString)
'Declare the Client as an IP Address.
'Must be in the Correct form. eg. 000.0.0.0
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(msg)
Writer.Flush()
Catch ex1 As Exception
End Try
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While
Parse(Message, Client.Client.RemoteEndPoint)
End If
End Sub
Any suggestions, please, If it's duplicate, please just link to another post, I don't have even the words to ask this on google
Can you break this down into 2 sections? Client Send & Recieve code and server send & receive code?
First off no computer has the same IP. Your client IP machine can have the IP address of 192.168.1.125 and your server machine could have 192.168.1.56. 2 machines connected to a network can never have a conflicting IP address.
I have successfully coded my own client/server application that works on the internet and the intranet (home network). I can help you.
But please break your code down into 2 sections of Client Side & Server Side.

VB.NET: TCP Client Socket timeout?

I have a TCP Client socket that connects to a server when Form1 loads. I need a way for the client socket to "give up" trying to connect to the remote server if it takes too long to connect. i.e Time out after 30 seconds.
Currently when the socket cannot connect to the server for what ever reason the form will appear to freeze up until the server can be reached.
Its probably worth noting im using System.Net.Sockets.TcpClient()
Here is My Sub for creating the connection:
Public serverStream As NetworkStream
Public clientSocket As New System.Net.Sockets.TcpClient()
Public Sub CreateTCPConnection()
Try
clientSocket.Connect(System.Net.IPAddress.Parse(My.Settings.ServerIP), My.Settings.ServerPort)
ConnectionStatusLbl.Text = "Connection: Connected"
ConnectionStatusPB.Image = My.Resources.LED_Green
'This should work according to MSDN but does not
clientSocket.SendTimeout = 3000
clientSocket.ReceiveTimeout = 3000
UpdateTimer.Start()
Catch ex As Exception
ConnectionStatusLbl.Text = "Connection: Not Connected"
ConnectionStatusPB.Image = My.Resources.LED_Red
clientSocket.Close()
MsgBox(ex.ToString)
End Try
End Sub
You could thread the whole CreateTCPConnectionfunction, then you use a Timer in the Mainthread to abort the thread after X Seconds, if a public boolean is not setted to true.
Pseudo Code for the thread initialization:
Dim isConnected as Boolean
Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
isConnected = false;
trd = New Thread(AddressOf CreateTCPConnection)
trd.IsBackground = True
//Afer the start function the thread tries to connect asyncron to your server
trd.Start()
Timer timer = new Timer();
//Start the timer to check the boolean
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Start()
End Sub
Private Sub CreateTCPConnection()
//Your Connection method
End Sub
//Checks the isConnected status each second. If isConnected is false and 3 seconds are reached, the thread gets aborted.
Private Sub TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
if(myEventArgs.elapsedTime >= 3 AND isConnected == false) Then
trd.Abort();
End if
End Sub
This is a small workaround.

How to check IP Address changes in VB.NET?

How do I detect live IP Changes?
Example:
My ip without VPN: 3.3.3.3, whenever I connect to vpn my ip changes to 5.5.5.5.
My question is: How do I detect the IP has changed when the form is running?
I have tried:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
AddHandler NetworkChange.NetworkAddressChanged, AddressOf downloadip
Dim n As NetworkInterface
For Each n In adapters
Timer4.Stop()
Msgbox("IP Changed")
Next n
End Sub
Try checking your IP on an external website
for example:
Public Sub GetExternalIp()
Try
Dim WithEvents IPBrowser As New WebBrowser
IPBrowser.Visible = False
Me.Controls.Add(IPBrowser)
IpBrowser.Navigate("http://seemyip.com/onyoursite.php")
Catch
Label2.Text = "failed to get external IP"
End Try
End Sub
'then, put this OnNavigatingEvent
If IpBrowser.Document.All("ip").GetAttribute("value") IsNot Nothing Then
MyExtIp = IpBrowser.Document.All("ip").GetAttribute("value")
Else
MessageBox.Show("Can't find your IP")
End If

TCP/ip recive connection but locks up when trying to read data

Please can someone tell me why my server locks up when my rclient connects and try to send data?
If I comment out:
Dim bytes(rclient.ReceiveBufferSize) As Byte
rstream.Read(bytes, 0, CInt(rclient.ReceiveBufferSize))
RString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = RStringcode here
and just have
TextBox2.Text = ("Remote connected")
the server picks up on a connected client so it must be something i have done with the .read ?
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO.Ports
Dim tclient As New TcpClient
Dim tstream As NetworkStream
Dim rclient As New TcpClient
Dim rstream As NetworkStream
Dim sArray() As String
Dim Tricopter As New TcpListener(2000)
Dim Remote As New TcpListener(2001)
Dim myVal As String
Dim TricopterThread As New Thread(AddressOf TricopterThreadSub)
Dim RemoteThread As New Thread(AddressOf RemoteThreadSub)
Dim tre(tclient.ReceiveBufferSize) As Byte
Dim TState = False
Dim RState = False
Dim SerialSwitch = False
Dim Toggle = False
Dim TString As String
Dim RString As String
Dim Remo(rclient.ReceiveBufferSize) As Byte
Dim TSendText() As Byte
Private Sub RemoteThreadSub()
rclient = Remote.AcceptTcpClient
rstream = rclient.GetStream
RState = True
End Sub
Private Sub TricopterThreadSub()
tclient = Tricopter.AcceptTcpClient
tstream = tclient.GetStream
TState = True
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
Tricopter.Start()
Remote.Start()
Timer1.Start()
TricopterThread.Start()
RemoteThread.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
myVal = ">" & TrackBar1.Value & "," & TrackBar2.Value & "," & TrackBar3.Value & "," & TrackBar4.Value & "/n"
Try
If TState = False Then
TextBox1.Text = ("No tricopter connected")
Else
TextBox1.Text = myVal
If Toggle = False Then
TSendText = Encoding.ASCII.GetBytes(myVal)
End If
If Toggle = True Then
TSendText = Encoding.ASCII.GetBytes(RString)
End If
tstream.Write(TSendText, 0, TSendText.Length)
End If
If RState = False Then
TextBox2.Text = ("No Remote connected")
Else
' TextBox2.Text = ("Remote connected")
Dim bytes(rclient.ReceiveBufferSize) As Byte
rstream.Read(bytes, 0, CInt(rclient.ReceiveBufferSize))
RString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = RString
End If
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Toggle = False Then
CheckBox1.CheckState = 1
Toggle = True
Else
CheckBox1.CheckState = 0
Toggle = False
End If
End Sub
End Class
You are assuming that reads return a specific amount of data such as ReceiveBufferSize. That is not true. A read returns at least one byte, that is all.
Just to clarify, TCP does not support message based transfer.
The correct way to read depends on the protocol. If the exact number of bytes expected is known you need to read in a loop until so many bytes are received (or use BinaryReader which does that for you).
For a line based protocol you can use StreamReader.ReadLine which again automates the looping.
ReceiveBufferSize is completely unrelated to how much data is available or will come.
DataAvailable is how much data can be read right now without blocking. But more data might come. It is almost always a bug to use it. Might return 0 at any time even if data comes in 1ms later.
EDIT: For those of you who have downloaded/tested this already, I made a bugfix to the classes so you'll need to redownload the sources if you're going to use them again.
If you want to perform proper data transfer you'll need to use a more reliable method than just simply reading random data. And as usr pointed out: the TcpClient.ReceiveBufferSize property does not tell you how much data there is to receive, nor how much data there is sent to you. ReceiveBufferSize is just a variable indicating how many bytes you are expected to get each time you read incoming data. Read the MSDN page about the subject for more info.
As for the data transfer, I've created two classes which will do length-prefixed data transfer for you. Just import them to your project and you'll be able to start using them immediatelly. Link: http://www.mydoomsite.com/sourcecodes/ExtendedTcpClient.zip
Example usage
Server side
First declare a new variable for ExtendedTcpClient, and be sure to
include WithEvents in the declaration.
Dim WithEvents Client As ExtendedTcpClient
Then you just need to use a normal TcpListener to check for
incoming connections. The TcpListener.Pending() method can be
checked in for example a timer.
When you are to accept a new TcpClient, first declare a new
instance of the ExtendedTcpClient. The class requires to have a
form as it's owner, in this application Me is the current form.
Then, use the ExtendedTcpClient.SetNewClient() method with
Listener.AcceptTcpClient() as it's argument to apply the
TcpClient from the listener.
If Listener.Pending() = True Then
Client = New ExtendedTcpClient(Me)
Client.SetNewClient(Listener.AcceptTcpClient())
End If
After that you won't be needing the timer anymore, as the
ExtendedTcpClient has it's own thread to check for data.
Now you need to subscribe to the PacketReceived event of the
client. Create a sub like so:
Private Sub Client_PacketReceived(sender As Object, e As ExtendedTcpClient.PacketReceivedEventArgs) Handles Client.PacketReceived
End Sub
In there you can for example output the received packet as text into
a TextBox. Just check if the packet header is PlainText and then
you can convert the received packets contents (which is an array of
bytes, accessed via e.Packet.Contents) to a string and put it in
the TextBox.
If e.Packet.Header = TcpMessagePacket.PacketHeader.PainText Then
TextBox1.AppendText("Message recieved: " & System.Text.Encoding.Default.GetString(e.Packet.Contents) & Environment.NewLine)
End If
Lastly, when closing the form you just need to disconnect the client.
Private Sub ServerWindow_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Client IsNot Nothing Then Client.Disconnect()
End Sub
And that's it for the server side.
Client side
For the client side you will only be needing a normal TcpClient (unless you don't want to receive data there too).
Dim Client As New TcpClient
Then connect to the server via the IP and port you've given the listener.
Client.Connect("127.0.0.1", 5555) 'Connects to localhost (your computer) at port 5555.
Now if you want to send plain text to the server you'd do something like this:
Dim MessagePacket As New TcpMessagePacket(System.Text.Encoding.Default.GetBytes(TextBox2.Text), TcpMessagePacket.PacketHeader.PainText)
MessagePacket.Send(Client) 'Client is the regular TcpClient.
And now everything should be working!
Link to a complete example project: http://www.mydoomsite.com/sourcecodes/TCP%20Messaging%20System.zip
If you want to add more headers to the class, just open TcpMessagePacket.vb and add more values in the PacketHeader enum (located in the region called Constants).
Hope this helps!
Screenshot from the example project
(Click the image for larger resolution)

Visual Basic UDPClient Server/Client model?

So I'm trying to make a very simple system to send messages from a client to a server (and later on from server to client as well, but baby steps first). I'm not sure exactly how to use UDPClient to send and receive messages (especially to receive them), mostly because I don't have anything triggering the ReceiveMessage() function and I'm not sure what would.
Source Code is at this link, go to File>Download. It is already built if you want to just run the exe.
So my question is basically: How can I easily use UDPClient, how can I get this system to work and what are some tips for executing this kind of connection? Anything I should watch out for (threading, issues with code,etc)?
Source.
You need first need to set up two UdpClients. One client for listening and the other for sending data. (You'll also need to pick a free/unused port number and know the IP address of your target - the machine you want to send data to.)
To set up the receiver,
Instantiate your UdpClient variable with the port number you chose earlier,
Create a new thread to avoid blocking while receiving data,
Loop over the client's receive method for as long as you want to receive data (the loop's execution should be within the new thread),
When you receive one lot of data (called a "packet") you may need to convert the byte array to something more meaningful,
Create a way to exit the loop when you want to finish receiving data.
To set up the sender,
Instantiate your UdpClient variable with the port number you chose earlier (you may want to enable the ability to send broadcast packets. This allows you to send data to all listeners on your LAN),
When you need to transmit data, convert the data to a byte array and then call Send().
I'd suggest that you have a quick skim read through this.
Here's some code to get you started off...
'''''''''''''''''''''''Set up variables''''''''''''''''''''
Private Const port As Integer = 9653 'Port number to send/recieve data on
Private Const broadcastAddress 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
Private receivingClient As UdpClient 'Client for handling incoming data
Private sendingClient As UdpClient 'Client for sending data
Private receivingThread As Thread 'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
Private closing As Boolean = False 'Used to close clients if form is closing
''''''''''''''''''''Initialize listening & sending subs'''''''''''''''''
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
InitializeSender() 'Initializes startup of sender client
InitializeReceiver() 'Starts listening for incoming data
End Sub
''''''''''''''''''''Setup sender client'''''''''''''''''
Private Sub InitializeSender()
sendingClient = New UdpClient(broadcastAddress, port)
sendingClient.EnableBroadcast = True
End Sub
'''''''''''''''''''''Setup receiving client'''''''''''''
Private Sub InitializeReceiver()
receivingClient = New UdpClient(port)
Dim start As ThreadStart = New ThreadStart(AddressOf Receiver)
receivingThread = New Thread(start)
receivingThread.IsBackground = True
receivingThread.Start()
End Sub
'''''''''''''''''''Send data if send button is clicked'''''''''''''''''''
Private Sub sendBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendBut.Click
Dim toSend As String = tbSend.Text 'tbSend is a textbox, replace it with whatever you want to send as a string
Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)'Convert string to bytes
sendingClient.Send(data, data.Length) 'Send bytes
End Sub
'''''''''''''''''''''Start receiving loop'''''''''''''''''''''''
Private Sub Receiver()
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port) 'Listen for incoming data from any IP address on the specified port (I personally select 9653)
While (True) 'Setup an infinite loop
Dim data() As Byte 'Buffer for storing incoming bytes
data = receivingClient.Receive(endPoint) 'Receive incoming bytes
Dim message As String = Encoding.ASCII.GetString(data) 'Convert bytes back to string
If closing = True Then 'Exit sub if form is closing
Exit Sub
End If
End While
End Sub
'''''''''''''''''''Close clients if form closes''''''''''''''''''
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
closing = True 'Tells receiving loop to close
receivingClient.Close()
sendingClient.Close()
End Sub
Here are a few other exmples: Here, here, here and here.
Imports System.Threading
Shared client As UdpClient
Shared receivePoint As IPEndPoint
client = New UdpClient(2828) 'Port
receivePoint = New IPEndPoint(New IPAddress(0), 0)
Dim readThread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))
readThread.Start()
Public Shared Sub WaitForPackets()
While True
Dim data As Byte() = client.Receive(receivePoint)
Console.WriteLine("=" + System.Text.Encoding.ASCII.GetString(data))
End While
End Sub