Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#StreamSocket服务器到客户端通信的最大超时时间_C# - Fatal编程技术网

C#StreamSocket服务器到客户端通信的最大超时时间

C#StreamSocket服务器到客户端通信的最大超时时间,c#,C#,我从MSDN获得了以下代码。基本上,我想创建一个应用程序(客户端),它不断地等待来自服务器的命令来执行某些任务。因为服务器不能直接与客户机对话,所以客户机必须打开与服务器的连接,请求一些东西,而服务器会无限期地打开请求,直到它用命令响应客户机。reader.ReadLineAsync()将阻止执行,直到服务器响应。reader.ReadLineAsync()是否有最大超时时间?还是有更好的方法 try { //Create the StreamSocket and establish a

我从MSDN获得了以下代码。基本上,我想创建一个应用程序(客户端),它不断地等待来自服务器的命令来执行某些任务。因为服务器不能直接与客户机对话,所以客户机必须打开与服务器的连接,请求一些东西,而服务器会无限期地打开请求,直到它用命令响应客户机。reader.ReadLineAsync()将阻止执行,直到服务器响应。reader.ReadLineAsync()是否有最大超时时间?还是有更好的方法

try
{
    //Create the StreamSocket and establish a connection to the echo server.
    Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();

    //The server hostname that we will be establishing a connection to. We will be running the server and client locally,
    //so we will use localhost as the hostname.
    Windows.Networking.HostName serverHost = new Windows.Networking.HostName("localhost");

    //Every protocol typically has a standard port number. For example HTTP is typically 80, FTP is 20 and 21, etc.
    //For the echo server/client application we will use a random port 1337.
    string serverPort = "1337";
    await socket.ConnectAsync(serverHost, serverPort);

    //Write data to the echo server.
    Stream streamOut = socket.OutputStream.AsStreamForWrite();
    StreamWriter writer = new StreamWriter(streamOut);
    string request = "test";
    await writer.WriteLineAsync(request);
    await writer.FlushAsync();

    //Read data from the echo server.
    Stream streamIn = socket.InputStream.AsStreamForRead();
    StreamReader reader = new StreamReader(streamIn);
    string response = await reader.ReadLineAsync();
}
catch (Exception e)
{
//Handle exception here.            
}