【发布时间】:2011-04-30 15:21:55
【问题描述】:
我需要接收来自服务器应用程序的第二个回复。当我第一次连接到服务器应用程序时,我得到了回复。但是当我尝试发送另一条消息时,我收不到它。
我已尝试寻找解决方案,但找不到任何东西。我相信问题是我的读者的指针仍然在最后。这就是为什么我无法阅读下一个回复的原因。这是我的代码:
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");
}
【问题讨论】:
-
协议?你是什么意思?您是指发送的消息吗?
-
是——发送和接收的消息的指定格式是什么?它应该是 CONNECT\nlogin...\npassword...\n\n\0 吗?另外,它是如何失败的——你是遇到错误还是没有打印出来?
标签: c# tcpclient streamreader