Multiple Sockets

kurt69

Well-known member
Joined
Jan 17, 2006
Messages
78
Location
Australia
Programming Experience
Beginner
Hey howsit going?

Umm I'm trying to write some networking code, my application will have up to 8 concurrent connections, so it needs 8 sockets. I would rather not have to declare and write code for 8 sockets so I figured I'd try using some arrays, anyway here is what I've got. I don't have the chance to test it, so I would like to know if this would work before I do.

VB.NET:
    Public Sub lobbyHost()
        Dim h As Integer
        Dim tcpLstnr(8)
        For h = 0 To UBound(tcpLstnr)
            tcpLstnr(h) = New Sockets.TcpListener(IPAddress.Parse("127.0.0.1"), (11001 + h))
            Do While hosted = True
                tcpLstnr(h).Start()
                If tcpLstnr(h).Pending = True Then
                    Try
                        Dim tcpCntrl As TcpClient = tcpLstnr(h).AcceptTcpClient()
                        tcpCntrl.NoDelay = True
                        tcpCntrl.LingerState.Enabled = False
                        Dim tcpStrm As NetworkStream = tcpCntrl.GetStream
                        Dim bytes(1024) As [Byte]
                        Dim rcvData As [String]
                        rcvData = Nothing
                        Dim i As Int32
                        While (i <> 0)
                            rcvData = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                            i = tcpStrm.Read(bytes, 0, bytes.Length)
                        End While
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                    End Try
                End If
            Loop
        Next
    End Sub
 
You do what you need to make what you want to have done work.. ;) I can't advice if you should make your variables private or public or how you want to design your classes and your application. I really don't understand what you are asking in that last post. Hope the example help you get just a little insight on how to make and manage connections with a server+multiple clients architecture. Sockets programming is really an advanced topic even for the programmer friendly TcpListener/TcpClient when it comes to making everything flow with streams and threads, this is something you should not underestimate if private/public variables and classes and events is an issue for you.
 
Ok. I see

I guess I can keep it like it is and instead put up a method to the >LobbyHost class, passing the name of the connection and what kind of message I want to send. Just one more question regarding your class compared to the MSDN version:

In the MSDN they declare the methods as SHARED. Is this only to save space? I see in the help that this makes each instance use the same code instead of having one for each instance. Is this of any importance at all? In fact, I was in the impression that the VB compiler would do this anyway. At least - thats what other compilers I have used does.

And thanx alot for any help. This was kind of what i neaded to finnish up my application

Regards
Ole
 
For JohnH

Dear JohnH,

I am newbie in VB 2005. I examined your first example, and I noticed that the server doesn't have the capability to detect client connection/ disconnection like on your second example.

How do I supposed to detect a client connect/ disconnect in your first example?

Thanks.
 
Last edited:
Not sure exactly what you are referring to, you normally detect network problems with Try-Catch, you can get various WebException in different cases, but they all lead to you not being able to complete the current transmission.
As for "clean" disconnects it could be part of your own defined protocol to make the client send a "disconnect" message to server, if it's not implied by connect-disconnect architecture.
 
Hello JohnH,

What I was trying to refer is, on your second example, we could see in server's list box says something like:

"127.0.0.1/xxxx connected" and
"127.0.0.1/xxxx disconnected"

When in your first example, there isn't anything like those. (I think yes, a clean disconnect detection)

Would you please show us how to do that kind of implementation to your first example?

Thanks
 
Last edited:
first example is a single connection at a time lobbyhost class (just for a beginner to see how to connect and send text really), so you can add the connect/disconnect info before and after the text reading loop.
 
Back
Top