1

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

尽管我的消息实际上并没有发送得那么快,而且也不可靠。由于部分字符串丢失,有时发送只是停止并且线程不继续(不知道为什么).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) 线程。

4

1 回答 1

0

因为您正在尝试逐位重构字符串(如果您使用更常见的多字节编码,例如 UTF8,这种方法会中断),因此您的方法很脆弱。

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

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

只需将所有读取内容倒入 a 中MemoryStream,直到达到指定的消息长度,然后MemoryStream使用您认为合适的任何编码解码 a 的内容。

于 2013-01-24T10:26:29.360 回答