Socket send-receive error

Hi.
I have following code. On the other site is simple repeater (everithing received will be send back). After several iterations (~70), sending will stop working (no more data on repeater side). In the received data are repeating previously received data. It look like buffer owerfllow. I am doing something wrong?

Thanks for reply

        var buffer = new byte[20];
        var recBuff = new byte[20];
        byte i = 0;

        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        var remoteip = new byte[] { 192, 168, 1, 100 };
        var ipaddr = new IPAddress(remoteip);
        var ept = new IPEndPoint(ipaddr, 502);

        socket.Connect(ept);

        while (true)
        {
            buffer[1] = i;
            var send = socket.Send(buffer);
            var rec = socket.Receive(recBuff);
            i++;
            Debug.Print(i + ": " + send + "-" + rec + "  " + buffer[1] + "=" + recBuff[1]);
            Thread.Sleep(200);
        }

I found something. When i modify code like bellow, it works fine. It probably reset send and receive buffer and fix buffer overflow error. But it is just workaround.

    while (true)
    {
        buffer[1] = i;
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 50);
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 50);
        var send = socket.Send(buffer);
        var rec = socket.Receive(recBuff);
        i++;
        Debug.Print(i + ": " + send + "-" + rec + "  " + buffer[1] + "=" + recBuff[1]);
        Thread.Sleep(200);
    }

In the documentation https://msdn.microsoft.com/en-us/library/cc544872.aspx it states “Receives all of the data currently available from a bound socket and places the data in a buffer array.” It does not say that data will be truncated like some of the other overloaded methods of Receive. Check out https://msdn.microsoft.com/en-us/library/cc544866.aspx, this overload might suit your needs.

It does not matter witch Receive method overload is used. All methods calls internally the same receive method (check NETMF System assembly with decompiler). Error is not in my code, error is in socket implementation. Socket internal send and receive buffers are not cleared.
After calling Receive method, I try to check Available. It return zero, as expected. But after multiple send-receive on the same socket, Availabe return 1400 (default socket receive buffer length) witch is absolutely wrong, And from now, Receive method start producing old data from receive buffer. I thing, socket internal tracking of position in send and receive buffer is corrupted.

@David_Skula1 can you file an issue here, and upload your repro?

I had the same problem on my N3 Ethernet after sending about 1400 bytes. If it can be to any help in fixing this issue I changed to UDP and that worked fine.