Socket Recieve problems

Jun 21 2009 8:15 AM

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:

 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 :)


Answers (1)