Results 1 to 38 of 38

Thread: Receive Paremeters on Multithreaded Server

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Receive Paremeters on Multithreaded Server

    I have created a multi-threaded server but can not figure out how to send an object/string. I think I have the ability to receive it already.

    Server created:
    vb Code:
    1. serverThread = New Threading.Thread(AddressOf startHosting)

    How do I specify an object or string parameter here?
    vb Code:
    1. connection.Connect(ip, port)

    When new users connect my thread accepts an object parameter:
    vb Code:
    1. Private Sub clientWorker(ByVal param As Object)
    2. End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Receive Paremeters on Multithreaded Server

    How do I specify an object or string parameter here?
    You don't. When you connect, all you do is connect. Once you're connected, then you can send data.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Ok, I had a feeling that was the case, so I re-wrote things to work around that.

    My issue I have now is, how do I know what thread index/ip address data is arriving on?

    This is my data arrival evenet
    vb Code:
    1. myNet.DataArrival

    And this is the function that handles the data (yes, I know it is in C#)
    C# Code:
    1. private void winsock_DataArrival(string message)
    2. {
    3. }
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    I don't remember Sockets in VB.Net having a DataArrival event. They use Receive or BeginReceive to get data. Where did that event come from ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    It gets passed into the function from this event. Should this have more or different parameters?
    vb Code:
    1. Public Event DataArrival(ByVal message As String)
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Quote Originally Posted by rex64 View Post
    It gets passed into the function from this event. Should this have more or different parameters?
    vb Code:
    1. Public Event DataArrival(ByVal message As String)
    What class is that event declared in ? The Socket class in the .Net framework has no such event.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Here is the stream watcher:
    vb Code:
    1. ''' <summary>
    2.     ''' Watches a stream while a user is connected? Stays alive while user is connected?
    3.     ''' </summary>
    4.     Private Sub streamWatcher(ByVal objStream As Object)
    5.         Try
    6.             Dim curStream As NetworkStream = CType(objStream, NetworkStream)
    7.             Do Until Disposing = True Or curStream Is Nothing
    8.  
    9.                 If curStream.DataAvailable Then
    10.                     Dim message As String = receiveText(curStream)
    11.                     RaiseEvent(DataArrival(message))
    12.                 End If
    13.                 Threading.Thread.Sleep(10)
    14.  
    15.             Loop
    16.         Catch
    17.         End Try
    18.     End Sub

    And he is called from here:
    vb Code:
    1. Dim t As New Threading.Thread(AddressOf streamWatcher)
    2. t.Start(client.GetStream)
    3. client = Nothing
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    You're using the TcpClient class....its documented in the MSDN. Read here. It will show you how to send data.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    I looked at that, but we are doing multi-threaded and I am not sure how to check which client is communicating with the server. Sorry, I am a little new to multi-threaded networking.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    You didn't define that DataArrival event properly....well at least according to the standard practice. Notice that all events in the framework have two parameters, a "sender As Object" parameter and and one based on an EventArgs object. This pattern ensures you can always get a reference to the object that raised the event through the "sender" parameter.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Ok. Can I raise the event like this:
    vb Code:
    1. RaiseEvent DataArrival(Me, message)
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    How can I keep track of which client index this belongs with? I have a ClientInfo list that I add clients to when they connect.

    vb Code:
    1. Public Structure ClientInfo
    2.         Dim ip As System.Net.IPEndPoint
    3.         Dim username As String
    4.         Dim stream As NetworkStream
    5.         Dim status As connectionStatusEnum
    6.         Dim lastPing As DateTime
    7.     End Structure
    8.  
    9.  Public clientList As New List(Of ClientInfo)
    10.  
    11. ''' <summary>
    12.     ''' Watches a stream while a user is connected? Stays alive while user is connected?
    13.     ''' </summary>
    14.     Private Sub streamWatcher(ByVal objStream As Object)
    15.         Try
    16.             Dim curStream As NetworkStream = CType(objStream, NetworkStream)
    17.             Do Until Disposing = True Or curStream Is Nothing
    18.  
    19.                 If curStream.DataAvailable Then
    20.                     Dim message As String = receiveText(curStream)
    21.                     RaiseEvent DataArrival(Me, message) 'I added 'me' here<<<<<<<<<<<
    22.                 End If
    23.                 Threading.Thread.Sleep(10)
    24.  
    25.             Loop
    26.         Catch
    27.         End Try
    28.     End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    So let me get this straight, you want your streamWatcher sub to be able to identify which client receives data before raising DataArrival ? Is this correct ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    I just need to know which client or thread index the data is coming from. What do you recommend? I think I have multiple stream watchers. Maybe each one should pass in an index or a reference to the ClientInfo?
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    What is the class that contains that code you posted in post #12 ? What is its purpose ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    This is a networking class we have written to work with a 3D game we are creating.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Yes I understand but what does this class in particular do ? Could you post the code for the entire class ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    See attached

    clsNet.vb
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  19. #19
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Impressive....did you write all that ? However I must complain that this class doesn't really seem to have a single purpose. It has stuff that seems to imply its some kind of server and yet also code that implies its a client. I really think you should separate these things. If you create a class to handle clients alone then it should come naturally to determine which client received or sent a message. What you have now are basically a collection of functions and I can see now why you're having trouble. Its a bad design. Sit back and really think about how you're designing these classes.

    For example, I have a Client/Server application and every connection is represented by a kind of socket object I made. It raises events when it receives or sends data and I've no trouble determining who sent or received what because its all naturally encapsulated in a class instance.

    [EDIT]

    Hmm...looking more at your code it seems that you did indeed create this class as a client....Can you clear this up ? Does the clsNetEasy class represent a client connection or is it just a collection of network functions ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Also, take a look at this post where I have attached a project that simulates multiple devices being pinged. It should serve to adequately demonstrate how to identify which device is doing the communication at any given time and also how to multi-thread it properly.
    Last edited by Niya; Nov 16th, 2012 at 06:14 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    It looks like you are creating a different instance of the class for each thread to keep track of them. Is that what you recommend I do? It sounds like it would use more resources, but it should work better. Let me know your thoughts, and if I figured out the puzzle

    It looks like I am creating the threads on the fly, and not managing them very well. Should I have a list of threads? Or a list of classes with a single thread in each one? Or what is the best?
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  22. #22
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Quote Originally Posted by rex64 View Post
    It looks like you are creating a different instance of the class for each thread to keep track of them. Is that what you recommend I do?
    Not only do I recommend it...I strongly recommend it.

    Quote Originally Posted by rex64 View Post
    ...It sounds like it would use more resources...
    Well we are not in the DOS era anymore where you only had 640KB of memory to play with. You can go hog wild in this case. Its more than worth it.

    Quote Originally Posted by rex64 View Post
    It looks like I am creating the threads on the fly, and not managing them very well. Should I have a list of threads? Or a list of classes with a single thread in each one? Or what is the best?
    You should have a list of objects and each of these objects can manage their own threads internally. You shouldn't have to interact with these threads outside of the object. Interact with the object's methods, properties and events.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Ok, so this is what I am planning:
    clsNetClient (connects to the server)
    clsNetServer (has list of clsNetServerConnection)
    clsNetServerConnection (each connection will be its own class)

    Quote Originally Posted by Niya View Post
    You should have a list of objects and each of these objects can manage their own threads internally. You shouldn't have to interact with these threads outside of the object. Interact with the object's methods, properties and events.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Here is my plan. Let me know your thoughts.

    Name:  Network Class Digram.jpg
Views: 145
Size:  44.4 KB
    Last edited by rex64; Nov 19th, 2012 at 07:10 PM.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Should I have one total or 1 per client of these:
    vb Code:
    1. Dim server As TcpListener = Nothing

    And what about this?
    vb Code:
    1. Dim connection As TcpClient = Nothing
    Last edited by rex64; Nov 19th, 2012 at 07:49 PM.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  26. #26
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Quote Originally Posted by rex64 View Post
    Here is my plan. Let me know your thoughts.

    Name:  Network Class Digram.jpg
Views: 145
Size:  44.4 KB
    Yes this is a very good design. Its exactly what I had in mind.

    Quote Originally Posted by rex64 View Post
    Should I have one total or 1 per client of these:
    vb Code:
    1. Dim server As TcpListener = Nothing

    And what about this?
    vb Code:
    1. Dim connection As TcpClient = Nothing
    Well clients don't need to listen do they
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Should each client connection on the server have its own TcpListner?
    Dim server As TcpListener = Nothing

    So if 100 clients were connected I would have:
    QTY1: clsNetServer
    QTY100: clsNetConnection

    And on the Client's computer, each would have their own clsNetClient.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  28. #28
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Quote Originally Posted by rex64 View Post
    Should each client connection on the server have its own TcpListner?
    What is a TcpListener for ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    I think TcpListener is for watching an ip address and port. So I am thinking I should only need 1 of these. Are the clients just network streams? To disconnect a client do you just dispose of the stream?

    vb Code:
    1. ''' <summary>
    2.     ''' Starts hosting...
    3.     ''' </summary>
    4.     Public Sub hostServer(ByVal username As String, ByVal ServerString As String)
    5.         If myIpInfo.Count > 0 Then
    6.             server = New TcpListener(IPAddress.Parse(myIpInfo(0).IPAddress), serverPort) '1244
    7.  
    8.             server.Start()
    9.         End If
    10.         _ServerString = ServerString
    11.  
    12.         serverThread = New Threading.Thread(AddressOf startHosting)
    13.         serverThread.Start(username)
    14.  
    15.     End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  30. #30
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    A server's job is to listen for client connections, not just in this scenario but in any scenario where there is a server/client paradigm. In this case I'm assuming you are opting to encase a TcpListener within a larger server class. The TcpListener has an AcceptTcpClient method which returns a TcpClient object when a client attempts to make a connection. The TcpClient object represents the connection between the specific client machine and the server So just think of it, you have one server machine that can accept incoming connections from multiple client machines, each client having its own stream. If you have a client class that wraps a TcpClient object inside then you wouldn't need a TcpListener. A server listens not a client.

    If you want to close a connection then you do so through the TcpClient class which has a Close method. All your communication would be through a TcpClient object on both ends. The TcpListener is only responsible for creating TcpClient objects for communication between client and server.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  31. #31
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    You questioning gives me an instinctive feeling that you don't fully understand the Client/Server paradigm. I suggest you read this to wrap your head around it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Thanks. I do understand the client server, but I am trying to learn the best way to implement it in VB .NET. So if I understand this correctly:
    clsNetServer would have a single TcpListener. When a client connect to the server the TcpListener would automatically create a TcpClient object which I could then pass to the constructor of ClsNetConnection. Does that sound about correct? I would not need a stream because the TcpClient contains a stream. Hope you enjoyed Thanksgiving!
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  33. #33
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    Quote Originally Posted by rex64 View Post
    Thanks. I do understand the client server, but I am trying to learn the best way to implement it in VB .NET. So if I understand this correctly:
    clsNetServer would have a single TcpListener. When a client connect to the server the TcpListener would automatically create a TcpClient object which I could then pass to the constructor of ClsNetConnection. Does that sound about correct? I would not need a stream because the TcpClient contains a stream.
    Ya...you got the idea. The TcpClient however is not really created automatically, not by the TcpListener anyway. A call to AcceptTcpClient blocks until a client establishes a connection. Only then it returns a TcpClient.

    Quote Originally Posted by rex64 View Post
    Hope you enjoyed Thanksgiving!
    Well we don't celebrate thanks giving but thank you all the same
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Things are working better, but for some reason I think the stream may be getting damaged. I can send data to the server and it receives it but I am not getting any data back on the stream after I run a few commands. It goes to the sendData inside the else statement, but the client (on the same computer) does not receive the chat messages. Other things seem to arrive ok. We have been having weird problems like this for a while. It seems like if we comment certain things out that it works, but we have not figured out why. Right now I do not see any reason it should not be working at this point.

    What tests do you recommend on the stream?
    vb Code:
    1. ''' <summary>
    2.     ''' Sends text on the stream to the server.
    3.     ''' </summary>
    4.     Public Sub sendText(ByVal message As String)
    5.  
    6.         If stream Is Nothing Then
    7.             If connection IsNot Nothing Then
    8.                 If connection.Connected Then
    9.                     sendData(System.Text.Encoding.ASCII.GetBytes(message), connection.GetStream)
    10.                 End If
    11.             End If
    12.         Else
    13.             sendData(System.Text.Encoding.ASCII.GetBytes(message), stream)
    14.         End If
    15.     End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Also, it looks like this stream loop is not active all the time. Should this loop be running the entire time that it is connected? Or should this close at some point? Is this a good way to do it?


    vb Code:
    1. ''' <summary>
    2.     ''' Watches a stream while a user is connected? Stays alive while user is connected?
    3.     ''' </summary>
    4.     Private Sub streamWatcher(ByVal objStream As Object)
    5.         Try
    6.             Dim curStream As NetworkStream = CType(objStream, NetworkStream)
    7.             Do Until Disposing = True Or curStream Is Nothing  
    8.  
    9.                 If curStream.DataAvailable Then
    10.                     Dim message As String = receiveText(curStream)
    11.                     RaiseEvent DataArrival(Me, message)
    12.                 End If
    13.                 Threading.Thread.Sleep(10)
    14.  
    15.             Loop
    16.         Catch
    17.         End Try
    18.     End Sub
    Last edited by rex64; Nov 24th, 2012 at 12:10 PM.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  36. #36
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    The loop should be running the entire time on another thread. It should exit on a disconnect. This is what I do for my network applications.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  37. #37

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    Re: Receive Paremeters on Multithreaded Server

    Ok, I think I am understanding everything. The loop is getting stuck in startHosting. Disposing is = to true in the debugger, but it does not seem to get off that line of code. It has a green arrow and when I press F8 (step into) or Start it acts like it is playing (which is weird for F8 (Step Into)). Then when I press pause it is right back on that exact same line of code.

    I may be crazy, but looking at this code closer, during normal running, isn't this stuck in an infinite loop of creating threads? This sounds very bad

    It is getting stuck on this line:
    client = server.AcceptTcpClient()

    In here:
    vb Code:
    1. ''' <summary>
    2.     ''' Used by host server.
    3.     ''' </summary>
    4.     Private Sub startHosting(ByVal username As Object)
    5.         Dim client As TcpClient
    6.  
    7.         _username = CType(username, String)
    8.  
    9.         'before anyone tries changing this, it MUST BE AN OBJECT ARRAY!!!!
    10.         'it stores a TCPClient and a string, so explicit typing WON'T work. -Ray
    11.  
    12.         Do Until _close Or Disposing = True
    13.             Try
    14.                 client = server.AcceptTcpClient()
    15.  
    16.  
    17.  
    18.                 Dim myThread As New Threading.Thread(AddressOf clientWorker)
    19.                 myThread.Start(client)
    20.             Catch
    21.                 _close = True
    22.             End Try
    23.  
    24.         Loop
    25.     End Sub
    Last edited by rex64; Nov 26th, 2012 at 10:28 AM.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  38. #38
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Receive Paremeters on Multithreaded Server

    client = server.AcceptTcpClient() is a blocking call. I expect it would only proceed after receiving a connection in which case the loop would proceed and eventually reach right back there. startHosting should be running on another thread so as to prevent locking up the UI.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width