How do you loop a listener?

bonedoc

Well-known member
Joined
May 4, 2006
Messages
112
Programming Experience
Beginner
I have a server that listens for incoming data on port 500. After it reads, however, it stops listening. What do I need to do to make it start listening again after it processes its first listen? Right now, I just have a listener upon the form load.
 
A complex question :)

First and foremost, your platform is listed as .NET 1.0, is that accurate? If so, the answer is a bit more complex unless you are able to upgrade to 2.0.

The reason here is that you cannot simply loop the TCPListener, or you will lock your GUI thread. You will need to drop the Listener off to a sub thread and let it process there. It's not as bad as it sounds if your able to use .NET 2.0's BackgroundWorker. Even in 1.1 its not to bad, but you will have to more manually control the threading process.

Let us know 1.1, or if possible, 2.0 as your platform and we'll provide more details.
 
Sockets programming and multithreading go hand in hand, much because in sockets you are either stuck on a blocking call for periods of time or have an asynchronous call waiting to callback, both which usually continue looping for a while. Read some multithreading tutorials and make this part of your programming repertoire with the same ease as declaring a variable, it will pay off greatly when doing advanced stuff like sockets. The Begin... methods like BeginConnect and BeginRead used with callbacks is also multithreading and enables the calling thread to continue uninterupted while the working thread goes at its own pace. Doing sockets requires a lot of reading and knowledge.
 
Alright then. It gets a bit complex but, I'll keep posting answers as long as you keep asking questions ;-)

To start with, here is the MSDN article on the BackgroundWorker, give it a read, but don't get to frustrated at it, it lacks a lot in the 'first timers' helpfullness, but its a good general overview.

I'm looking now, trying to find a decent tutorial site for the BGW, but in the mean time, read of that MSDN info, and then Google the BGW and see what you find. If nothing else, I have a nice little project I used one in I can cleanse and upload as an example.

As John mentioned Socket programming nearly always requires threaded programming, so the two are a great thing to learn together, but it dose require time to understand it all :)
 
Back
Top