research Question

Multi Client/Server VB.NET App with TCPListeners

Avatar of Lee W, MVP
Lee W, MVPFlag for United States of America asked on
Visual Basic.NET
3 Comments1 Solution11 ViewsLast Modified:
I'm playing around with creating a multi-client/server service.

Basically, I would have a service running on all clients that would communicate with a server that was running the "server" service of the application.

I've been following tutorials and various posts on how to do this and while I've gotten the communication and service running between one client and one server, I feel like I'm missing something.

I'm THINKING I would have the following subs:

Sub OnStart
Sub OnStop
Sub StartTCPListener
Sub StopTCPListener
Sub AcceptClientConnection
Sub GetDataFromClientConnection
Sub TimerCheck (tests that the TCP listener is running and if not, starts it, as well as running numerous other timer based activities.)

(truth be told, I probably will have a bunch more subs and functions, but those are the main ones I expect to handle communication).

I've got OnStart, OnStop, and (I think) the TimerCheck sub figured out... but I'm having a hard time wrapping my head around the StartTCPListener and StopTCPListener subs.  It would SEEM that the variable assigned to the TCPListener must be local to the sub, in which case, I can't reference it in other subs, making the StopTCPListener sub useless.  Likewise, since it's local, the stop must be in the StartTCPListener sub, but if it is, then it's no longer listening after the sub executes.  Or it hangs everything waiting for a connection.

FYI, I believe I have to keep the TCPListener as a local, sub defined variable because I'm referencing variables that only get defined by calling subs.

Dim NetListener As New TcpListener(IPAddress.Parse(RegSettings.LocalServerIP), RegSettings.ListenPort)

Open in new window

RegSettings.LocalServerIP and RegSettings.ListenPort are both retrieved from the registry and otherwise empty at the beginning of the class module that defines the service.

What am I missing here?  Maybe I'm not understanding how TCPListener works?  Or perhaps I'm overly concerned with closing the listener properly?

Next I'm having trouble figuring out the multiple client connection issue.  I do have some code that might explain this, but I'm open to ideas/suggestions on this.  I'm planning on playing with this code: 
though alternatives, are welcome!

Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 3 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 3 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004