コード例 #1
0
        // Read out and print the message received from the socket.
        private async void StartReader(Windows.Networking.Sockets.StreamSocket socket,
                                       Windows.Storage.Streams.DataReader reader)
        {
            try
            {
                uint bytesRead = await reader.LoadAsync(sizeof(uint));

                if (bytesRead > 0)
                {
                    uint strLength = (uint)reader.ReadUInt32();
                    bytesRead = await reader.LoadAsync(strLength);

                    if (bytesRead > 0)
                    {
                        String message = reader.ReadString(strLength);
                        WriteMessageText("Received message: " + message + "\n");
                        StartReader(socket, reader); // Start another reader
                    }
                    else
                    {
                        WriteMessageText("The peer app closed the socket\n");
                        reader.Dispose();
                        CloseSocket();
                    }
                }
                else
                {
                    WriteMessageText("The peer app closed the socket\n");
                    reader.Dispose();
                    CloseSocket();
                }
            }
            catch
            {
                WriteMessageText("The peer app closed the socket\n");
                reader.Dispose();
                CloseSocket();
            }
        }
コード例 #2
0
        public async Task<string> readFile()
        {
            string text;

            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile dataFile = await storageFolder.GetFileAsync("db.txt");
            var stream = await dataFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
            ulong size = stream.Size;
            using (var inputStream = stream.GetInputStreamAt(0))
            {
                using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
                {
                    uint numBytesLoaded = await dataReader.LoadAsync((uint)size);
                    text = dataReader.ReadString(numBytesLoaded);
                    dataReader.Dispose();
                }
            }
            stream.Dispose();
            return text;
        }
コード例 #3
0
        public async Task <string> readFile()
        {
            string text;

            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile   dataFile      = await storageFolder.GetFileAsync("db.txt");

            var stream = await dataFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

            ulong size = stream.Size;

            using (var inputStream = stream.GetInputStreamAt(0))
            {
                using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
                {
                    uint numBytesLoaded = await dataReader.LoadAsync((uint)size);

                    text = dataReader.ReadString(numBytesLoaded);
                    dataReader.Dispose();
                }
            }
            stream.Dispose();
            return(text);
        }