I am just starting to move to VB.NET from VB6, and I need to develop a simple program to listen to UDP packets on a user-supplied port and then display the information. In VB6, I would simply use the Winsock control and then wait for the receive event. Upon receiving the event, I would get the information and display it. However, in my VB.NET research, I have found no class that provides an event when data has been received on the network....
The UDPClient class has a Receive method, but this method is a blocking method and stops execution at that line of code until data arrives at the port. I cannot use the method in this way. Is there a socket class that has an event or do I need to write my own class and use threads? This is a common problem facing most developers that migrate from VB6 to .NET. The .NET BCL (Base Class Libraries) are just thing wrappers around the Winsock APIs. These classes do not have the events that you are used to, but there are easy work-arounds to solve this dilema.
The most common method used to solve this problem is to create a background thread that tries to receive data and then raise an event when some data has been received. If the socket is a blocking socket, the background thread can be blocked without causing problems. If you want to interrupt the socket from a blocking call such as receive, you can either suspend/abort the thread, or shutdown/close the socket.
Dig deeper on Win Development Resources
Related Q&A from Mark Belles
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.