C# Socket Programlama Öneri ve Görüşleriniz

canyil97

Üye
21 Haz 2015
81
0
Merhabalar,
zByOED.png


Formlardaki mesajlarda scrollu aşağı indirmeyi unutmuşum her mesajı içinde barındırıyor merak etmeyin.
Şu şekilde yeri geldi çaldığım yeri geldi üstüne düşünüp ekleme yaptığım bir program var eleştirilerinize açığım.

Buyrun kodlar:
CLIENT KODLARI
Kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace deneme5client
{
    public partial class Form1 : Form
    {
        
        TcpClient clientSocket = new TcpClient();
        NetworkStream serverStream;
        string readData = null;
        public Form1()
        {
            InitializeComponent();
        }
        
        private **** button1_Click(object sender, EventArgs e)
        {

            readData = "Conected to Chat Server ...";
            msg();
            string ip = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
            clientSocket.Connect(ip, 10044);
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(txtbaglan.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            
            Thread ctThread = new Thread(getMessage);
            ctThread.Start();
        }
       
        private **** button2_Click(object sender, EventArgs e)
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(txtbxmsg.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            txtbxmsg.Clear();
        }
        private **** getMessage()
        {
            while (true)
            {
                try { serverStream = clientSocket.GetStream(); }
                catch { MessageBox.Show("Servera baglanirken sorun yasandi"); break; }
                //int buffSize = 0;
                byte[] inStream = new byte[100];
                //buffSize = clientSocket.ReceiveBufferSize;
                try { serverStream.Read(inStream, 0, inStream.Length); }
                catch (Exception ex) { MessageBox.Show(ex.ToString());
                }
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                readData = "" + returndata;
                msg();
            }
        }

        private **** msg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
                txtbxpool.Text = txtbxpool.Text + Environment.NewLine + " >> " + readData;
        }

       

    }
}
SERVER KODLARI
Kod:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace deneme5server
{

    class Program
    {
        public static Hashtable clientsList = new Hashtable();
        static Class1 classkisi = new Class1();
        static **** Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(10044);
            TcpClient clientSocket;
             

            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");
            
            while ((true))
            {
                
                clientSocket = serverSocket.AcceptTcpClient();
                
                Console.WriteLine("Suana kadar "+(classkisi.KisiSayisi+1) + " kisi var ");
                byte[] bytesFrom = new byte[10025];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);
                

                broadcast(dataFromClient + " Joined ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " Joined chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
       
        public static  **** broadcast(string msg, string uName, bool flag)
        {
            
            
            foreach (DictionaryEntry Item in clientsList)
            {
                classkisi.KisiSayisi = clientsList.Count;
                TcpClient broadcastSocket;
                broadcastSocket = (TcpClient)Item.Value;
                NetworkStream broadcastStream = broadcastSocket.GetStream();
                Byte[] broadcastBytes = null;
               
                if (flag == true)
                {
                    broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
                }
                else
                {
                    broadcastBytes = Encoding.ASCII.GetBytes(msg);
                    
                }
                //Console.WriteLine(uName + " says : " + msg + classkisi.KisiSayisi);
                broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
                broadcastStream.Flush();
                

            }
        }  //end broadcast function
        public class handleClinet
        {
            TcpClient clientSocket;
            string clNo;
            Hashtable clientsList;

            public **** startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
            {
                this.clientSocket = inClientSocket;
                this.clNo = clineNo;
                this.clientsList = cList;
                Thread ctThread = new Thread(doChat);
                ctThread.Start();
            }

            private **** doChat()
            {
                int requestCount = 0;
                byte[] bytesFrom = new byte[10025];
                string dataFromClient = null;
                Byte[] sendBytes = null;
                string serverResponse = null;
                string rCount = null;
                requestCount = 0;

                while ((true))
                {
                    try
                    {
                        requestCount = requestCount + 1;
                        NetworkStream networkStream = clientSocket.GetStream();
                        networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                        Console.WriteLine("["+requestCount.ToString() + ".mesaj]" + "From client - " + clNo + " : " + dataFromClient);
                        
                        
                        Program.broadcast(dataFromClient, clNo, true);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }//end while
            }//end doChat
        } //end class handleClinet

       
    }
}

Ayrıca bu tip paylaşılan programlar hep aynı modeme bağlı bilgisayarlar için geçerli çok uzaktaki birine nasıl veri gönderebilirim.
(önceden mysql la yapmıştım 10 sn de bir yeniliyordu fena programdı ama saçmaydı hiç olur mu öyle :secret :RpS_thumbdn:)
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.