问候, 我已经在C#中定制了Telnet客户端。我正在使用它来与IMAP服务器进行通信。它只是写命令,然后得到响应。测试代码:从调用上面的代码如何同步读取/写入流?
Telnet tc = new Telnet();
// setup server credentials
const string server = "pop.nexlink.net";
const int port = 140;
const int timeout = 70;
// establish server connection
tc.Setup(server, port, timeout);
// test initial server response
Console.WriteLine(tc.output);
// set-up a few commands to send
var array = new[] { "USER [email protected]", "PASS password", "QUIT" };
// while connected to server
if (tc.IsConnected)
{
foreach (string str in array)
{
// Show command on console
Console.WriteLine(str);
// Write to Stream
tc.WriteLine(str);
// Read from Stream
tc.Read();
// Test the Stream output
Console.Write(tc.output);
}
}
// close connection
tc.Disconnect();
输出是:
- 用户[email protected]
- + OK欢迎Atmail POP3服务器 - 登录与用户@域。
- +确定需要密码。
- PASS密码
- 退出
- + OK登录。
- + OK再见。
这是一个简单的例子,但它显示了一个竞争条件的问题。第nr.6行的输出应该出现在nr之前。 5
问:如何处理?
似乎它做得很好。服务器在完成登录用户之前会收到退出消息。 – 2010-08-17 13:09:01
这是可能的。反应是相当随机的。这取决于连接速度,服务器响应时间,返回的数据量。一旦有更多的命令请求更大的流,它会变得非常混乱。一般而言,Reads比Writes慢得多。 – asyncpro 2010-08-17 13:51:42