none
StreamSocket如何持续等待服务器发送的数据(主动接收响应) RRS feed

  • 问题

  • UWP的StreamSocket只能发送一条数据,才能接收得到服务器发过来的数据!

    不能主动接收服务器响应的数据!

    即使我写了一个死循环从InputStream持续接收数据,但效果还是一样,只能发送一条数据才会执行死循环的代码!

    Sockets.StreamSocket socket = new StreamSocket();
    
    HostName serverHost = new HostName("localhost");
    
        string serverPort = "1337";
        await socket.ConnectAsync(serverHost, serverPort);
    
    
        Stream streamOut = socket.OutputStream.AsStreamForWrite();
        StreamWriter writer = new StreamWriter(streamOut);
        string request = "test";
        await writer.WriteLineAsync(request);
        await writer.FlushAsync();
    

    while(true)
    {
        Stream streamIn = socket.InputStream.AsStreamForRead();
        StreamReader reader = new StreamReader(streamIn);
        string response = await reader.ReadLineAsync();
    }

    2016年7月22日 15:56