异步TCP服务器/客户端不可靠的数据包?

时间:2013-01-24 10:17:50

标签: c# asynchronous tcp tcpclient netstream

好吧,对于我的游戏,我设置了服务器/客户端点对点连接,来回发送位置等。

虽然我的消息实际上发送速度不快,但也不可靠。由于缺少部分字符串,有时发送只是停止而线程不会继续(不确定原因).t

无论如何我的接收代码在这里:

    public void RecieveAsync()
    {

        if (netStream == null) netStream = Server.GetStream();


        if (netStream.DataAvailable == false) return;

        netStream.BeginRead(ReadBuffer, 0, ReadBuffer.Length, new AsyncCallback(recieveCallBack), netStream);




    }


    public void recieveCallBack(IAsyncResult ar)
    {
        //try
        //{

        String content = String.Empty;

        Console.WriteLine("Stuck trying to get data");

        int rec = netStream.EndRead(ar);



        if (rec > 0)
        {


                Console.WriteLine(Encoding.ASCII.GetString(
    ReadBuffer, 0, rec));

            string packet = Encoding.ASCII.GetString(
    ReadBuffer, 0, rec);
                bool completedPacket = false;
                int appendTo = rec;

                if (packet.Contains("<eof>"))
                {
                    appendTo = packet.IndexOf("<eof>");
                    packet.Replace("<eof>", "");
                    completedPacket = true;
                }

                SB.Append(packet, 0, appendTo);

                // Check for end-of-file tag. If it is not there, read   
                // more data.  

                content = SB.ToString();



            if (completedPacket)
            {
                // All the data has been read from the   
                // client. Display it on the console.  


                if (DataRecieved != null)
                {
                    string RecievedData = SB.ToString();

                    DataRecieved(RecievedData);

                    netStream.Flush();

                    Array.Clear(ReadBuffer, 0, ReadBuffer.Length);
                    ReadBuffer = new byte[1024];
                }

                SB.Clear();

                // Echo the data back to the client.  
            }
            else
            {
                // Not all data received. Get more.  
                Array.Clear(ReadBuffer, 0, ReadBuffer.Length);
                ReadBuffer = new byte[1024];
                netStream.BeginRead(ReadBuffer, 0, ReadBuffer.Length, recieveCallBack, netStream);
            }


        }

    }

我在这里发送代码:

    public void Send(byte[] data, int index, int length)
    {
        //add data as state

        //socket.NoDelay = true;

        if (netStream == null) netStream = TcpClient.GetStream();

        netStream.BeginWrite(data, 0, length, sendCallback, netStream);




    }

    private void sendCallback(IAsyncResult ar)
    {
        //try
        //{
           netStream.EndWrite(ar);

            //if (ar.AsyncState != null)
            //{
            //    byte[] buffer = (byte[])ar.AsyncState;
            //    socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, sendCallback, null);
            //    return;
            //}

            if (OnSend != null)
            {
                OnSend(this);
            }

            netStream.Flush();


        //catch (Exception ex)
        //{
        //    System.Windows.Forms.MessageBox.Show(ex.ToString());
        //    return;
        //}
    }

数据包在Encoding.ASCII.Getbytes。

服务器和客户端都在使用Thread.Sleep(1)更新while(true)线程。

1 个答案:

答案 0 :(得分:0)

因为你试图逐位重构一个字符串(如果你使用更常见的多字节编码,如UTF8,这种方法会破坏),你的方法很脆弱。

正如我的评论中所述,您可能会错过<eof>,因为它分为两次读取。

IMO,一种更好的方法是用前面的长度字段(4字节int)标记你的消息,这样你就不必在消息的末尾tip着脚尖试图弄清楚它是否已经完成。

只需将所有读物倒入MemoryStream,直至达到指示的消息长度,然后使用您认为合适的编码解码MemoryStream的内容。