*Sitemize Üye Olunca Elinize Ne Geçer?

<--- 1. Üye Olarak Linkleri Görebilirsiniz... --->

<--- 2. İstediğiniz Kadar Paylaşım Yapabilirsiniz... --->

<--- 3. Güzel Bir Forum Hayatı Yaşayabilirsiniz... --->


Join the forum, it's quick and easy


*Sitemize Üye Olunca Elinize Ne Geçer?

<--- 1. Üye Olarak Linkleri Görebilirsiniz... --->

<--- 2. İstediğiniz Kadar Paylaşım Yapabilirsiniz... --->

<--- 3. Güzel Bir Forum Hayatı Yaşayabilirsiniz... --->

Would you like to react to this message? Create an account in a few clicks or log in to continue.

● En Güncel Paylaşım Platformu ●

---Misafir--- Hos Geldiniz Daha iyi Bir Hizmet İçin Üye olunuz.ÜyeLer Link GörebiLir

    Javada Sunucu-İstemci Örneği(Server-Client)

    MnyTirith
    MnyTirith
    ● Admin ●
    ● Admin ●


    <b>Doğum tarihi</b> Doğum tarihi : 20/06/90

    Javada Sunucu-İstemci Örneği(Server-Client) Empty Javada Sunucu-İstemci Örneği(Server-Client)

    Mesaj tarafından MnyTirith Salı Mart 09, 2010 8:21 pm

    NOT:arkadaşlar
    kodlardaki yıldızlı yerler vo1d ve 1'in yerinde i vardır. iyi
    çalışmalar.


    Öncelikle Serverimizi yani sunucumuzu yazalım. Sunucu için Kodlarımız
    aşağıda:


    Kod:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author ADANALI
    */
    import java.io.*;
    import java.net.*;

    public class SunucuOrnek1 {

    public static final int PORT = 8080;

    public static String karsiliginiBul(int sayi) {
    String ifade = null;
    switch (sayi) {
    case 0:
    ifade = "sifir";
    break;
    case 1:
    ifade = "bir";
    break;
    case 2:
    ifade = "iki";
    break;
    case 3:
    ifade = "uc";
    break;
    case 4:
    ifade = "dort";
    break;
    case 5:
    ifade = "bes";
    break;
    case 6:
    ifade = "alti";
    break;
    case 7:
    ifade = "yedi";
    break;
    case 8:
    ifade = "sekiz";
    break;
    case 9:
    ifade = "dokuz";
    break;
    default:
    ifade = "bilinmeyen deger";
    break;
    }
    return ifade;
    }

    public static **** main(String[] args) throws IOException {
    ServerSocket sunucuSoketi = new ServerSocket(PORT);
    System.out.println("Basladi: " + sunucuSoketi);
    try {
    Socket kullaniciSoketi = sunucuSoketi.accept();
    BufferedReader giris = new BufferedReader(new InputStreamReader(kullaniciSoketi.getInputStream()));
    try {
    System.out.println("baglanti kabul edildi :" + kullaniciSoketi);
    PrintWriter cikis = new PrintWriter(new BufferedWriter(new OutputStreamWriter(kullaniciSoketi.getOutputStream())), true);
    String str = giris.readLine();
    while (str != null) {
    System.out.println(str);
    Integer i = new Integer(str);
    int sayi = i.intValue();
    String ifade = karsiliginiBul(sayi);
    cikis.println(ifade);
    str = giris.readLine();
    }
    } finally {
    System.out.println("baglanti kapatiliyor...");
    kullaniciSoketi.close();
    }
    } finally {
    sunucuSoketi.close();
    }
    }
    }

    Şimdi ise istemci örneğimizi
    yazalım. kodlarımız aşağıdadır:


    Kod:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author ADANALI
    */
    import java.io.*;
    import java.net.*;

    public class istemciOrnek1 {

    public static **** main(String[] args)throws IOException{
    InetAddress adres = InetAddress.getByName(null);
    System.out.println("Ip Adresi : "+adres);
    Socket soket = new Socket(adres,8080);

    try{
    System.out.println("soket ="+soket);
    BufferedReader giris = new BufferedReader(new InputStreamReader(soket.getInputStream()));
    PrintWriter cikis = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soket.getOutputStream())), true);
    for(int i = 0; i<10;i++)
    {
    try{
    Thread.sleep(200);
    int sayi = (int)(Math.random() *10);
    cikis.println(sayi);
    String str = giris.readLine();
    System.out.println("-->"+sayi+" = "+str);
    }catch(Exception excep){excep.printStackTrace();}
    }
    }finally{System.out.println("baglanti kapatiliyor..."); soket.close();}
    }
    }

    aşağıda da sonuçları görelim

    Javada Sunucu-İstemci Örneği(Server-Client) Sunucu

    Javada Sunucu-İstemci Örneği(Server-Client) Istemci

    Şimdide bilmeyenler için
    çalıştırılmasının resmi aşağıdadır.


    Javada Sunucu-İstemci Örneği(Server-Client) Yap1

    Netbeans
    IDE'yi indirmek için tıklayınız.

      Forum Saati Paz Mayıs 19, 2024 6:49 am