3

尝试编写一段简单的代码来使用 StreamSocket 连接到 IMAP 服务器。它读取服务器连接响应,但不会读取其他任何内容。

    private async void TCPTest(string hostName, string port)
    {
        status.Text = "start of TCPTest";

        StreamSocket socket = new StreamSocket();
        await socket.ConnectAsync(new HostName(hostName), port);

        string senddata = "A001 login uuu ppp";

        status.Text += "\n first reading...";
        DataReader reader = new DataReader(socket.InputStream);
        reader.InputStreamOptions = InputStreamOptions.Partial;
        await reader.LoadAsync(1024);
        string data = reader.ReadString(reader.UnconsumedBufferLength);
        status.Text += "\n > read " + data;

        status.Text += "\n writing...";
        writer = new DataWriter(socket.OutputStream);
        writer.WriteString(senddata);
        await writer.StoreAsync();
        status.Text += "\n > wrote " + sendata;

        status.Text += "\n second reading...";
        writer = new DataWriter(socket.OutputStream);
        await reader.LoadAsync(1024);
        string data2 = reader.ReadString(reader.UnconsumedBufferLength);
        status.Text += "\n >" + data2;

        status.Text += "\n end of TCPTest";
    }

这是我得到的输出。

start of TCPTest
first reading...
> read * OK mydomain.com IMAP4rev1 MDaemon 13.0.1 ready
writing...
> wrote A001 login uuu ppp
second reading... 

该应用程序永远不会比这更进一步。没有错误,它只是挂起。我在这里做一些根本错误的事情吗?我的代码与MSDN 上的 StreamSocket 示例相同(或多或少)

4

1 回答 1

3

原来我并没有终止我的命令。添加Environment.Newline到最后解决的事情。

于 2012-10-08T14:33:59.717 回答