Bir adet mesajlaşma uygulaması....
Bu bir C# projesidir.
Herzaman bir Chat programın nasıl çalıştığını merak ederdim.Internette C# ile bunun nasıl yapıyorlar diye neyseki biraz araştırdıktan sonra birseyler öğrendim.
Öncelikle bu işin esas Temeli Socket Programlama.
Socketler, bir tür süreçler arası haberleşme(interprocessing) yöntemidir.Aklınıza gelebilecek hemen her internet programı socket program olarak çalışır. Örneğin ftp programları 21 numaralı port üzerinde çalışan socket programlarıdır veya Chat programları.Socket programlama cok karısık ve bir o kadarda uzun konudur.Biz burda Client ve Server olarak iki sürec arasindaki bir Chat Programının nasıl yapıldığını kısaca görelim.
Yaptığım örneğin görünüşü
Öncelikle Chat programı iki kısımdan olusacağı için iki tane WinForm projesi açalım ve isimlerini Client ve Server yapalım.
Önce SERVER kismini inceleyelim.
Önce SERVER kismini inceleyelim.
Server ilk çalıştığında belirtilen Portu dinlemeye başlar.O Port üzerinden (Client) tarafindan bir bağlanma isteği geldiğinde kendisine bağlanmaya izin verir.
Ve böylece Client, Servere bağlanmış kendi aralarında veri alış verişi yapmaya hazır hale gelmiş olurlar.
Server için Formumuza 3 Tane Button, 2 tane TexBox ve 1 tanede richTextBox ve birde Label koyalım.Asağıdaki kodlarımızıda gerekli yerlere yazalım.
Kod aralarına gerekli açıklamaları yaptım nerde ne yapılıyor anlayabilirsiniz !
Uzay isimleri olarak şu iki usingi Kodumuzun başına ekleyelim
Ve böylece Client, Servere bağlanmış kendi aralarında veri alış verişi yapmaya hazır hale gelmiş olurlar.
Server için Formumuza 3 Tane Button, 2 tane TexBox ve 1 tanede richTextBox ve birde Label koyalım.Asağıdaki kodlarımızıda gerekli yerlere yazalım.
Kod aralarına gerekli açıklamaları yaptım nerde ne yapılıyor anlayabilirsiniz !
Uzay isimleri olarak şu iki usingi Kodumuzun başına ekleyelim
KODLAR:
1
2
| using System.Net.Sockets; using System.Threading; //Ayni anda iki Method calistirabilmek icin |
SERVER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
| using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace Server { public partial class Form1 : Form { //Gerekli Siniflarin Nesneleri tanimlaniyor Thread t; IPAddress ipadresimiz; TcpListener dinle; Socket soket; NetworkStream ag; StreamReader oku; StreamWriter yaz; public delegate void ricdegis(string text); public Form1() { InitializeComponent(); } // Serverde Method_1 public void okumayabasla() { soket = dinle.AcceptSocket(); ag = new NetworkStream(soket); oku = new StreamReader(ag); while (true) { try { string yazi = oku.ReadLine(); ekranabas(yazi); } catch { return; } } } // Serverde Method_2 (Gelen Veriyi richTextBox icine yazdirmak icin) public void ekranabas(string s) { if (this.InvokeRequired) { ricdegis degis = new ricdegis(ekranabas); this.Invoke(degis, s); } else { s = "Server: " + s; richTextBox1.AppendText(s + "n"); } } // Serverde Method_3 (Serverimizin Port dinlemesine baslamsi icin) private void dinlemeye_basla() { try { ipadresimiz = IPAddress.Parse("127.0.0.1"); dinle = new TcpListener(ipadresimiz, Convert.ToInt16(textBox1.Text)); dinle.Start(); t = new Thread(new ThreadStart(okumayabasla)); t.Start(); richTextBox1.AppendText(DateTime.Now.ToString()+" Dinleme baslatildi...n"); } catch (Exception) { MessageBox.Show("Dinleme baslatilamadi"); } } private void button1_Click(object sender, EventArgs e) { dinlemeye_basla(); } private void button2_Click(object sender, EventArgs e) { if (textBox2.Text == "") //Burda bos alan göndermeyi önlüyoruz... return; else { yaz = new StreamWriter(ag); yaz.WriteLine(textBox2.Text); yaz.Flush(); richTextBox1.AppendText(textBox2.Text + "n"); textBox2.Text = ""; } } private void button3_Click(object sender, EventArgs e) { ag.Close(); } } } |
Şimdi Client kısmını yazalım.Yine açtığımız WinForm üzerine 3 Button, 3 TextBox, 1 tanede richTextBox ve 2 tanede Lebel ekleyelim.Yine yukarda
belirttiğim gibi Using kısmına şu uzay isimlerini yazalım.
belirttiğim gibi Using kısmına şu uzay isimlerini yazalım.
1
2
| using System.Net.Sockets; using System.Threading; //Ayni anda iki Method calistirabilmek icin |
CLIENT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
| using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace Server { public partial class Form1 : Form { //Yine gerekli Siniflarin nesneleri tanimlaniyor Thread t; TcpClient baglantikur; NetworkStream ag; StreamReader oku; StreamWriter yaz; public delegate void ricdegis(string text); public Form1() { InitializeComponent(); } // Clintde Method_1 (Gelen veri okunuyor) public void okumayabasla() { ag = baglantikur.GetStream(); oku = new StreamReader(ag); while (true) { try { string yazi = oku.ReadLine(); ekranabas(yazi); } catch { return; } } } // Clientde Method_2 (Okunan Veri richTextBox icine yaziliyor) public void ekranabas(string s) { if (this.InvokeRequired) { ricdegis degis = new ricdegis(ekranabas); this.Invoke(degis, s); } else { s = "Client: " + s; richTextBox1.AppendText(s + "n"); } } // Clientde Method_3 (Istenilen IP'ye istenen Port üzerinden baglaniliyor) public void baglanti_kur() { try { //Ben Lochalhos üzerinde deneme yapacagim icin 127.0.0.1 verdim baglantikur = new TcpClient("127.0.0.1", Convert.ToInt16(textBox1.Text)); t = new Thread(new ThreadStart(okumayabasla)); t.Start(); richTextBox1.AppendText(DateTime.Now.ToString()+" Baglanti kuruldu...n"); } catch (Exception) { MessageBox.Show("Server ile baglanti kurulurken hata olustu !"); } } private void button1_Click(object sender, EventArgs e) { baglanti_kur(); } private void button2_Click(object sender, EventArgs e) { if (textBox2.Text == "") //Burda bos alan göndermeyi önlüyoruz... return; else { yaz = new StreamWriter(ag); yaz.WriteLine(textBox2.Text); yaz.Flush(); richTextBox1.AppendText(textBox2.Text + "n"); textBox2.Text = ""; } } private void button3_Click(object sender, EventArgs e) { baglantikur.Client.Close(); } private void Form1_Load(object sender, EventArgs e) { } } } |
kaynak kodunu gönderebilir misiniz?
YanıtlaSilsağ olun
YanıtlaSilBu yorum yazar tarafından silindi.
YanıtlaSilricdegis nedir ? Birisi bu kodu yazmış herkes kendi kodu gibi paylaşmış.
YanıtlaSilSystem.NotImplementedException: 'Metot veya işlem uygulanmadı.'
YanıtlaSilşu hatayı alıyorum
dive,ssdusevery.blogspot.com veya baaşka siteve dosyaları yüklesen olurmu
YanıtlaSilBu yorum yazar tarafından silindi.
YanıtlaSil