我需要從服務器應用程序接收第二個答覆。當我第一次連接到服務器應用程序時,我收到了答覆。但是當我嘗試發送另一條消息時,我無法收到它。使用TcpClient和StreamReader不能接收多條消息
我試過尋找解決方案,但找不到任何東西。我相信問題是我的讀者指針仍然是最後的。這就是爲什麼我無法閱讀下一個答覆。這裏是我的代碼:
public static void XConn()
{
TcpClient client = new TcpClient();
client.Connect("xxx.xxx.xxx.xxx",xx); // i cant show the IP sorry
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
String r = "";
sw.AutoFlush = true;
sw.WriteLine("CONNECT\nlogin:xxxxxxx \npasscode:xxxxxxx \n\n" + Convert.ToChar(0)); // cant show also
while(sr.Peek() >= 0)
{
r = sr.ReadLine();
Console.WriteLine(r);
Debug.WriteLine(r);
if (r.ToString() == "")
break;
}
sr.DiscardBufferedData();
//get data needed, sendMsg is a string containing the message i want to send
GetmsgType("1");
sw.WriteLine(sendMsg);
// here i try to receive again the 2nd message but it fails =(
while(sr.Peek() >= 0)
{
r = sr.ReadLine();
Console.WriteLine(r);
Debug.WriteLine(r);
if (r.ToString() == "")
break;
}
s.Close();
Console.WriteLine("ok");
}
哪種協議是這樣嗎? – 2011-04-30 06:16:00
協議?你什麼意思?你的意思是發送的消息? – 2011-04-30 06:25:26
是 - 發送和接收的消息的指定格式是什麼?它應該是CONNECT \ n登錄... \ n密碼... \ n \ n \ 0?此外,它是如何失敗 - 你是否得到一個錯誤或沒有被打印出來? – 2011-04-30 06:30:13