Harry Potter and the Sorcerer's Stone


TCP/IP ulanishi orqali ma'lumotlarni yuborish va qabul qilish uchun ma'lumotlar oqimidan foydalanadigan C# dasturini yarating


Download 192.49 Kb.
bet34/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   30   31   32   33   34   35   36   37   ...   62
Bog'liq
Algaritim amaliy-2

70. TCP/IP ulanishi orqali ma'lumotlarni yuborish va qabul qilish uchun ma'lumotlar oqimidan foydalanadigan C# dasturini yarating.
Javob:
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main(string[] args)
{
// ulanish ma'lumotlari
string server = "localhost"; // server nomi yoki IP manzili
int port = 1234; // port raqami

try
{


// serverga ulanish
TcpClient client = new TcpClient(server, port);

// ma'lumotlarni yuborish


string message = "Salom, server!"; // yuboriladigan matn
byte[] data = Encoding.ASCII.GetBytes(message); // matnni baytlarga o'zgartirish
NetworkStream stream = client.GetStream(); // ulanish orqali to'liq ma'lumot almashish uchun

stream.Write(data, 0, data.Length); // ma'lumotni yuborish


Console.WriteLine("Ma'lumot yuborildi: {0}", message);

// ma'lumotni qabul qilish


data = new byte[256]; // qabul qilish uchun joy
string responseData = string.Empty; // qabul qilinadigan matn

int bytes = stream.Read(data, 0, data.Length); // ma'lumotni o'qiymiz


responseData = Encoding.ASCII.GetString(data, 0, bytes); // baytlarni matnga o'zgartirish

Console.WriteLine("Ma'lumot qabul qilindi: {0}", responseData);

// ulanishni yopish
stream.Close();
client.Close();
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message);
}

Console.ReadLine(); // dasturni tugatish uchun konsolni kutish


}
}
71. Ma'lumotlar oqimi yordamida Cassandra ma'lumotlar bazasidan ma'lumotlarni o'qiydi va uni formatlangan tarzda ko'rsatadigan C# dasturini amalga oshiring.
Javob:
using Cassandra;
using System;
namespace CassandraDataflow
{
class Program
{
static void Main(string[] args)
{
var cluster = Cluster.Builder()
.AddContactPoints("127.0.0.1") // Replace with your Cassandra cluster IPs
.Build();

var session = cluster.Connect("your_keyspace"); // Replace with your keyspace

var rs = session.Execute("SELECT * FROM your_table;"); // Replace with your table name

Console.WriteLine("ID\tName\t\t\tEmail");


Console.WriteLine("--\t----\t\t\t-----");

foreach (var row in rs)


{
Console.WriteLine($"{row.GetValue("id")}\t{row.GetValue("name"),-20}\t{row.GetValue("email")}");
}

Console.ReadLine();


}
}
}
Ushbu kod Cassandra klasteringizga ulanadi, jadvalingizga nisbatan SELECT iborasini bajaradi va natijalarni formatlangan holda konsolga chop etadi.
Iltimos, IP-manzil, kalit maydoni nomi, jadval nomi va ustun nomlarini maʼlumotlar bazasi uchun mos qiymatlar bilan almashtiring.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   62




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