我的客户端中有这个方法,每当我单击客户端表单中的按钮时,它都会向模型发送一个整数。
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());
}
如何在接收方法的一个流中同时获取整数(go)和字符串(answer)。