Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🌍 network - Ağ İşlemleri

TCP, UDP, Socket, DNS

87
Fonksiyon
648
Satır
18.5 KB
Boyut

🚀 Hızlı Başlangıç

içe_aktar network

// TCP Server
değişken sunucu = network.TcpListener_bind("127.0.0.1:8080")
yazdır("TCP sunucu 8080 portunda dinliyor...")

döngü yap
    değişken (soket, adres) = sunucu.kabul_et()
    yazdır("Bağlantı: " + adres.yazıya())
    
    değişken veri = soket.oku(1024)
    soket.yaz("Echo: " + veri)
    soket.kapat()
son

// TCP Client
değişken istemci = network.TcpStream_connect("127.0.0.1:8080")
istemci.yaz("Merhaba sunucu!")
değişken yanıt = istemci.oku(1024)
yazdır(yanıt)

📚 Özellikler

  • TCP: Stream-based connection
  • UDP: Datagram packets
  • DNS: Name resolution
  • Socket: Low-level networking

💡 Örnekler

Chat Server

içe_aktar network, thread, collections

değişken istemciler = collections.Vec_new()
değişken mutex = thread.Mutex_new(istemciler)

fonksiyon istemci_işle(soket: TcpStream) yap
    döngü yap
        değişken mesaj = soket.oku(1024)
        eğer mesaj == "" ise yap
            kır
        son
        
        // Tüm istemcilere yayınla
        değişken liste = thread.Mutex_lock(mutex)
        her istemci içinde liste için yap
            istemci.yaz(mesaj)
        son
        thread.Mutex_unlock(mutex)
    son
    soket.kapat()
son

değişken sunucu = network.TcpListener_bind("0.0.0.0:9000")
yazdır("Chat sunucu çalışıyor: 9000")

döngü yap
    değişken (soket, adres) = sunucu.kabul_et()
    yazdır("Yeni istemci: " + adres.yazıya())
    
    thread.spawn(fonksiyon() yap
        istemci_işle(soket)
    son)
son

← logging | async →