C#TCP / IP客户端接收0个字节

时间:2018-01-09 15:32:21

标签: c# sockets tcp server tcp-ip

**大家好,**

我正在开发游戏服务器。客户端连接到服务器并且用户单击登录按钮后,它应该将数据包发送到服务器。 在发送数据包后,它开始接收0个字节...我很无能为力。

  public void SendDataToServer(byte[] data)
    {
        ByteBuffer buffer = new ByteBuffer();
        buffer.WriteBytes(data);
        m_Network.MyStream.Write(buffer.ToArray(), 0, buffer.ToArray().Length);
        buffer = null;
    }


    private void OnReceive(IAsyncResult result)
    {
        if (PlayerSocket != null)
        {
            if (PlayerSocket == null)
                return;

            int byteArray = MyStream.EndRead(result);
            m_CurrentBytes = null;
            Array.Resize(ref m_CurrentBytes, byteArray);
            Buffer.BlockCopy(m_ASyncBuffer, 0, m_CurrentBytes, 0, byteArray);

            if (byteArray == 0)
            {
                Debug.Log("You got disconnected from the Server.");
                PlayerSocket.Close();
                return;
            }

            m_ShouldHandleData = true;

            if (PlayerSocket == null)
                return;
            MyStream.BeginRead(m_ASyncBuffer, 0, 8192, OnReceive, null);
        }
    }

服务器确实收到了数据包。这是我当前的数据包

    public void SendLoginPacket()
{
    int ID = int.Parse(m_InputField.text);

    ByteBuffer byteBuffer = new ByteBuffer();
    byteBuffer.WriteInteger(1200); //Test login packet ID
    byteBuffer.WriteFloat(12); //Test if it receives
    m_Network.m_ClientSendData.SendDataToServer(byteBuffer.ToArray());
}

0 个答案:

没有答案