我需要从服务器应用程序接收第二个答复。当我第一次连接到服务器应用程序时,我收到了答复。但是当我尝试发送另一条消息时,我无法收到它。使用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