Connecting to IMAP via StreamSocket in WinRT / C#

感情迁移 提交于 2019-12-10 17:46:33

问题


Trying to write a simple bit of code to connect to an IMAP server using StreamSocket. It reads the server connect response, but then it won't read anything else.

    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";
    }

This is the output I get.

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

The app never gets any further than this. There's no error, it just hangs. Am I doing something fundamentally wrong here? My code is the same (more or less) as the StreamSocket example on MSDN


回答1:


Turns out I wasn't terminating my command. Adding Environment.Newline to the end solved things.



来源:https://stackoverflow.com/questions/12783337/connecting-to-imap-via-streamsocket-in-winrt-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!