Results 1 to 40 of 273

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

Threaded View

  1. #1

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

    [VB2008/.NET 3.5] Asynchronous TcpListener & TcpClient

    Someone asked recently if there were any existing components out there that would allow them to send text messages between a client and server using the TcpClient without locking up the UI and without their having to learn the intricacies of the TcpClient class. I don't know for sure but I'm guessing that the answer is "no", so I decided to create one.

    I've attached the project below, so you're welcome to use the source code as a learning tool or as a basis for your own code. If you do use the code, in part or in full, I don't require any recognition but I do require that you don't claim the code as your own creation. You can also just compile the library and slot it straight into your own project as a black box if you don't care about how it works.

    FEATURES:

    * Uses TcpListener and TcpClient to provide communication between server and multiple clients.
    * All connection, read and write operations performed asynchronously to avoid UI issues.
    * Simple interface for send text messages, i.e. just call Send and specify the message.
    * Server can send a message to a single client or all clients with a single method call in each case.
    * Uses event model to provide notifications regarding connections and messages.
    * Events can be raised in background threads or the UI thread simply by setting a property.
    * Solution includes WinForms projects for testing server and multiple clients.
    * Code demonstrates various techniques including inheritance, custom events, asynchronous programming and LINQ.

    USAGE:

    To test out the features you can simply open the project in VS and run it. The test projects for the client and server will both run and present you with two windows: one for the server and one for the client. Click the Connect button in the client window to connect to the server.

    Both server and client provide a log window to notify you of all connection and communication events. To send a message from the client to the server simply type into the TextBox at the bottom of the window and click the Send button. The same goes for the server sending a message to the client.

    The client test rig is an MDI application. Each child window represents a client and multiple clients can connect to the server simultaneously. Use the File menu to create a new client.

    When multiple clients are connected to the server you can select the client to send a message to using the ComboBox at the bottom of the server window. Clicking the Send All button will send the message to all connected clients.

    To use the Wunnell.Net.MessageClientServer.dll assembly in your own applications you'll first want to open the References page of your project properties, add a reference to the assembly and then import the Wunnell.Net namespace. You can add instances of the client and server classes in code like so:
    vb.net Code:
    1. Private WithEvents client As New MessageClient(hostName, remotePort)
    2. Private WithEvents server As New MessageServer(port)
    In the case of the client, the host name and remote port are the name or address and port number of the server. In the case of the server, the port is the port number to listen on. The server's port and the client's remote port must be the same.

    Note that by using WithEvents in the declarations you can use the drop-down lists at the top of the code window to generate event handlers with Handles clauses. You can use AddHandler if you prefer.

    Note that you don't necessarily have to handle any events if you don't want to, but if you want to provide feedback on what's happening you will obviously need to. Note also that, if you using the library in a WinForms app and you want events raised in the UI thread then you should assign your form to the SynchronisingObject property of the client or server. All operations will still be performed on background threads. It's only notifications that will occur on the UI thread.
    vb.net Code:
    1. Me.client.SynchronisingObject = Me
    To be able to communicate you will first have to call the client's Connect method. You can then call Send to send a message, e.g.
    vb.net Code:
    1. Me.client.Connect()
    2. Me.client.Send("Hello Server")
    To send a message from the server to a specific client you simply call Send and specify the client host details and the message. To send a message to all clients you call Send and just specify the message, e.g.
    vb.net Code:
    1. For Each host In Me.server.Hosts
    2.     Me.server.Send(host, "Hello " & host.ToString())
    3. Next
    4.  
    5. Me.server.Send("Hello All Clients")
    To disconnect you simply dispose the client or the server.

    KNOWN ISSUES:

    1. If you try to close a client window in the test application without having connected the window will not close. [Fixed in v1.0.0.1 on 2009-10-11. See post #2]
    2. HostInfo.Equals throws an exception if passed a value that is not type HostInfo. [Fixed in v1.0.1.0 on 2009-11-10. See post #6]
    3. IOException may be thrown when closing client child window. [Fixed in v1.1.0.1 on 2010-10-10]

    The whole solution has been tested but more rigorous testing is still required. I'll be looking to improve the feature set and address any issues that arise so please feel free to post comments, suggestions and questions here, although I can't guarantee if and when I'll get to them.

    UPDATES:

    25 Oct 2009: v1.1.0.0 posted. See post #22 for details of changes.
    10 Oct 2010: v1.1.0.1 posted. Fixes occasional crash when closing client child window.
    Attached Files Attached Files
    Last edited by jmcilhinney; Oct 9th, 2010 at 09:53 PM. Reason: Uploaded version 1.1.0.1

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