在WinRT / C中通过StreamSocket连接到IMAP#

时间:2012-10-08 14:02:57

标签: c# sockets windows-8 windows-runtime

尝试使用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... 

该应用程序永远不会比这更进一步。没有错误,它只是挂起。我在这里做了一些根本错误的事吗?我的代码与StreamSocket example on MSDN

相同(或多或少)

1 个答案:

答案 0 :(得分:3)

原来我没有终止我的命令。将Environment.Newline添加到最终解决了问题。