2012-08-09 50 views
0

我正在連接一個郵件服務器。但我只是得到了TCP連接成功的響應。我認爲發送數據已完成。如何獲得迴應?StreamSocket在城域讀取兩次

這裏是我的代碼:

private async void Connect(string hostName,string port) 
{ 
    StreamSocket ImapSer = new StreamSocket(); 
    await ImapSer.ConnectAsync(new HostName(hostName),port); 

    string str="A001 login xxx ***"; 
    // Byte[] flag = new Byte[1024]; 
    // flag=System.Text.Encoding.Unicode.GetBytes(str); 

    DataReader sr = new DataReader(ImapSer.InputStream); 

    sr.InputStreamOptions = InputStreamOptions.Partial; 

    await sr.LoadAsync(1024); 
    string data = sr.ReadString(sr.UnconsumedBufferLength); 
    txtblk1.Text = data; 

    DataWriter writer = new DataWriter(ImapSer.OutputStream); 
    writer.WriteString(str); 
    await writer.StoreAsync(); 

    // writer.WriteString(str); 
    // await writer.StoreAsync(); 

    DataReader sr2 = new DataReader(ImapSer.InputStream); 
    sr2.InputStreamOptions = InputStreamOptions.Partial; 

    await sr2.LoadAsync(1024); 
    string data2 = sr2.ReadString(sr2.UnconsumedBufferLength); 
    txtblk2.Text = data2; 
} 

回答

0

只能使用一個DataReader

DataReader sr = new DataReader(ImapSer.InputStream); 
sr.InputStreamOptions = InputStreamOptions.Partial; 

await sr.LoadAsync(1024); 
string data = sr.ReadString(sr.UnconsumedBufferLength); 
txtblk1.Text = data; 

DataWriter writer = new DataWriter(ImapSer.OutputStream); 
writer.WriteString(str); 
await writer.StoreAsync(); 

await sr.LoadAsync(1024); 
string data2 = sr.ReadString(sr.UnconsumedBufferLength); 
txtblk2.Text = data2; 
+0

我不知道這是正確的。 ReadString需要codeUnitCount(字符串的長度),它不總是與字符串長度相同 – Wouter 2016-12-24 23:18:22