簡體   English   中英

回復時出現TCP錯誤

[英]TCP error on reply

我創建了一個簡單的網絡游戲,該游戲使用TCP套接字來獲取其他玩家的數據。 我有兩個不同的類,一個服務器和一個客戶端。 當我一次只發送一條消息時,一切都很好:

客戶:

public void ClientTester()
{
    thread = new Thread(SendPosition);
    thread.Start();
}

private void SendPosition()
{
    while (true)
    {
        using (TcpClient client = new TcpClient())
        {
            client.Connect("127.0.0.1", 82);
            using (NetworkStream n = client.GetStream())
            {
                BinaryWriter w = new BinaryWriter(n);
                w.Write("Position:" + GameFiles.Player.Player.position.ToString());
                w.Flush();
                string msg = new BinaryReader(n).ReadString();
                parseString(msg, "Position:", 9);
            }
        }

        Thread.Sleep(60);
    }
}

服務器:

public void ServerTester()
{
    thread = new Thread(TheadedMethod);
    thread.Start();
}

private void TheadedMethod()
{
    while (true)
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 82);
        listener.Start();
        using (TcpClient c = listener.AcceptTcpClient())
        using (NetworkStream n = c.GetStream())
        {
            parseString(msg, "Position:", 9);
            BinaryWriter w = new BinaryWriter(n);
            w.Write("Position:" + GameFiles.Player.Player.position.ToString());
            w.Flush();
        }
        listener.Stop();
    }
}

這是新代碼:

客戶:

public void ClientTester()
{
    thread = new Thread(SendPosition);
    thread.Start();

    SendMousePosition();
}

private void SendPosition()
{
    while (true)
    {
        using (TcpClient client = new TcpClient())
        {
            client.Connect("127.0.0.1", 82);
            using (NetworkStream n = client.GetStream())
            {
                BinaryWriter w = new BinaryWriter(n);
                w.Write("Position:" + GameFiles.Player.Player.position.ToString());
                w.Flush();
                string msg = new BinaryReader(n).ReadString();
                parseString(msg, "Position:", 9);
            }
        }

        Thread.Sleep(60);
    }
}

private void SendMousePosition()
{
    using (TcpClient client = new TcpClient())
    {
        client.Connect("127.0.0.1", 82);
        using (NetworkStream n = client.GetStream())
        {
            BinaryWriter w = new BinaryWriter(n);
            w.Write("MousePosition:" + cursor.mousePosition());
            w.Flush();
            string msg = new BinaryReader(n).ReadString();
            parseString(msg, "MousePosition:", 14);
        }
    }
}

服務器:

public void ServerTester()
{
    thread = new Thread(TheadedMethod);
    thread.Start();
}

private void TheadedMethod()
{
    while (true)
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 82);
        listener.Start();
        using (TcpClient c = listener.AcceptTcpClient())
        using (NetworkStream n = c.GetStream())
        {
            string msg = new BinaryReader(n).ReadString();
            if (msg == "Position:")
            {
                parseString(msg, "Position:", 9);
                BinaryWriter w = new BinaryWriter(n);
                w.Write("Position:" + GameFiles.Player.Player.position.ToString());
                w.Flush();
            }
            if (msg == "MousePosition:")
            {
                parseString(msg, "MousePosition:", 14);
                BinaryWriter w = new BinaryWriter(n);
                w.Write("MousePosition:" + cursor.mousePosition());
                w.Flush();
            }
        }
        listener.Stop();
    }
}

當我嘗試發送兩條消息時,出現錯誤消息:

Unable to read beyond the end of the stream.

從客戶端方法SendPosition()的這一行開始:

string msg = new BinaryReader(n).ReadString();

即使我創建了BinaryReader的新實例,為何也無法正常工作? 服務器不應該自動響應發送給它的每條消息嗎?

您做錯了件事:第一,您始終創建並重新創建連接。 而是一次創建一個偵聽器,進入循環並閱讀消息。 在TCP廣告中設置新連接會產生大量開銷,尤其是在您僅發送小消息的情況下。 客戶端中的相同內容,連接一次,然后在需要時發送。

第二個問題是TCP是一種協議,並且沒有消息邊界。 這意味着,當您從TCP連接中讀取內容時,您無法事先知道要讀取多少數據,您需要提供一種自行分離消息的方法:您可以添加消息邊界,可以在每個消息之前添加一個包含郵件大小,其中所有郵件都具有相同的固定大小。 無論哪種方式,您可能必須閱讀多次才能獲得完整的消息,或者一次閱讀可能會給您帶來不止一條消息。

關於第二個問題,您當然不能嘗試讀取超過已收到的內容,這可能是錯誤消息告訴您的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM