EndSend without matching BeginSend

Hi,

I don’t have too much experience with networking, but I’ve managed to implement a working solution using the Unity Transport guides found here: Workflow: Creating a minimal client and server | Unity Transport | 0.3.1-preview.4

However, I’m getting a weird issue, where on some devices only a System.InvalidOperationException is thrown with the message: EndSend without matching BegindSend, as soon as a device joins the game. The error is coming from Unity.Networking.Transport.NetworkDriver.EndSend.
My code causing the error is here:

while (_clientInputs.Count > 0)
{
    byte[] inputBytes = _clientInputs.Dequeue().PackToBytes();

    NativeArray<byte> bytes = new NativeArray<byte>(inputBytes, Allocator.Temp);
    m_Driver.BeginSend(m_Connection, out DataStreamWriter writer);
    writer.WriteBytes(bytes);
    m_Driver.EndSend(writer);
}

This code is from the Update loop. Is the issue that I’m using a while loop here? (not sure if I’m not allowed to send more than once per Update). Or am I using the NativeArray wrong? Or is it something else entirely?
An example of this error occurring is when I host on my PC and try to join with my Android device, then the Android gets this error. If I host on my Android and join with my PC there is no issue. I’ve also gotten the error with iOS devices trying to join each other, but most devices seem to have no problem.

I’ll be happy to provide more information.

You need to check the return value from BeginSend. The send queue is limited and when it is full BeginSend will fail. When it does you cannot call EndSend and you should not try to write to the DataStream.
The send queue size does not depend on the platform, but how long the OS needs the keep the data you send before making it available to the send queue again does depend on the platform so you can get different behavior based on that.

1 Like