zoukankan      html  css  js  c++  java
  • TCP in Windows Runtime(运行时) GIS

    Windows.Networking.Sockets namespace

    As with the .NET implementation, there are two primary classes to handle server and client roles. In WinRT, these are StreamSocketListener and StreamSocket.

    The following method starts a server on port 51111, and waits for a client to connect,It then reads a single message comprising a length-prefixed string:

    async void Server()
    {
    var listener = new StreamSocketListener();
    listener.ConnectionReceived += async (sender, args) =>
    {
    using (StreamSocket socket = args.Socket)
    {
    var reader = new DataReader (socket.InputStream);
    await reader.LoadAsync (4);
    uint length = reader.ReadUInt32();
    await reader.LoadAsync (length);
    Debug.WriteLine (reader.ReadString (length));
    }
    listener.Dispose(); // Close listener after one message.
    };
    await listener.BindServiceNameAsync ("51111");
    }

    In this example, we used a WinRT type called DataReader (in Windows.Networking)to read from the input stream, rather than converting to a .NET Stream object and
    using a BinaryReader. DataReader is rather like BinaryReader except that it supports asynchrony. The LoadAsync method asynchronously reads a specified number of
    bytes into an internal buffer, which then allows you to call methods such as Read UInt32 or ReadString. The idea is that if you wanted to, say, read 1000 integers in a
    row, you’d first call LoadAsync with a value of 4000, and then ReadInt32 1000 times in a loop. This avoids the overhead of calling asynchronous operations in a loop (as each asynchronous operation incurs a small overhead).

    The StreamSocket object that we obtained from awaiting AcceptAsync has separate input and output streams. So, to write a message back, we’d use the socket’s OutputStream.

    We can illustrate the use of OutputStream and DataWriter with the corresponding client code:

    async void Client()
    {
    using (var socket = new StreamSocket())
    {
    await socket.ConnectAsync (new HostName ("localhost"), "51111",
    SocketProtectionLevel.PlainSocket);
    var writer = new DataWriter (socket.OutputStream);
    string message = "Hello!";
    uint length = (uint) Encoding.UTF8.GetByteCount (message);
    writer.WriteUInt32 (length);
    writer.WriteString (message);
    await writer.StoreAsync();
    }
    }

    We start by instantiating a StreamSocket directly, then call ConnectAsync with the host name and port. (You can pass either a DNS name or an IP address string into
    HostName’s constructor.) By specifying SocketProtectionLevel.Ssl, you can request SSL encryption (if configured on the server).
    Again, we used a WinRT DataWriter rather than a .NET BinaryWriter, and wrote the length of the string (measured in bytes rather than characters), followed by the
    string itself which is UTF-8 encoded. Finally, we called StoreAsync which writes the buffer to the backing stream, and closed the socket.

  • 相关阅读:
    CodeForces 363B Fence
    php结合redis实现高并发下的抢购、秒杀功能 (转载)
    PHP+Mysql基于事务处理实现转账功能的方法
    Yahoo网站性能优化的34条军规
    Cookie/Session机制详解
    PHP根据传入参数合并多个JS和CSS文件的简单实现
    PHP 使用redis实现秒杀
    PHP 常用字符串函数
    mysqldump
    局域网下关闭别人的电脑
  • 原文地址:https://www.cnblogs.com/gisbeginner/p/2671641.html
Copyright © 2011-2022 走看看