Avatar of Lee W, MVP
Lee W, MVP
Flag for United States of America

asked on 

Multi Client/Server VB.NET App with TCPListeners

I'm playing around with creating a multi-client/server vb.net 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!

Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon