Page 7 of 7 FirstFirst ... 4567
Results 241 to 261 of 261

Thread: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

  1. #241
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    I am sorry if I am bumping an old thread, but I have come across a serious problem and no luck with fixing it.

    I wanted the ability to force close few clients upon particular conditions (like chat spam). I have added the following code to the MessageServer class

    Code:
        Public Sub Close(ByVal host As HostInfo)
            Dim client = (From c In Me.clients.Keys _
                          Where Me.clients(c).Equals(host) _
                          Select c).First()
    
            Me.RemoveClient(client)
        End Sub
    And in the RemoveClient method, I have added

    Code:
       client.GetStream().Close()
                client.Close()
    This works perfectly fine to disconnect a particular client when the HostInfo is passed to the Close method. However, there is a problem. Whenever I try to close a connection, all the other connections from the same host are also closed.
    For example, If I have three hosts connected... 192.168.1.2:2554, 192.168.1.2:2555 and 192.168.1.4:8888 and if I try to disconnect the 2nd host, the 1st host is also getting disconnected because it is from the same host, whereas the 3rd connection is working fine. This seems to be very strange. I have come across another thread at Stackoverflow mentioning the same problem, but I had no idea how to proceed.

    http://stackoverflow.com/questions/9...r-tcpclients-a

  2. #242

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    @iProgrammer, while reviving a very old thread is generally not a great idea, it's not really a problem in the case of CodeBank threads like this one. With regards to your issue, it's quite a while since I looked at this code but I may have some time in the next couple of days so I'll see if I can see a way to resolve your issue.

  3. #243
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by jmcilhinney View Post
    @iProgrammer, while reviving a very old thread is generally not a great idea, it's not really a problem in the case of CodeBank threads like this one. With regards to your issue, it's quite a while since I looked at this code but I may have some time in the next couple of days so I'll see if I can see a way to resolve your issue.
    I totally understand. Thanks for your time

  4. #244
    New Member
    Join Date
    Jan 2014
    Posts
    3

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Hi !!! I'm will try use this library, but the server is a windows service. This produce that the service dead by SynchronizationContext class. Someone could help me ? A light for resolve this...

  5. #245

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by jafcarsa View Post
    Hi !!! I'm will try use this library, but the server is a windows service. This produce that the service dead by SynchronizationContext class. Someone could help me ? A light for resolve this...
    The point of the SynchronizationContext is to be able to marshal a method call to the UI thread in order to update the UI. There's no UI in a Windows Service so there's no need to update anything specifically on the UI thread, therefore there's no need for the SynchronizationContext. It only works in Windows Forms and WPF applications because those are the only applications that need to use it. In a Windows Service you will still need to synchronise multiple threads such that they don't interfere with each other but there's no need to perform any particular operation on any particular thread.

  6. #246
    New Member
    Join Date
    Jan 2014
    Posts
    3

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by jmcilhinney View Post
    The point of the SynchronizationContext is to be able to marshal a method call to the UI thread in order to update the UI. There's no UI in a Windows Service so there's no need to update anything specifically on the UI thread, therefore there's no need for the SynchronizationContext. It only works in Windows Forms and WPF applications because those are the only applications that need to use it. In a Windows Service you will still need to synchronise multiple threads such that they don't interfere with each other but there's no need to perform any particular operation on any particular thread.
    Thanks you jmcilhinney for your answer and time. I understand this and i try explain to you. I want the server to be a service because I'm using it to send notification messages to clients (as messenger). The client has a graphical interface. The problem is that when a client connects to the server, the service drop me a NullException, as the context for the service is null. I tried to work without this class, but then the server not notify to customers. Any idea?

  7. #247

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by jafcarsa View Post
    Thanks you jmcilhinney for your answer and time. I understand this and i try explain to you. I want the server to be a service because I'm using it to send notification messages to clients (as messenger). The client has a graphical interface. The problem is that when a client connects to the server, the service drop me a NullException, as the context for the service is null. I tried to work without this class, but then the server not notify to customers. Any idea?
    You don't use the SynchronizationContext in a Windows Service because there's no UI and no UI thread. If what you did without it doesn't work then you did it wrong but there's no way for us to know what you did wrong if we don't know what you did. I suggest that you start a new thread on the topic and make reference to this thread if appropriate.

  8. #248
    New Member
    Join Date
    Jan 2014
    Posts
    3

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by jmcilhinney View Post
    You don't use the SynchronizationContext in a Windows Service because there's no UI and no UI thread. If what you did without it doesn't work then you did it wrong but there's no way for us to know what you did wrong if we don't know what you did. I suggest that you start a new thread on the topic and make reference to this thread if appropriate.
    Thanks you jcmilhinney !!! Regards...

  9. #249
    New Member
    Join Date
    May 2014
    Posts
    12

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    First of all - jmcilhinney - great job!
    This STILL rocks!

    I am sorry if I am bumping an old thread but just a few questions / surgestions to this.

    As a start - I use vb in vs.net 2013 and the code works 100% after the standard auto convert. The Demo/Test works as intended. Great.

    A few things:
    * The possibility to set host and port IN RUNTIME instead of doing it in the CODE - look #218, please. This describe very much what I'm after.
    * The possibility to send files - I can see that this was a subject in some of the post but for a newbee like me it's difficult to figure out how I must do this.
    * The possibility to open/close connections during RUNTIME - how sould I invoke this?


    Again - THANKS!!! The code is VERY well documented and really easy to understand. Excellent.

    Thx for answering.


    Best Regards
    Mogge

  10. #250

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by Mogge View Post
    First of all - jmcilhinney - great job!
    This STILL rocks!

    I am sorry if I am bumping an old thread but just a few questions / surgestions to this.

    As a start - I use vb in vs.net 2013 and the code works 100% after the standard auto convert. The Demo/Test works as intended. Great.

    A few things:
    * The possibility to set host and port IN RUNTIME instead of doing it in the CODE - look #218, please. This describe very much what I'm after.
    * The possibility to send files - I can see that this was a subject in some of the post but for a newbee like me it's difficult to figure out how I must do this.
    * The possibility to open/close connections during RUNTIME - how sould I invoke this?


    Again - THANKS!!! The code is VERY well documented and really easy to understand. Excellent.

    Thx for answering.


    Best Regards
    Mogge
    Bumping an old CodeBank thread is far less of an issue than bumping an old question thread. You just may not be guaranteed of getting a reply from the original author if they're not still around or subscribed to the thread and others may not be inclined to open a thread from the New Posts link when it's already got lots of replies. I'm still around and still subscribed to all my CodeBank threads though, so no issue here.

    The original point of this thread was to demonstrate the use of asynchronous methods. The TCP communication and chat client was just a means to an end, not an end in itself. That's why I haven't really looked at improving those aspects of the demo substantially. Let me see if I can address your questions, although I'm not actually looking at the code right now because I'm on a train on the way to work.

    1. I can't recall exactly where the host name and port number are specified but, wherever it is, you can use whatever values you want from whatever source you want. Simply change any values that I've hard-coded to variables that are populated from user input. You can do that any way that you want.
    2. The only thing that gets sent form one end to the other is Bytes. This demo creates those Bytes from a String and then back again but the Bytes can represent anything you want. If you are going to send data of different types though, you're going to have to send a header with each message so that the receiver knows how to interpret it. The rules about how to interpret communication is the protocol for your app.
    3. If I remember correctly, the client app lets you open and close child windows to open and close connections to the server, so that can already be done. If you want to use a different prompt to open and close a connection then by all means do so, but the actual opening and closing will still be done the same way.

    You need to separate the different parts of the app and realise which parts change and which stay the same. You can change a higher layer without changing what happens below it.

  11. #251
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,683

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Boo!!
    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


    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

  12. #252
    Registered User
    Join Date
    May 2015
    Posts
    4

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Thank you so much Mr jmcilhinney ,,,
    i'm a newbie
    wow it's wonderful ,, that's what i need if r u agree to edit and use it again ...
    just a little edit and u 'll be appreciated
    1 - Sending & Receiving msgs with UTF8 Encoding.
    2- About server : when receive a msg from client i need to catch it's ID (or something like this) and resend it to the same client in the same time for example :
    Client ID or Name:
    client-1: sends msg "1" to the Server and the Server resend msg "One" to the same client (client-1)
    client-2: sends msg "2" to the Server and the Server resend msg "Two" to the same client (client-2)
    and so on ....
    thank u in advance

  13. #253
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    I wanted to modify the code so it can connect to other PC within the same network.

    This is my attempt, changing this code

    Code:
    ''' <summary>
    ''' A window to represent a client that sends text messages to and receives text messages from a server.
    ''' </summary>
    Public Class ClientWindow
    
    #Region " Fields "
    
        ''' <summary>
        ''' The name of the server to connect to.
        ''' </summary>
        ''' <remarks>
        ''' In this case use the local machine.
        ''' </remarks>
        Private ReadOnly host As String = Environment.MachineName
        ''' <summary>
    To this code

    Code:
    ''' <summary>
    ''' A window to represent a client that sends text messages to and receives text messages from a server.
    ''' </summary>
    Public Class ClientWindow
    
    #Region " Fields "
    
        ''' <summary>
        ''' The name of the server to connect to.
        ''' </summary>
        ''' <remarks>
        ''' In this case use the local machine.
        ''' </remarks>
        Private ReadOnly host As String = "192.168.110.140"
    They are in the same network. My IP is 192.168.110.130 and the IP of the server listener is 192.168.110.140. Any idea why isn't this approach working?

  14. #254

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by cary1234 View Post
    I wanted to modify the code so it can connect to other PC within the same network.

    This is my attempt, changing this code

    Code:
    ''' <summary>
    ''' A window to represent a client that sends text messages to and receives text messages from a server.
    ''' </summary>
    Public Class ClientWindow
    
    #Region " Fields "
    
        ''' <summary>
        ''' The name of the server to connect to.
        ''' </summary>
        ''' <remarks>
        ''' In this case use the local machine.
        ''' </remarks>
        Private ReadOnly host As String = Environment.MachineName
        ''' <summary>
    To this code

    Code:
    ''' <summary>
    ''' A window to represent a client that sends text messages to and receives text messages from a server.
    ''' </summary>
    Public Class ClientWindow
    
    #Region " Fields "
    
        ''' <summary>
        ''' The name of the server to connect to.
        ''' </summary>
        ''' <remarks>
        ''' In this case use the local machine.
        ''' </remarks>
        Private ReadOnly host As String = "192.168.110.140"
    They are in the same network. My IP is 192.168.110.130 and the IP of the server listener is 192.168.110.140. Any idea why isn't this approach working?
    I just tested with two of my own machines and it worked fine with either a machine name or an IPv4 address.

  15. #255
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Thank you so much, yeah it's also for me now. I think the problem is in my firewall. I just turned it off and everything is working fine now.

  16. #256

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by cary1234 View Post
    Thank you so much, yeah it's also for me now. I think the problem is in my firewall. I just turned it off and everything is working fine now.
    When I first ran the project on either machine, I was prompted to allow a firewall exception to be created automatically. I would think that the whole thing would either fail on the one machine and on multiple machines or else work in both cases.

  17. #257
    New Member
    Join Date
    Jul 2010
    Posts
    8

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Hi All,

    I know it's been a long time, I have been using this library for a long time (~8 or so years). I have been tweaking it and maintaining it for my purposes. I have not made any changes to my version for ~2 years however it was definitely time to allow others to use it.

    While the code has been re-written to C# (not the purpose of this forum), I am supplying the link for anyone who may whish to download / compile the library for use with a couple additional changes / enhancements.

    https://github.com/Johno-ACSLive/ACS-Messaging

    I will eventually create a CI / CD pipeline for automated build releases and also provide a NuGet package but the source is available. I still have the VB.NET version prior to conversion (I don't believe I made any functional changes to the code). I don't plan on making this available (only because C# seems to be treated better when it comes to things like .NET Core / Standard which is where I will be moving the project to) but can if anyone is interested.

    Very big thank you to jmcilhinney for the original project, it definitely helped greatly with network communication between client/server applications I developed!!!

    Thanks

  18. #258
    New Member
    Join Date
    Jul 2010
    Posts
    8

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Hi All,

    I know it's been a long time, I have been using this library for a long time (~8 or so years). I have been tweaking it and maintaining it for my purposes. I have not made any changes to my version for ~2 years however it was definitely time to allow others to use it.

    While the code has been re-written to C# (not the purpose of this forum), I am supplying the link for anyone who may whish to download / compile the library for use with a couple additional changes / enhancements.

    https://github.com/Johno-ACSLive/ACS-Messaging

    I will eventually create a CI / CD pipeline for automated build releases and also provide a NuGet package but the source is available. I still have the VB.NET version prior to conversion (I don't believe I made any functional changes to the code). I don't plan on making this available (only because C# seems to be treated better when it comes to things like .NET Core / Standard which is where I will be moving the project to) but can if anyone is interested.

    Very big thank you to jmcilhinney for the original project, it definitely helped greatly with network communication between client/server applications I developed!!!

    Thanks

  19. #259

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by Johno2518 View Post
    Very big thank you to jmcilhinney for the original project, it definitely helped greatly with network communication between client/server applications I developed!!!

    Thanks
    I'm very pleased that my demo was put to good use.

  20. #260
    Registered User
    Join Date
    Feb 2020
    Posts
    1

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Hello,

    I love the idea of this library, but even using the test application included in the solution, the text would come through in other characters. An example of this is:

    Sending "Test" over TCP would turn into "敔瑳" in the output window.

    It's also only accepting every second connection.

    Would appreciate your help, Thanks, Will

  21. #261

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Quote Originally Posted by PacificPilotNZL View Post
    Hello,

    I love the idea of this library, but even using the test application included in the solution, the text would come through in other characters. An example of this is:

    Sending "Test" over TCP would turn into "敔瑳" in the output window.

    It's also only accepting every second connection.

    Would appreciate your help, Thanks, Will
    That sounds like an encoding issue. I haven't looked at this code for some time but I certainly don't recall that behaviour. Did you just use the attached solution as is? What version of VS are you using? I'll take a look some time soon but it's 1.30 AM here, so not right now.

Page 7 of 7 FirstFirst ... 4567

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