StreamSocket在地铁中读取两次

时间:2012-08-09 06:57:59

标签: c# microsoft-metro datareader

我正在连接邮件服务器。但我只是得到了TCP连接成功的响应。我以为发送数据已经完成了。如何得到答复?

这是我的代码:

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

1 个答案:

答案 0 :(得分: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;