Avatar of ITKnightMare
ITKnightMare
 asked on

Async Multi-threaded TCP Server/Client Packet Monitor Program (A Tough Challenge)

Hi, I am new to .NET in general, I have written this program in VB6 fine, but since it's not multithreaded it's lame, and slow.

I am trying to write it in VB.NET NEED HELP DOING THIS O_o

Please don't just post a link and say, "Figure it out a-hole!"  I think I need to revise my whole idea to achieve this in multithreading
but this would be my first VB.NET application.

Here's a drawing of what I am trying to write.

# TAKE A LOOK HERE PLEASE #
#
#         http://img171.imageshack.us/img171/6493/achievementkz7.png
#
# TAKE A LOOK HERE PLEASE #


And here is the program in VB6


'Create a WinSock object with Index 0 so it's an array and call it, "client"

'Create another WinSock object with Index 0 and call this one, "server"

'Create one more WinSock object and call is "listener"

'______________________________________________

Sub AddNewClient()

Load client(client.UBound + 1)

Load server(server.UBound + 1)

End Sub

Private Sub client_Close(Index As Integer)

server(Index).Close

client(Index).Close

End Sub

Private Sub client_DataArrival(Index As Integer, ByVal bytestotal As Long)

Dim clientdata As String


client(Index).GetData clientdata

'Monitor the packet as clientData

server(Index).SendData clientdata


End Sub


Private Sub client_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

client(Index).Close

server(Index).Close

End Sub


Private Sub Form_Load()
listener.Listen
End Sub

Private Sub Form_Unload(Cancel As Integer)
listener.Close
End Sub

Private Sub listener_ConnectionRequest(ByVal requestID As Long)

If server.Count = 1 Then
AddNewClient
End If

For i = 1 To server.UBound

If server(i).State = sckClosed And client(i).State = sckClosed Then
AcceptHost i, requestID, listener.RemoteHostIP
Exit For
End If

If i = server.UBound Then
AddNewClient
AcceptHost server.UBound, requestID, listener.RemoteHostIP
End If
Next i

End Sub

Sub AcceptHost(idx, rID, rhostip)

server(idx).RemoteHost =  SERVER IP
server(idx).RemotePort = SERVER PORT
server(idx).Connect


client(idx).Accept rID
End Sub

Private Sub server_Close(Index As Integer)

client(Index).Close
server(Index).Close

End Sub

Private Sub server_DataArrival(Index As Integer, ByVal bytestotal As Long)
Dim serverdata As String

server(Index).GetData serverdata

'Monitor the packet as serverData

client(Index).SendData serverdata

End Sub


Private Sub server_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

client(Index).Close
server(Index).Close

End Sub
Visual Basic.NET

Avatar of undefined
Last Comment
DjDezmond

8/22/2022 - Mon
DjDezmond

http://www.microsoft.com/downloads/details.aspx?familyid=08e3d5f8-033d-420b-a3b1-3074505c03f3&displaylang=en

This is an MSDN example link. Install the MSI and there's loads of vb.net examples in there. One of them is Sockets. That is the perfect multi-threaded client/server example. You'll have to encorperate them both into the same application (as the example provides a client sample and a server sample), but everything is there that you need.

I posted a question similar yesterday and got this same link. I found it very useful!

Hope this helps,

Dez
ITKnightMare

ASKER
I already got that one, it's not really helping me on learning this though.  See I got no .NET knowledge, and all MSDN has is "connecting" sockets, I don't know how to control that with a listening socket since it's not an array.  How would I assign "index" IDs to them?  And how does a listenening socket work in .NET ?

I basically need .NET equivalent of my code I posted above O_o someone told me to use TCP streams, I don't even know how to do that.  If you can take a look at my code, and the picture I posted, you'll get a more clear picture of what I need.
DjDezmond

Right, here is the code for a server application I am working on at the moment (the same one i mentioned above). The majority of the structure to this code was taken from the MSDN example i gave above.

I think if you are new to .Net, then you picked a hell of a project to start on! I been programming for years, and this is the only server application with multi-threading i have succesfully had working!

I can't help with the packet monitering, but ill post my code and try my best to explain what its doing:

This code belongs to the Main Form...

-------------------------------------------------
'Variable and object declarations:
    Private tcpListener As TcpListener
    Private ClientTCPs As New Hashtable()
    Const PORT_NUM As Integer = 1240
    Public Event NewConnection(ByVal Sender As ClientClass, ByVal ListItem As ListViewItem)
    Private listenerThread As Threading.Thread
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
DjDezmond

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
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.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question