• Forum vBulletin altyapısından Xenforo altyapısına geçirildi, bu sebeple eski şifreleriniz ile foruma giriş yapamayacaksınız, parolamı unuttum adımından mailiniz ile şifre sıfırlayarak giriş yapabilirsiniz.

    Üyeliklerinde geçerli bir mail adresi olmadığı için sıfırlama yapamayacak kullanıcılar forum kullanıcı adlarını ve yeni şifrelerini yazarak info@maxigame.org adresine şifre sıfırlamak istediklerine dair bir mail göndersinler şifrelerini sıfırlayıp mail adreslerini güncelleyeceğiz. Şifreniz sıfırlandıktan sonra foruma giriş yapıp tekrar istediğiniz gibi değiştirebilirsiniz.

C# Kodlama

  • Konuyu başlatan Konuyu başlatan Iwa
  • Başlangıç tarihi Başlangıç tarihi
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...

Iwa

Aileden
Aktiflik
K.Tarihi
26 Eyl 2009
Mesajlar
235
Puanı
97
kakbgo.png


Kodlama

15711.png

Switch kullanımı
Form'a bir tane TextBox bir tanede Button ekleyin.
Eklediğiniz TextBox'un ismini txtSwitch yapın.
Eklediğiniz Button'un Click eventine aşağıdaki kodları yazın.
Kod:
[COLOR=#0000ff]switch[/COLOR] ([COLOR=#008080]txtSwitch[/COLOR].Text) (Seçim)
{
               [COLOR=#0000ff]case[/COLOR] "Synchronous": (Eğer belirtilen değer ise kod işler)
               [COLOR=#008080]MessageBox[/COLOR].Show("Cemil");
               [COLOR=#0000ff]break;[/COLOR] (Bitiş)              

               [COLOR=#0000ff]case[/COLOR] "C#": 
               [COLOR=#008080]MessageBox[/COLOR].Show("C Sharp");
               [COLOR=#0000ff]break[/COLOR];
               
               [COLOR=#0000ff]default: [/COLOR][COLOR=#000000](Eğer belirttiklerimizden hiçbiri değil ise buradaki kodlar işler)[/COLOR][COLOR=#0000ff]
[/COLOR][COLOR=#008080]               MessageBox[/COLOR].Show("Hiç biri");
               [COLOR=#0000ff]break[/COLOR]; (Bitiş)
            
}
15711.png

Sınırsız Döngü
Kod:
[COLOR=DarkSlateBlue][COLOR=#0000ff]
while[/COLOR][COLOR=#483d8b] ([/COLOR][COLOR=#0000ff]true[/COLOR][COLOR=#483d8b])
{
işlemler
}[/COLOR][/COLOR]

veya
Kod:
[COLOR=#0000ff]for[/COLOR][COLOR=#483d8b] (; ;)
{
işlemler
}
[/COLOR]

15711.png

If kullanımı
Form'a bir tane TextBox bir tanede Button ekleyin.
Eklediğiniz TextBox'un ismini txtIf yapın.
Eklediğiniz Button'un Click eventine aşağıdaki kodları yazın.
Kod:
[COLOR=#0000ff]if[/COLOR][COLOR=#000000] ([/COLOR][COLOR=#008080]txtIf[/COLOR][COLOR=#000000].Text == [/COLOR][COLOR=#ff0000]"Synchronous"[/COLOR][COLOR=#000000]) (Eğer girilen değer Synchronous ise kodlar işler)
{
Eğer belirtilen değer ise işleyecek kodlar
}
[/COLOR][COLOR=#0000ff]else if[/COLOR][COLOR=#000000] ([/COLOR][COLOR=#008080]txtIf[/COLOR][COLOR=#000000].Text == [/COLOR][COLOR=#ff0000]"Maxigame"[/COLOR][COLOR=#000000]) (2. if kullanacağım için başına else getiriyoruz)
{
Eğer belirtilen veri ise işleyecek kodlar
}
[/COLOR][COLOR=#0000ff]else[/COLOR][COLOR=#000000]
{
eğer belirtilenlerden hiçbiri değil ise buradaki kod işler
}[/COLOR][COLOR=#483d8b]
[/COLOR]

15711.png

Değeri küçültmek/büyültmek
Küçültmek
Kod:
[COLOR=#0000ff]string[/COLOR] a = "DENEME";
            [COLOR=#0000ff]string[/COLOR] y = a.ToLower();
[COLOR=#008080]MessageBox[/COLOR].Show(y);
(sonuç deneme olacaktır.)
Büyültmek
Kod:
[COLOR=#0000FF]string[/COLOR] a = "deneme";
            [COLOR=#0000FF]string[/COLOR] y = a.ToUpper();
[COLOR=#008080]MessageBox[/COLOR].Show(y);
(sonuç DENEME olacaktır.)
15711.png

Değerin içinde birşeyin olup olmadığını anlamak
Kod:
            [COLOR=#0000ff]string[/COLOR] s = "Microsoft Visual C#";
            [COLOR=#0000ff]if[/COLOR] (s.Contains("C#"))
            {
var
            }
            [COLOR=#0000ff]else[/COLOR]
            {
yok
            }
15711.png

Değişkenler
”¢bool (Değerler true ve false)
”¢sbyte (-127 ile 128 arası sayılar)
”¢byte (0 ile 255 arası sayılar)
”¢Int16(-32768 ile 32767 arası sayılar)
”¢UInt16(0 il 65535 arası sayılar)
”¢Int32(-2147483648 ile 2147483647 arası sayılar)
”¢UInt32(0 ile 4294967295 arası sayılar)
”¢Int64 (-9223372036854775808 ile 9223372036854775807 arası sayılar)
”¢UInt64(0 ile 18446744073709551615 arası sayılar)
”¢int (-2147483648 ile 2147483647 arası sayılar)
”¢uint(0 ile arası sayılar)4294967295
”¢string (Metin girmek için kullanılır)
”¢long (-9223372036854775808 ile 9223372036854775807 arası sayılar)
”¢ulong (0 ile 18446744073709551615 arası sayılar)
”¢float (-3,402823E+38 ile 3,402823E+38 arası sayılar)
”¢double (-1,79769313486232E+308 ile 1,79769313486232E+308 arası sayılar)
”¢decimal (-79228162514264337593543950335 ile 79228162514264337593543950335 arası sayılar)
”¢object (İstediğiniz bir şey için kullanabilirsiniz)
”¢var (Verdiğiniz değerin değişkeni neyse ona dönüşür)

”¢Bazı değişkenlerin başında U olur bunun açılımı Unsigned s olanıda signed'tir, unsigned negatif sayılar almaz.
”¢int açılımı Integer'dir.
”¢float ve double
adlı değişkenler virgüllü sayı alabilirler.
”¢Bazı değişkenlerin sonundaki sayılar kaç bit kullandığını gösterir, Int16 örneğin 2 byte kullanır tek byte 8 bit 2 byte 16 bit eder diğerleride aynı şekilde.
15711.png

Net Socket
Açıklamalar kodun altındadır.
Server (ConsoleApplication)
Kod:
[LEFT][COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System;[/FONT][/COLOR]
[B][COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Collections.Generic;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Linq;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Text;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Net.Sockets;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Net;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Threading;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]namespace[/FONT][/COLOR][COLOR=#000000][FONT=monospace] ConsoleApplication1[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]{[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]class[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Program[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]    {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]static[/FONT][/COLOR][COLOR=#000000][FONT=monospace] List<User> UserList = [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] List<User>();[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// user listesini tanımlıyoruz[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]private [/FONT][/COLOR][COLOR=blue][FONT=monospace]static [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] ConsoleCommand()[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]while[/FONT][/COLOR][COLOR=#000000][FONT=monospace] ([/FONT][/COLOR][COLOR=blue][FONT=monospace]true[/FONT][/COLOR][COLOR=#000000][FONT=monospace]) {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// sınırsız döngü[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]string[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Command = Console.ReadLine();[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// konsola birşeyler yazılana kadar bekler[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]if[/FONT][/COLOR][COLOR=#000000][FONT=monospace] (Command.StartsWith("/")) {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// eğer konsola yazılan yazı / ile başlıyorsa kod işler[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]switch[/FONT][/COLOR][COLOR=#000000][FONT=monospace] (Command.Substring(1).ToLower()) {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// en başta / olacağı için birinciden itibaren başlatıyoruz[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]case[/FONT][/COLOR][COLOR=#000000][FONT=monospace] "quit":[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// eğer konsola yazılan quit ise[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                            Environment.Exit(Environment.ExitCode);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// programı kapatır[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]break[/FONT][/COLOR][COLOR=#000000][FONT=monospace];[/FONT][/COLOR]

[COLOR=#000000][FONT=monospace]                    }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]private [/FONT][/COLOR][COLOR=blue][FONT=monospace]static [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Main([/FONT][/COLOR][COLOR=blue][FONT=monospace]string[/FONT][/COLOR][COLOR=#000000][FONT=monospace][] args)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Thread([/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] ThreadStart(ConsoleCommand)).Start();[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Yeni bir thread açıyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            Socket ServerSocket = [/FONT][/COLOR][COLOR=blue][FONT=monospace]default[/FONT][/COLOR][COLOR=#000000][FONT=monospace](Socket);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ServerSocket = [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Socketi ayarlıyoruz (tcp)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ServerSocket.Bind([/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] IPEndPoint(IPAddress.Any, 3322));[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Portu 3322 yaptık, 65535e kadar yükselebilir [/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ServerSocket.Listen(20);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Bir anda en fazla kaç kişi girebileceğini ayarlıyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ServerSocket.BeginAccept([/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AsyncCallback(AcceptCallBack), ServerSocket);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Server socketini AcceptCallBack'e gönderiyoruz ve bağlantı başlıyor[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]

[COLOR=blue][FONT=monospace]private [/FONT][/COLOR][COLOR=blue][FONT=monospace]static [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AcceptCallBack(IAsyncResult ar)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            Socket serversocket = (Socket)ar.AsyncState;[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Aldığımız socketi tanımlıyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            Socket acceptedsocket = serversocket.EndAccept(ar);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Socketi kabul ediyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            User user = [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] User(acceptedsocket);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Socketi tanımladığımız classa yerleştiriyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            UserList.Add(user);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// classı kullanıcı listesine ekliyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            Console.WriteLine("Bir bağlantı kabul edildi.");[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Konsola yazı yazıyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            serversocket.BeginAccept([/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AsyncCallback(AcceptCallBack), serversocket);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Tekrarlıyoruz [/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]class[/FONT][/COLOR][COLOR=#000000][FONT=monospace] User[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Socket socket;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]static[/FONT][/COLOR][COLOR=#000000][FONT=monospace] byte[] rBuffer = [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] byte[2048];[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// 2 KB'lik byte array açıyoruz[/FONT][/COLOR]

[COLOR=blue][FONT=monospace]public[/FONT][/COLOR][COLOR=#000000][FONT=monospace] User(Socket tsocket)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                socket = tsocket;[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// veri almaya başlıyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                socket.BeginReceive(rBuffer, 0, rBuffer.Length, 0, [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AsyncCallback(OnReceive), [/FONT][/COLOR][COLOR=blue][FONT=monospace]null[/FONT][/COLOR][COLOR=#000000][FONT=monospace]);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]

[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] DC()[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                UserList.Remove([/FONT][/COLOR][COLOR=blue][FONT=monospace]this[/FONT][/COLOR][COLOR=#000000][FONT=monospace]);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] SendAll([/FONT][/COLOR][COLOR=blue][FONT=monospace]string[/FONT][/COLOR][COLOR=#000000][FONT=monospace] str) [/FONT][/COLOR][/B][B][COLOR=green][FONT=monospace]// Herkese veriyi göndermek için bunu kullanın, Örnek: SendAll("deneme");[/FONT][/COLOR][/B][B]
[COLOR=#000000][FONT=monospace]            {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]foreach[/FONT][/COLOR][COLOR=#000000][FONT=monospace] (User user [/FONT][/COLOR][COLOR=blue][FONT=monospace]in[/FONT][/COLOR][COLOR=#000000][FONT=monospace] UserList) {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]try[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                        user.socket.Send(Encoding.Default.GetBytes(str));[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    } [/FONT][/COLOR][COLOR=blue][FONT=monospace]catch[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] OnReceive(IAsyncResult ar)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]try[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Hata gelirse yakalamak için try kullanıyoruz[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]int[/FONT][/COLOR][COLOR=#000000][FONT=monospace] DataSize = socket.EndReceive(ar);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Alınan verinin uzunluğunu buluyoruz[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]if[/FONT][/COLOR][COLOR=#000000][FONT=monospace] (DataSize > 0) {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Eğer alınan verinin uzunluğu 0 dan yüksekse[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]string[/FONT][/COLOR][COLOR=#000000][FONT=monospace] str = Encoding.Default.GetString(rBuffer, 0, DataSize);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Byte arrayı stringe çeviriyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                        Console.WriteLine("Alınan veri:" + str);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Alınan veriyi konsola yazıyoruz[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]if[/FONT][/COLOR][COLOR=#000000][FONT=monospace] (str == "Synchronous") {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Eğer veri Synchronous ise kod işler[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Maxigame diye bir veri gönderiyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                            socket.Send(Encoding.Default.GetBytes("Maxigame"));[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                        }[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// tekrar[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                        socket.BeginReceive(rBuffer, 0, rBuffer.Length, 0, [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AsyncCallback(OnReceive), [/FONT][/COLOR][COLOR=blue][FONT=monospace]null[/FONT][/COLOR][COLOR=#000000][FONT=monospace]);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    } [/FONT][/COLOR][COLOR=blue][FONT=monospace]else[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Eğer alınan verinin değeri 0 dan düşükse kullanıcı socketi kapatmış demektir[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                        Console.WriteLine("Kullanıcı çıktı");[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                } [/FONT][/COLOR][COLOR=blue][FONT=monospace]catch[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Eğer hata gelirse buradaki kodlar işleyecektir, burada hata vermişse kullanıcı socketi kapatmış demektir[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    Console.WriteLine("Kullanıcı çıktı");[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]

[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]

[COLOR=#000000][FONT=monospace]    }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]}[/FONT][/COLOR][/B][/LEFT]
Client (WindowsForm)
Form'a 2 tane TextBox ekleyin ismini değiştirmeyin, 2. eklediğiniz TextBox'un MultiLine özelliğini true yapın 1 tanede Button ekleyin.
İlk
TextBox'a istediğinizi yazıp eklediğiniz buttona
tıklayın servere veriyi göndermiş olursunuz.
Kod:
[LEFT][B][COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Collections.Generic;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.ComponentModel;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Data;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Drawing;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Linq;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Text;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Windows.Forms;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]using[/FONT][/COLOR][COLOR=#000000][FONT=monospace] System.Net.Sockets;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]namespace[/FONT][/COLOR][COLOR=#000000][FONT=monospace] WindowsFormsApplication1[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]{[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public[/FONT][/COLOR][COLOR=#000000][FONT=monospace] partial [/FONT][/COLOR][COLOR=blue][FONT=monospace]class[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Form1 : Form[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]    {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]static[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Socket ClientSocket;[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public [/FONT][/COLOR][COLOR=blue][FONT=monospace]static[/FONT][/COLOR][COLOR=#000000][FONT=monospace] byte[] rBuffer = [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] byte[2048];[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]public[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Form1()[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            InitializeComponent();[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]

[COLOR=blue][FONT=monospace]private [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Form1_Load(object sender, EventArgs e)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            CheckForIllegalCrossThreadCalls = [/FONT][/COLOR][COLOR=blue][FONT=monospace]false[/FONT][/COLOR][COLOR=#000000][FONT=monospace];[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Thread içinden form'a erişilmesini engelliyor, bunun engellememesini sağlıyoruz.[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ClientSocket = [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Socketi ayarlıyoruz (tcp)[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]try[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[/B][B][COLOR=#000000][FONT=monospace]               ClientSocket.Connect("127.0.0.1", 3322);
[/FONT][/COLOR][/B][B][COLOR=green][FONT=monospace]// Belirtilen adrese bağlanıyoruz[/FONT][/COLOR][/B][B]
[COLOR=#000000][FONT=monospace]            } [/FONT][/COLOR][COLOR=blue][FONT=monospace]catch[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Eğer hata veriyorsa server kapalıdır ve ya bağlantı reddedilmiştir [/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                MessageBox.Show("Bağlanılamıyor");[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                Environment.Exit(Environment.ExitCode);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ClientSocket.BeginReceive(rBuffer, 0, rBuffer.Length, 0, [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AsyncCallback(OnReceive), [/FONT][/COLOR][COLOR=blue][FONT=monospace]null[/FONT][/COLOR][COLOR=#000000][FONT=monospace]);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ClientSocket.Send(Encoding.Default.GetBytes("Synchronous"));[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// Synchronous diye bir veri gönderiyoruz[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]private [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] OnReceive(IAsyncResult ar)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]try[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]int[/FONT][/COLOR][COLOR=#000000][FONT=monospace] DataSize = ClientSocket.EndReceive(ar);[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]if[/FONT][/COLOR][COLOR=#000000][FONT=monospace] (DataSize > 0) {[/FONT][/COLOR]
[COLOR=blue][FONT=monospace]string[/FONT][/COLOR][COLOR=#000000][FONT=monospace] str = Encoding.Default.GetString(rBuffer, 0, DataSize);[/FONT][/COLOR]

[COLOR=#000000][FONT=monospace]                    textBox2.AppendText(Environment.NewLine + str);[/FONT][/COLOR]
[COLOR=green][FONT=monospace]// tekrar[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    ClientSocket.BeginReceive(rBuffer, 0, rBuffer.Length, 0, [/FONT][/COLOR][COLOR=blue][FONT=monospace]new[/FONT][/COLOR][COLOR=#000000][FONT=monospace] AsyncCallback(OnReceive), [/FONT][/COLOR][COLOR=blue][FONT=monospace]null[/FONT][/COLOR][COLOR=#000000][FONT=monospace]);[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                } [/FONT][/COLOR][COLOR=blue][FONT=monospace]else[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                    MessageBox.Show("bağlantı kapandı");[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            } [/FONT][/COLOR][COLOR=blue][FONT=monospace]catch[/FONT][/COLOR][COLOR=#000000][FONT=monospace] {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]                MessageBox.Show("bağlantı kapandı");[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]

[COLOR=blue][FONT=monospace]private [/FONT][/COLOR][COLOR=blue][FONT=monospace]void[/FONT][/COLOR][COLOR=#000000][FONT=monospace] button1_Click(object sender, EventArgs e)[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        {[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]            ClientSocket.Send(Encoding.Default.GetBytes(textBox1.Text));[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]        }[/FONT][/COLOR]

[COLOR=#000000][FONT=monospace]    }[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]}[/FONT][/COLOR][/B][/LEFT]
15711.png
 
Son düzenleme:
socket programlama için ayriyeten teşekkürler :)

bu arada public static List<User> UserList = new List<User>(); bu kodu daha önce hiç kullanmadım açıklayabilir misin? :)
 
Son düzenleme:
socket programlama için ayriyeten teşekkürler :)

bu arada public static List<User> UserList = new List<User>(); bu kodu daha önce hiç kullanmadım açıklayabilir misin? :)
Kullanıcı listesi işte :D tüm bağlanan kişilere veri göndermek için vs.
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Geri
Üst