Eric Fang
Eric Fang

Reputation: 13

C# socket read buffer error with multiple clients connecting to servers

I write an Windows C# application which can create up to 16 threads. Each thread creates a socket for a remote device. Each thread send commands to read the device status. (every 300 ms) It is OK when I create one or two threads to read the status. But when I create 10 threads to read device status, I will get wrong data in the socket receive buffer.

Please refer to the following for my socket driver code:

class SocketClient {

    private IPAddress ipAddress;
    private IPEndPoint remoteEP;
    private Socket mSocket;
    private SocketAsyncEventArgs e = new SocketAsyncEventArgs();
    private System.Timers.Timer timer_connection;
    private static byte[] response = new byte[1024];
    private Boolean waittingConnectDone;
    private Boolean boolConnected;
    public SocketClient() {
    }

    private byte[] acknowledge = null;

    public Boolean Connect(String IP, int Port) {
        Boolean bRet = true;
        ipAddress = IPAddress.Parse(IP);
        remoteEP = new IPEndPoint(ipAddress, Port);
        mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //mSocket.ReceiveTimeout = GlobalVar.ethernet_timeout;
        mSocket.ReceiveTimeout = 500;

        try {
            waittingConnectDone = false;
            e.RemoteEndPoint = remoteEP;
            e.UserToken = mSocket;
            e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);
            mSocket.ConnectAsync(e);

            if (timer_connection != null) {
                timer_connection.Dispose();
            } else {
                timer_connection = new System.Timers.Timer();
            }
            timer_connection.Interval = 2000;
            timer_connection.Elapsed += new ElapsedEventHandler(timer_connection_Tick);
            timer_connection.Start();
            while (true) {
                if (waittingConnectDone)
                    break;
                Application.DoEvents();
            }
            bRet = boolConnected;

            //sender.Connect(remoteEP);
        } catch {
            Debug.WriteLine("### Ethernet ### Connection Error!");
            bRet = false;
        }
        return bRet;
    }

    private void e_Completed(object sender, SocketAsyncEventArgs e) {
        boolConnected = true;
        waittingConnectDone = true;
    }

    private void timer_connection_Tick(object sender, EventArgs e) {
        if (!mSocket.Connected) {
            timer_connection.Stop();
            boolConnected = false;
            waittingConnectDone = true;
        }
    }

    public void Disconnect() {
        try {
            mSocket.Shutdown(SocketShutdown.Both);
            mSocket.Close();
        } catch {
        }
    }

Each Thread use following code to read the device status:

    private byte[] acknowledge = null;
    private static byte[] response = new byte[1024];

    public byte[] sendCommand(byte[] Cmp_TxData) {
        try {
            bytesSent = mSocket.Send(Cmp_TxData);
            bytesRec = mSocket.Receive(response);

            acknowledge = new byte[bytesRec];
            Array.Copy(response, 0, acknowledge, 0, bytesRec);
        }
        catch
        {
            acknowledge = null;
        }

        return acknowledge;
    }

And the buffer data error is something like following:

TX --> 00-03-01-F4-00-03-44-14

RX <-- 00-00-00-00-00-00-00-00-00-00-00

Sometime I read correct data, but sometimes the data are all 0!

Is there anything wrong with my socket driver?

Really appreciate your help!!

Upvotes: 0

Views: 746

Answers (1)

Alex
Alex

Reputation: 13224

I have not checked all of the posted code (I suspect there may be more errors, multithreading is hard if you try to do it all yourself), but this line:

private static byte[] response = new byte[1024];

Is definitely a source for the types of errors you report. This line declares a buffer that is shared between all threads.

So if multiple threads do this:

bytesRec = mSocket.Receive(response);

The data in this response buffer can be concurrently modified.

To resolve this specific error, you could ensure that you create a static buffer for each thread using [ThreadStatic] or ThreadLocal (see for example http://reedcopsey.com/2009/11/12/thread-specific-data-becomes-easier-in-net-4-0-via-threadlocalt/).

Upvotes: 2

Related Questions