Skip to content

Socket Recieve problems

David Dobson

Asked Jun 21, 2009, 4:15 AM EDT1 answer
Share
LinkedInX

Hi Eveyone

I have been trying to get some c# socket code working for a while now and I can't see what wrong with it. It's supposed to receive a varying number of bytes over a TCP socket and place them in a buffer. I seem to either get some of the bytes into the buffer before the end of message break point is hit or I end up with a index out of range error (when the buffer overflows as I haven't been able to see the end of the message:

C#
 public void rxSubroutine() {

            while (commsActive) {

                // for serial based comms
                if (commsMode == c_Mode_t.serial_comms) {

                } // if

                // For IP based comms
                else if (commsMode == c_Mode_t.ip_comms) {
                 
                    // read a character.
                    bytesRead = TCPConnection.Receive(rawRxBytes, totalBytesRead, 128, SocketFlags.None, out err);
                    totalBytesRead += bytesRead;

                    if (SocketError.Success == err) {

                        if (bytesRead > 0) {
                            rxStarted = true;
                        } // if

                    } // if

                    else if (SocketError.WouldBlock != err) {

                        if (rxStarted) {
                            rxStarted = false; //break point placed here
                            totalBytesRead = 0;
                        } // else

                        return;
                    } // else if

                } // else if

                Thread.Sleep(10);
               
            } // while
        } // rxSubroutine

I'm trying to use the thread to show when the data has stopped coming into the application, but as I say it does not seem to work.

I'm still quite new to C#, any help anyone can offer would be greatly appreciated :)

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi
    Your posted information is very minimal to debug your problem please try  to post detailed code snippet.
    but I guess that you are facing problem with buffer size try  increasing the buffer size of the tcp object
    0