question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`WatsonTcpClient.Send()` with stream disconnects if `contentLength` is not equal to the stream's capacity.

See original GitHub issue

Given a byte[] buffer, I’d like to send a portion of it - moreover, I’d like to use my own Stream instances which may be pooled and recycled.

The WatsonTcpClient.Send (long contentLength, Stream stream) is the closest overload to my purposes as I can provide my own Stream and coincidentally my buffer’s data always starts at the zeroth index (although it’d be nice to also be able to define the starting position).

To my understanding, contentLength defines how much of my Stream should be read and sent.

However, if I choose a contentLength that is not equal to the buffer’s capacity, what happens is that the data is sent/received correctly as expected (it respects the contentLength that I chose), but the client will be disconnected afterwards.

The only instance in which the client doesn’t get disconnected is if contentLength is set equal to the buffer capacity, but then of course that’d defeat the point for my purposes.

The workaround for me so far is to copy the desired data into a new buffer of the exact size required, or create a new MemoryStream and write() to it what I need, but minimizing copies/allocations is exactly what I’m trying to do.

Below is the program I use to reproduce it. I’ve been using .NET 6.0 and tested this on Windows 10 and macOS Monterey.


using WatsonTcp;

internal class Sandbox
{

    public static void Main(string[] args)
    {
        if (args[0].ToLower().Equals("server"))
            StartServer();
        else
            StartClient();

        Console.ReadLine();
    }

    static void StartServer ()
    {
        WatsonTcpServer server = new WatsonTcpServer("127.0.0.1", 9001);

        server.Events.ClientConnected += (sender, args) =>
            Console.WriteLine("Server: a client has connected.");

        server.Events.ClientDisconnected += (sender, args) =>
            Console.WriteLine("Server: a client has disconnected: " + args.Reason);

        server.Events.MessageReceived += (sender, args) =>
            Console.WriteLine($"Server: received {args.Data.Length} bytes from client");

        server.Start();

        Console.WriteLine("Server has started.");
    }

    static void StartClient ()
    {
        WatsonTcpClient client = new WatsonTcpClient("127.0.0.1", 9001);

        client.Events.ServerConnected += (sender, args) => {
            Console.WriteLine("Client: connected to server. Will send a message...");

            //                       VVVVVVVVV Only want to send the first 5 bytes
            var buffer = new byte[] {1,2,3,4,5,6,7,8,9,10};

            using (var stream = new MemoryStream(buffer))
                client.Send(contentLength:5, stream); //    <-- will disconnect unless you use 10

        };

        client.Events.ServerDisconnected += (sender, args) =>
            Console.WriteLine("Client: disconnected from server.");

        client.Events.MessageReceived += (sender,args) =>
            Console.WriteLine($"Client: received {args.Data.Length} bytes from server.");

        Console.WriteLine("Client attempting connection...");
        client.Connect();
    }
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eatyouroatscommented, Aug 16, 2022

Works on my end. =)

0reactions
jchristncommented, Aug 16, 2022

Thanks for letting me know @eatyouroats !!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting response of http request without content-length?
In your specific case, if the server does not give content length back, then it MUST be closing the stream upon finishing the...
Read more >
TcpListener: Detecting a client disconnect as opposed to a ...
Each call between Connect and Disconnect() sends a stream of data to the server. The server knows how to analyze and process the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found