我在使用来自服务器的命令的while语句中遇到问题。因为在循环中我不能再从服务器获得消息。我还尝试读取流并尝试获取消息,但它失败了,因为您可以在我的代码中看到。有没有办法在该循环中捕获来自服务器的消息?
public void Connect()
{
clientSocket.Connect("localhost", 4200);
serverStream = clientSocket.GetStream();
byte[] outStream = Encoding.ASCII.GetBytes(User + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
Thread ctThread = new Thread(getMessage);
ctThread.Start();
}
public async void getMessage()
{
while (true)
{
serverStream = clientSocket.GetStream();
int buffsize = 0;
byte[] inStream = new byte[clientSocket.ReceiveBufferSize];
buffsize = clientSocket.ReceiveBufferSize;
serverStream.Read(inStream, 0, buffsize);
string rData = Encoding.ASCII.GetString(inStream);
readData = "" + rData;
readData = readData.Replace("\0", "");
switch (readData)
{
case ("Receive"):
break;
case ("Overview"):
Boolean lBreak = false;
while (!lBreak)
{
BinaryFormatter binFormatter = new BinaryFormatter();
serverStream = clientSocket.GetStream();
pictureBox1.Image = (Image)binFormatter.Deserialize(serverStream);
Thread ccb = new Thread(starter);
ccb.Start();
//int closer = 0;
//byte[] relay = new byte[clientSocket.ReceiveBufferSize];
// closer = clientSocket.ReceiveBufferSize;
//serverStream.Read(relay, 0, closer);
//string terminate = Encoding.ASCII.GetString(relay);
//readDataa = "" + terminate;
//readDataa = readDataa.Replace("\0", "");
//getMessage();
//switch (readDataa)
//{
// case "stopOverview":
// lBreak = true;
// serverStream.Flush();
// break;
//}
//if(lBreak == true)
//{
// serverStream.Flush();
// break;
//}
}
break;
case ("125"):
Application.Run(new Form3());
break;
case ("Return"):
break;
default:
break;
}
}
}