Harry Potter and the Sorcerer's Stone


Tarmoq ulanishi orqali ma'lumotlarni yuborish va qabul qilish uchun


Download 192.49 Kb.
bet26/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   22   23   24   25   26   27   28   29   ...   62
Bog'liq
Algaritim amaliy-2

54. Tarmoq ulanishi orqali ma'lumotlarni yuborish va qabul qilish uchun
ma'lumotlar oqimidan foydalanadigan C# dasturini yarating.
Javob:
using System;
using System.Threading.Tasks.Dataflow;

namespace DataFlowExample


{
class Program
{
static void Main(string[] args)
{
var server = new ActionBlock(message =>
{
Console.WriteLine("Server: " + message);
// Do some processing with the message
// Send back a reply message to the client
client.Post("Hello from the server!");
});

var client = new ActionBlock(message =>


{
Console.WriteLine("Client: " + message);
// Do some processing with the message
// Send back a reply message to the server
server.Post("Hello from the client!");
});

// Start the data flow by sending the first message from the client to the server


client.Post("Hello from the client!");

// Wait for the data flow to finish processing


server.Completion.Wait();
client.Completion.Wait();
}
}
}
Eslatma: Ushbu kod ActionBlock sinfidan foydalangan holda mijoz va server o'rtasida oddiy ma'lumotlar oqimini yaratadi. mijoz va server bloklari xabarlarni Post usuli yordamida oldinga va orqaga uzatadi. Completion.Wait ma'lumotlar oqimini qayta ishlash tugashini kutish uchun ishlatiladi.
55. Ma'lumotlar oqimi yordamida C# dasturini faylga ma'lumotlar yozish va o'qish uchun kod yozing.
Javob:
using System.IO;
// Faylga yozish
StreamWriter sw = new StreamWriter("file.txt");
sw.WriteLine("Ma'lumotlar faylda saqlandi.");
sw.Close();
// Fayldan o'qish
StreamReader sr = new StreamReader("file.txt");
string line = sr.ReadLine();
sr.Close();
Console.WriteLine(line);
56. Mavjud C# dasturini oddiy dastur o'rniga GZIP ma'lumotlar oqimidan foydalanish uchun o'zgartiring.
Javob:
using System.IO;
using System.IO.Compression;

// Matn ma'lumotni GZIP ko'rinishiga o'tkazish


string text = "Ma'lumotlar";
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
{
zipStream.Write(buffer, 0, buffer.Length);
}

// GZIP ma'lumotni faylga yozish


File.WriteAllBytes("compressed.gz", ms.ToArray());

// Fayldan GZIP ma'lumotni o'qish va matngacha qaytarish


byte[] compressedData = File.ReadAllBytes("compressed.gz");
ms = new MemoryStream(compressedData);
using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress))
{
byte[] decompressedBuffer = new byte[1024];
using (MemoryStream decompressedMs = new MemoryStream())
{
int read;
while ((read = zipStream.Read(decompressedBuffer, 0, decompressedBuffer.Length)) > 0)
{
decompressedMs.Write(decompressedBuffer, 0, read);
}

string decompressedText = Encoding.UTF8.GetString(decompressedMs.ToArray());


Console.WriteLine(decompressedText);
}
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   22   23   24   25   26   27   28   29   ...   62




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling