如果已在缓冲区中包含要在客户端处理的数据,如何阻止在TCP中发送

时间:2016-07-18 06:42:11

标签: c# sockets tcpclient tcpserver

我一直在使用sample( decimal( 5*5 ) ) toString(euros) 来发送和接收服务器到客户端的数据。

我正在使用以下代码

服务器

TCP sockets

客户端

 private string SendDataToClient(int currentStationSeq, int nextStationSeq, int SequenceOrder)
    {          
        byte[] byData = System.Text.Encoding.ASCII.GetBytes(String.Format("{0},{1},{2}", currentStationSeq, nextStationSeq, SequenceOrder));
        Socket workerSocket = null;
        for (int i = 0; i < m_workerSocketList.Count; i++)
        {
            workerSocket = (Socket)m_workerSocketList[i];
            if (workerSocket != null)
            {
                if (workerSocket.Connected)
                {
                    workerSocket.Send(byData);
                }
            }
        }           
    }

public void OnDataReceived(IAsyncResult asyn) { try { SocketPacket theSockId = (SocketPacket)asyn.AsyncState; int iRx = theSockId.thisSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; string szData = new string(chars); //Console.WriteLine(szData); string[] arrData = szData.Split(','); if (arrData.Length == 3) { // Some code } else if (arrData.Length == 2) { // Some Code } else if (arrData.Length == 1) { } WaitForData(); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { } } 不能连续发送时,它可以正常工作。但是,如果Server发送数据的速度比Server更快,则数据开始在缓冲区中累积。当Client从缓冲区读取它时,它会从缓冲区读取所有数据,从而破坏了我的逻辑。

我需要像clientServer发送数据的内容,它必须等待数据在client方处理,然后再发送任何新数据。

怎么可能?

0 个答案:

没有答案