using Visual Basic 2013 and simple tcp client/server to implement lan game

694 views Asked by At

All I can find for visual basic Net and up are chat apps. They don't work for me because I'm trying to make a LAN multiplayer board game and all the examples have you either typing directly on the Client and/or Server. I need to access the Client/Server via other forms. I can't seem to figure out how to accomplish this. Every time I try either I can't even send text via a public sub on the Client to tell it so send data to the server, or else the data gets to the point of being sent, but isn't. I am self-taught, but this is thrown me for a loop.

1

There are 1 answers

3
Idle_Mind On

If I make the sub in the Client a public shared sub, then it can't access anything else there.

You can have your instance of the Client class stored in a Shared variable in your main Form:

Public Class Form1

    Public Shared myClient As New Client

End Class

Now you can access it from anywhere within the application using:

Form1.myClient.Send(...)

The subs within Client do not need to be Shared.

*The Shared instance does not necessarily have to be in your form. You can create a dummy class just for this purpose:

Public Class Communication

    Public Shared myClient As New Client

End Class

Again, that can be accessed from anywhere:

Communication.myClient.Send(...)

*With VB.Net explicitly, you can instead use a Module. With this approach the instance is declared as Public without the Shared keyword:

Public Module Module1

    Public myClient As New Client

End Module

Now you can simply use myClient.Send(...) from anywhere in the application without needing to prepend it with anything. With the Module approach, .Net is actually converting everything to a Class with Shared members behind the scenes for you.