Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

[VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

iProgrammer

New member
Joined
Feb 11, 2009
Messages
38
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/...t-kills-the-connection-for-other-tcpclients-a
 
Joined
May 20, 2005
Messages
104,556
@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.
 

iProgrammer

New member
Joined
Feb 11, 2009
Messages
38
@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
 

jafcarsa

New member
Joined
Jan 16, 2014
Messages
3
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...
 
Joined
May 20, 2005
Messages
104,556
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.
 

jafcarsa

New member
Joined
Jan 16, 2014
Messages
3
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?
 
Joined
May 20, 2005
Messages
104,556
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.
 

jafcarsa

New member
Joined
Jan 16, 2014
Messages
3
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...
 

Mogge

New member
Joined
May 9, 2014
Messages
12
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
 
Joined
May 20, 2005
Messages
104,556
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.
 

lazreg

New member
Joined
May 24, 2015
Messages
4
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
 

cary1234

New member
Joined
Oct 7, 2009
Messages
253
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?
 
Joined
May 20, 2005
Messages
104,556
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.
 

cary1234

New member
Joined
Oct 7, 2009
Messages
253
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.
 
Joined
May 20, 2005
Messages
104,556
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.
 

Johno2518

New member
Joined
Jul 13, 2010
Messages
8
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
 

Johno2518

New member
Joined
Jul 13, 2010
Messages
8
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
 
Joined
May 20, 2005
Messages
104,556
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.
 

PacificPilotNZL

New member
Joined
Feb 13, 2020
Messages
1
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
 
Top