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

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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    8,598

    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

    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

  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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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.

  22. #262
    New Member
    Join Date
    Jun 2009
    Location
    Niagara Falls
    Posts
    14

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

    Hello,

    I have been using the library for a while and it's been working great.

    I have one problem; sometimes when the server is working on a virtual environment (VMWare) everything works well except when the client is closed, the server still sees the client as connected.

    I was wondering if I am missing something or there is a solution for that?

    Thanks!

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

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

    Quote Originally Posted by Erden View Post
    I have one problem; sometimes when the server is working on a virtual environment (VMWare) everything works well except when the client is closed, the server still sees the client as connected.
    Is client being closed explicitly? Or is it being closed implicitly? Implicit would be something like shutting down the computer without closing the client, disconnecting the ethernet cable etc....
    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

  24. #264
    New Member
    Join Date
    Jun 2009
    Location
    Niagara Falls
    Posts
    14

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

    It is closed explicitly.

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

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

    Ok. Well in general when writing TCP/IP applications and a connection is closed, ideally you want the endpoint that is closing to send a message to the other endpoint telling it this. Relying solely on the TCP/IP network stack to manage this is a bad idea. In order to make TCP/IP connections reliable, they had to make it extremely forgiving when it comes to closed connections to support cases where the connection is intermittent. TCP/IP by design tries as much as possible to avoid assuming that a connection is closed when conditions are unclear.

    My recommendation would to implement a message in your protocol that tells the server when a connection is being closed. This is the proper practice.
    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

  26. #266
    New Member
    Join Date
    Jun 2009
    Location
    Niagara Falls
    Posts
    14

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

    Quote Originally Posted by Niya View Post
    My recommendation would to implement a message in your protocol that tells the server when a connection is being closed. This is the proper practice.
    Thanks a lot Niya, for the explanation and recommendation. I'll implement a connection closing protocol.

    All the best.

  27. #267
    New Member
    Join Date
    Oct 2022
    Location
    West Sussex
    Posts
    15

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

    Hello, I am quite new to vb.net programming and this has caught my interest - I have plans for an application to grab a portion of the screen and send it to remote clients. Could the code in the opening post be adapted to send images rather than text strings?

    Regards,
    Malcolm

  28. #268

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

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

    Quote Originally Posted by MHutcheon View Post
    Could the code in the opening post be adapted to send images rather than text strings?
    Absolutely, I haven't looked at the internal workings of that Send method in a long time but it's Bytes that actually get sent so you can send anything that you can convert to Bytes, so anything at all. You could add an overload of Send that accepted an Image rather than a String and then converted that Image to Bytes and sent those in basically the same way. I have another CodeBank thread about saving Images to a database and that shows how to convert an Image to a Byte array and back again.

    Note that, if you intend to send different types of data, you'll need to precede that data with a code to indicate what type of data it is, so the receiver knows how to convert it back again.

  29. #269
    New Member
    Join Date
    Oct 2022
    Location
    West Sussex
    Posts
    15

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

    Quote Originally Posted by jmcilhinney View Post
    Absolutely, I haven't looked at the internal workings of that Send method in a long time but it's Bytes that actually get sent so you can send anything that you can convert to Bytes, so anything at all. You could add an overload of Send that accepted an Image rather than a String and then converted that Image to Bytes and sent those in basically the same way. I have another CodeBank thread about saving Images to a database and that shows how to convert an Image to a Byte array and back again.

    Note that, if you intend to send different types of data, you'll need to precede that data with a code to indicate what type of data it is, so the receiver knows how to convert it back again.
    Hi there!

    Thanks for your encouraging reply, and indeed for still being around some 13 years after the original post!

    I think I am good with converting the image to a byte array, but I will search for your thread to see if I can improve mine based on it.

    I am not asking you to do this for me, I won't learn anything if you do , but as I am still new, am I correct in assuming, in MessageServer.vb, with regards to Sending I am looking at adapting:

    Code:
        Public Sub Send(ByVal hostName As String, ByVal port As Integer, ByVal message As String)
            Dim client = (From c In Me.clients.Keys _
                          Let h = Me.clients(c) _
                          Where h.HostName = hostName AndAlso h.Port = port _
                          Select c).First()
    
            Me.Send(client, message)
        End Sub
    ...
        Public Sub Send(ByVal host As HostInfo, ByVal message As String)
            Dim client = (From c In Me.clients.Keys _
                          Where Me.clients(c).Equals(host) _
                          Select c).First()
    
            Me.Send(client, message)
        End Sub
    ...
        Public Sub Send(ByVal message As String)
            For Each client In Me.clients.Keys
                Me.Send(client, message)
            Next
        End Sub
    and from MessageClient.vb:

    Code:
        Public Sub Send(ByVal message As String)
            'Convert the text message to binary data.
            Dim buffer As Byte() = Me.Encoding.GetBytes(message)
    
            'Write the data asynchronously.
            Me.stream.BeginWrite(buffer, 0, buffer.Length, AddressOf Write, Nothing)
        End Sub

  30. #270

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

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

    @MHutcheon, this is the important part:
    vb.net Code:
    1. 'Convert the text message to binary data.
    2. Dim buffer As Byte() = Me.Encoding.GetBytes(message)
    That is converting the String to a Byte array. You need to accept an Image and convert that to a Byte array. The thread I was referring to that demonstrates that is here:

    https://www.vbforums.com/showthread....a-in-Databases

    Note that, as an Image may be fairly big, you might want to write it in chunks.

  31. #271
    New Member
    Join Date
    Oct 2022
    Location
    West Sussex
    Posts
    15

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

    Quote Originally Posted by jmcilhinney View Post
    @MHutcheon, this is the important part:
    vb.net Code:
    1. 'Convert the text message to binary data.
    2. Dim buffer As Byte() = Me.Encoding.GetBytes(message)
    That is converting the String to a Byte array. You need to accept an Image and convert that to a Byte array. The thread I was referring to that demonstrates that is here:

    https://www.vbforums.com/showthread....a-in-Databases

    Note that, as an Image may be fairly big, you might want to write it in chunks.
    Hi,

    My code is stopping at this point, saying:
    'Public Overrides Function GetBytes(s As String) As Byte()':
    Argument matching parameter 's' cannot convert from 'Byte()' to 'String'.
    and when I hover over message it is confirming for me message is a byte array - byte() - and has a length of 36167 bytes. If I read it correctly, the code is trying to convert my array back to a string? This is well beyond my level

    PS - apologies if I am straying off course here...

  32. #272

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

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

    Quote Originally Posted by MHutcheon View Post
    when I hover over message it is confirming for me message is a byte array - byte() - and has a length of 36167 bytes. If I read it correctly, the code is trying to convert my array back to a string? This is well beyond my level
    The point of calling Encoding.GetBytes is to convert a String to a Byte array. You're not using Strings so you don't use that method at all, especially not to pass a Byte array in. As you can see from then existing code, you get the Byte array from the original data and then you write it to the stream. Your original data is an Image. Get a Byte array from that and then write it to the stream.

  33. #273
    New Member
    Join Date
    Oct 2022
    Location
    West Sussex
    Posts
    15

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

    Quote Originally Posted by jmcilhinney View Post
    The point of calling Encoding.GetBytes is to convert a String to a Byte array. You're not using Strings so you don't use that method at all, especially not to pass a Byte array in. As you can see from then existing code, you get the Byte array from the original data and then you write it to the stream. Your original data is an Image. Get a Byte array from that and then write it to the stream.
    I thought that was what I was trying to. Here is my code to generate the byte array:
    Code:
    picScreenCapture.Image = CropImage
    Dim ms As New MemoryStream
    CropImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
    
    Dim arrImage() As Byte
    arrImage = ms.GetBuffer
    
    For Each recipient In arListOfRecipients
        Dim arElements As String() = recipient.ToString.Split(New String() {":"}, StringSplitOptions.None)
        Dim tmpIP As String = arElements(0)
        Dim tmpPort As Integer = arElements(1)
                    
       IMGserver.Send(tmpIP, tmpPort, arrImage)
    Next
    but using this generates the error in post 271.

    I don't really understand how get the code to use the different instances of MessageServer.

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