如何读取不同的内存流?

时间:2014-10-30 21:52:06

标签: c# client-server memorystream binarywriter

我在我的客户端有这个方法,每当我点击clientform中的一个按钮时,它就会向模型发送一个整数。

   public void SetInput(int index)
    {
        String go = index.ToString();
        //String string = "YES";
        MemoryStream memory = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(memory);

        writer.Write(go);
       // writer.Write(string);
        serverSocket.Send(memory.GetBuffer());
    }

这是接收方法。 // go是第一种方法的索引。

public  bool Receive(Socket player)
    {
        byte[] buffer = new byte[4096];
        int length = player.Receive(buffer);

        MemoryStream memory = new MemoryStream(buffer, 0, length);
        BinaryReader reader = new BinaryReader(memory);

        String go = reader.ReadString();
        String answer = reader.ReadString(); //this doesnt work
        if (IsTurnValid(Convert.ToInt32(go)) == true)
        {

            PlacePiece(GetActivePlayer(), Convert.ToInt32(go));

            if (IsWon() == true)
            {
                try
                {
..... etc

}
}
}
}

我已经制作了另一个应该发送字符串的方法,虽然我得到了一个神。

public void SetAnswer(string response)
    {
        String answer = response;
        MemoryStream memory = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(memory);

        writer.Write(answer);
        serverSocket.Send(memory.GetBuffer());
    }

如何在receive方法的一个流中同时获得整数(go)和字符串(answer)。

0 个答案:

没有答案