{"id":209,"date":"2015-06-04T23:29:28","date_gmt":"2015-06-04T20:29:28","guid":{"rendered":"https:\/\/gokhan-gokalp.com\/?p=209"},"modified":"2020-03-27T11:25:24","modified_gmt":"2020-03-27T10:25:24","slug":"c-ile-asenkron-socket-programlama","status":"publish","type":"post","link":"https:\/\/gokhan-gokalp.com\/tr\/c-ile-asenkron-socket-programlama\/","title":{"rendered":"C# ile Asenkron Socket Programlama"},"content":{"rendered":"<p>Kimine g\u00f6re eskide kalm\u0131\u015f olsa da, hala bir\u00e7ok yerde aktif olarak kullan\u0131lan soket programlamadan bahsedip \u00fczerinde bir \u00f6rnek ger\u00e7ekle\u015ftirmeye \u00e7al\u0131\u015faca\u011f\u0131m.<\/p>\n<h2>\u00d6ncelikle soket\u00a0programlama nedir?<\/h2>\n<p><a href=\"\/wp-content\/uploads\/2015\/06\/Socket-Workflow.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-222 lazyload\" data-src=\"\/wp-content\/uploads\/2015\/06\/Socket-Workflow.png\" alt=\"Socket-Workflow\" width=\"740\" height=\"659\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2015\/06\/Socket-Workflow.png 740w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2015\/06\/Socket-Workflow-300x267.png 300w\" data-sizes=\"(max-width: 740px) 100vw, 740px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 740px; --smush-placeholder-aspect-ratio: 740\/659;\" \/><\/a><\/p>\n<p>Soketler i\u00e7in istemci (Client) ve sunucu (Server) aras\u0131ndaki ba\u011flant\u0131n\u0131n sa\u011flanmas\u0131 i\u00e7in olan bir ileti\u015fim kanal\u0131 diyebiliriz. Ya\u015fam d\u00f6ng\u00fcs\u00fcn\u00fc basit\u00e7e tarif etmek gerekirse, sunucu \u00f6nceden belirlenen portu dinler, istemci ise bu porta mesaj g\u00f6nderir.<\/p>\n<p>\u0130leti\u015fim protokol\u00fc olarak <strong>TCP<\/strong> de, sunucu belirli bir portu dinleyerek gelen istekleri kar\u015f\u0131lar, <strong>UDP<\/strong> protokol\u00fcnde ise, tek bir soket \u00fczerinden <strong>birden \u00e7ok<\/strong> istemciye veriyi iletebilir.<\/p>\n<blockquote><p>Unutulmamal\u0131d\u0131r ki <em>UDP<\/em> protokol\u00fcn\u00fc kullanmaya karar vermi\u015f biri, meydana gelebilecek paket kay\u0131plar\u0131n\u0131 da g\u00f6ze al\u0131p, bunlar\u0131 iyi handle etmesi gerekmektedir.<\/p><\/blockquote>\n<p>Bunun en b\u00fcy\u00fck \u00f6rne\u011fi <em>VOIP<\/em> sistemlerinde ses paketlerinin zaman zaman kay\u0131plara u\u011framas\u0131d\u0131r. K\u0131saca &#8220;<em>fire and forget<\/em>&#8221; mant\u0131\u011f\u0131nda \u00e7al\u0131\u015fmaktad\u0131r diyebiliriz.<\/p>\n<h2>Nerelerde kullan\u0131l\u0131yor?<\/h2>\n<p>Bu makaleyi yazma sebebimden yola \u00e7\u0131karak, \u00f6ncelikle ger\u00e7ek bir senaryodan \u00f6rnek vermek istiyorum.<\/p>\n<p>\u00c7al\u0131\u015f\u0131yor oldu\u011fum firmada, olduk\u00e7a yo\u011fun servis verileri ile \u00e7al\u0131\u015fmaktay\u0131z. IIS&#8217;de konumlanan tek bir uygulama i\u00e7erisinden, yakla\u015f\u0131k 30+ farkl\u0131 servis \u00fczerinden verilerin e\u015f zamanl\u0131 olarak \u00e7ekildi\u011fini ve i\u015flendi\u011fini d\u00fc\u015f\u00fcnebilirsiniz. \u00c7ekilen bu verilerin boyut&#8217;lar\u0131 ise olduk\u00e7a b\u00fcy\u00fck bir durumda.<\/p>\n<p>Domain i\u00e7erisine her bir yeni servis eklendi\u011finde ise, IIS \u00fczerindeki uygulama, iyice hantalla\u015fmaya ve sunucuyu olduk\u00e7a yormaya ba\u015flam\u0131\u015ft\u0131. Bu gibi sebeplerden yola \u00e7\u0131karak, ana uygulama domain&#8217;i i\u00e7erisinden servis&#8217;leri ay\u0131rmaya karar verdik. Ayr\u0131lan bu servisleri ise, &#8220;n&#8221; adet yeni olu\u015fturdu\u011fumuz servis makinelerine deployment i\u015flemlerini ger\u00e7ekle\u015ftirdik. Ana uygulamam\u0131z ile servis makineleri aras\u0131ndaki ileti\u015fimi ise, gerek veri boyutlar\u0131n\u0131n b\u00fcy\u00fckl\u00fc\u011f\u00fcnden gerekse de daha h\u0131zl\u0131 bir ileti\u015fim sa\u011flayabilmek i\u00e7in soket \u00fczerinden y\u00f6netmeye karar verdik.<\/p>\n<p>B\u00f6ylece IIS \u00fczerinde konumlanan ana uygulamam\u0131z \u00fczerindeki y\u00fck\u00fc olduk\u00e7a azaltm\u0131\u015f ve daha esnek bir yap\u0131 sa\u011flam\u0131\u015f olduk.<\/p>\n<p>Bir ba\u015fka \u00f6rnek vermek gerekirse, ger\u00e7ek zamanl\u0131 uygulamalar\u0131 d\u00fc\u015f\u00fcnebiliriz. \u00d6rne\u011fin chat veya baz\u0131 pos cihazlar\u0131n\u0131 \u00f6rnek olarak g\u00f6sterebiliriz.<\/p>\n<h2>Bir \u00f6rnek ger\u00e7ekle\u015ftirelim<\/h2>\n<p>\u00d6ncelikle soket arac\u0131l\u0131\u011f\u0131 ile aktar\u0131cak oldu\u011fumuz nesnelerimizi, <strong>DataTransferObjects<\/strong> isminde bir proje i\u00e7erisinde tan\u0131mlayal\u0131m. \u00d6ncelikle <strong>ExampleDTO<\/strong> class&#8217;\u0131n\u0131 a\u015fa\u011f\u0131daki gibi olu\u015ftural\u0131m.<\/p>\n<pre class=\"lang:c# decode:true\">using System;\r\n\r\nnamespace ExampleDataTransferObjects\r\n{\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Serialize edebilmek i\u00e7in Serializable attribut\u00fc ile i\u015faretliyoruz.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    [Serializable]\r\n    public class ExampleDTO\r\n    {\r\n        public string Status { get; set; }\r\n        public string Message { get; set; }\r\n    }\r\n}<\/pre>\n<p>Olu\u015fturduktan sonra kodlamaya sunucu\u00a0ile devam edelim.<\/p>\n<p>Solution i\u00e7erisine yeni bir console application projesi ekleyip <strong>ExampleServer<\/strong> ad\u0131n\u0131 verelim. \u0130\u00e7erisinde ise <strong>Sockets<\/strong> isminde bir klas\u00f6r olu\u015fturup, &#8220;<strong>Client<\/strong>&#8221; class&#8217;\u0131n\u0131 a\u015fa\u011f\u0131daki gibi ekleyelim.<\/p>\n<pre class=\"lang:c# decode:true\">using ExampleDataTransferObjects;\r\nusing System;\r\nusing System.IO;\r\nusing System.Net.Sockets;\r\nusing System.Runtime.Serialization.Formatters.Binary;\r\n\r\nnamespace ExampleServer.Sockets\r\n{\r\n    public delegate void OnExampleDTOReceived(ExampleDTO eDTO);\r\n\r\n    public class Client\r\n    {\r\n        #region Variables\r\n        public OnExampleDTOReceived _OnExampleDTOReceived;\r\n        Socket _Socket;\r\n\r\n        \/\/ Socket i\u015flemleri s\u0131ras\u0131nda olu\u015fabilecek errorlar\u0131 bu enum ile handle edebiliriz.\r\n        SocketError socketError;\r\n        byte[] tempBuffer = new byte[1024]; \/\/ 1024 boyutunda temp bir buffer, gelen verinin boyutu kadar\u0131yla bunu receive k\u0131sm\u0131nda handle edece\u011fiz.\r\n        #endregion\r\n\r\n        #region Constructor\r\n        public Client(Socket socket)\r\n        {\r\n            _Socket = socket;\r\n        }\r\n        #endregion\r\n\r\n        #region Public Methods\r\n        public void Start()\r\n        {\r\n            \/\/ Socket \u00fczerinden data dinlemeye ba\u015fl\u0131yoruz.\r\n            _Socket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, OnBeginReceiveCallback, null);\r\n        }\r\n        #endregion\r\n\r\n        #region Private Methods\r\n        void OnBeginReceiveCallback(IAsyncResult asyncResult)\r\n        {\r\n            \/\/ Almay\u0131 bitiriyoruz ve gelen byte array'in boyutunu vermektedir.\r\n            int receivedDataLength = _Socket.EndReceive(asyncResult, out socketError);\r\n\r\n            if (receivedDataLength &lt;= 0 &amp;&amp; socketError != SocketError.Success)\r\n            {\r\n                \/\/ Gelen byte array verisi bo\u015f ise ba\u011flant\u0131 kopmu\u015f demektir. Buray\u0131 istedi\u011finiz gibi handle edebilirsiniz.\r\n                return;\r\n            }\r\n\r\n            \/\/ Gelen byte array boyutunda yeni bir byte array olu\u015fturuyoruz.\r\n            byte[] resizedBuffer = new byte[receivedDataLength];\r\n\r\n            Array.Copy(tempBuffer, 0, resizedBuffer, 0, resizedBuffer.Length);\r\n\r\n            \/\/ Gelen datay\u0131 burada ele alaca\u011f\u0131z.\r\n            HandleReceivedData(resizedBuffer);\r\n\r\n            \/\/ Tekrardan socket \u00fczerinden data dinlemeye ba\u015fl\u0131yoruz.\r\n            \/\/ Start();\r\n\r\n            \/\/ Socket \u00fczerinden data dinlemeye ba\u015fl\u0131yoruz.\r\n            _Socket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, OnBeginReceiveCallback, null);\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Gelen datay\u0131 handle edece\u011fimiz nokta.\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"resizedBuffer\"&gt;&lt;\/param&gt;\r\n        void HandleReceivedData(byte[] resizedBuffer)\r\n        {\r\n            if (_OnExampleDTOReceived != null)\r\n            {\r\n                using (var ms = new MemoryStream(resizedBuffer))\r\n                {\r\n                    \/\/ BinaryFormatter arac\u0131l\u0131\u011f\u0131 ile object tipimize geri deserialize i\u015flemi ger\u00e7ekle\u015ftiriyoruz ve ilgili delegate'e parametre olarak ge\u00e7iyoruz.\r\n                    ExampleDTO exampleDTO = new BinaryFormatter().Deserialize(ms) as ExampleDTO;\r\n\r\n                    _OnExampleDTOReceived(exampleDTO);\r\n                }\r\n            }\r\n        }\r\n        #endregion\r\n    }\r\n}<\/pre>\n<p>Sunucu i\u00e7in istemci i\u015flemlerini ger\u00e7ekle\u015ftirece\u011fimiz s\u0131n\u0131f\u0131 ekledikten sonra ise &#8220;Sockets&#8221; klas\u00f6r\u00fc i\u00e7erisine as\u0131l dinleme i\u015flemini yapaca\u011f\u0131m\u0131z class olan <strong>&#8220;Listener<\/strong>&#8221; class&#8217;\u0131n\u0131 ekleyelim ve a\u015fa\u011f\u0131daki gibi kodlayal\u0131m.<\/p>\n<pre class=\"lang:c# decode:true\">using ExampleDataTransferObjects;\r\nusing System;\r\nusing System.Net;\r\nusing System.Net.Sockets;\r\n\r\nnamespace ExampleServer.Sockets\r\n{\r\n    public class Listener\r\n    {\r\n        #region Variables\r\n        Socket _Socket;\r\n        int _Port;\r\n        int _MaxConnectionQueue;\r\n        #endregion\r\n\r\n        #region Constructor\r\n        public Listener(int port, int maxConnectionQueue)\r\n        {\r\n            _Port = port;\r\n            _MaxConnectionQueue = maxConnectionQueue;\r\n\r\n            \/\/ Socket'i tan\u0131ml\u0131yoruz IPv4, socket tipimiz stream olacak ve TCP Protokol\u00fc ile haberle\u015fece\u011fiz. \r\n            \/\/ TCP Protokol\u00fcnde server belirlenen portu dinler ve gelen istekleri kar\u015f\u0131lar oysaki UDP Protokol\u00fcnde tek bir socket \u00fczerinden birden \u00e7ok client'a ula\u015fmak m\u00fcmk\u00fcnd\u00fcr.\r\n            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\r\n        }\r\n        #endregion\r\n\r\n        #region Public Methods\r\n        public void Start()\r\n        {\r\n            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, _Port);\r\n\r\n            \/\/ Socket'e herhangi bir yerden ve belirtti\u011fimiz porttan gelecek olan ba\u011flant\u0131lar\u0131 belirtmeliyiz.\r\n            _Socket.Bind(ipEndPoint);\r\n\r\n            \/\/ Socketten gelecek olan ba\u011flant\u0131lar\u0131 dinlemeye ba\u015fl\u0131yoruz ve maksimum dinleyece\u011fi ba\u011flant\u0131y\u0131 belirtiyoruz.\r\n            _Socket.Listen(_MaxConnectionQueue);\r\n\r\n            \/\/ BeginAccept ile asenkron olarak gelen ba\u011flant\u0131lar\u0131 kabul ediyoruz.\r\n            _Socket.BeginAccept(OnBeginAccept, _Socket);\r\n        }\r\n        #endregion\r\n\r\n        #region Private Methods\r\n        void OnBeginAccept(IAsyncResult asyncResult)\r\n        {\r\n            Socket socket = _Socket.EndAccept(asyncResult);\r\n            Client client = new Client(socket);\r\n\r\n            \/\/ Client taraf\u0131ndan g\u00f6nderilen datam\u0131z\u0131 i\u015fleyece\u011fimiz k\u0131s\u0131m.\r\n            client._OnExampleDTOReceived += new Sockets.OnExampleDTOReceived(OnExampleDTOReceived);\r\n            client.Start();\r\n\r\n            \/\/ Tekrardan dinlemeye devam diyoruz.\r\n            _Socket.BeginAccept(OnBeginAccept, null);\r\n        }\r\n\r\n        void OnExampleDTOReceived(ExampleDTO exampleDTO)\r\n        {\r\n            \/\/ Client taraf\u0131ndan gelen data, istedi\u011finiz gibi burada handle edebilirsiniz senaryonuza g\u00f6re.\r\n            Console.WriteLine(string.Format(\"Status: {0}\", exampleDTO.Status));\r\n            Console.WriteLine(string.Format(\"Message: {0}\", exampleDTO.Message));\r\n        }\r\n        #endregion\r\n    }\r\n}<\/pre>\n<p>Dinleme i\u015flemini yapaca\u011f\u0131m\u0131z <strong>Listener<\/strong> class&#8217;\u0131n\u0131 haz\u0131rlad\u0131\u011f\u0131m\u0131za g\u00f6re, \u015fimdi console uygulamam\u0131z\u0131n &#8220;<strong>Program<\/strong>&#8221; class&#8217;\u0131n\u0131n <strong>Main<\/strong> method&#8217;u i\u00e7erisinde dinleme i\u015flemine ba\u015flayabiliriz.<\/p>\n<pre class=\"lang:c# decode:true\">using ExampleServer.Sockets;\r\nusing System;\r\n\r\nnamespace ExampleServer\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            int port = 5555;\r\n            Console.WriteLine(string.Format(\"Server Ba\u015flat\u0131ld\u0131. Port: {0}\", port));\r\n            Console.WriteLine(\"-----------------------------\");\r\n\r\n            Listener listener = new Listener(port, 50);\r\n\r\n            listener.Start();\r\n\r\n            Console.ReadLine();\r\n        }\r\n    }\r\n}<\/pre>\n<p>B\u00f6ylelikle <strong>5555<\/strong> port&#8217;u \u00fczerinden gelecek olan ba\u011flant\u0131lar\u0131 dinleyip, i\u015fleyecek bir sunucuya sahip olduk. \u015eimdi istemciyi kodlamaya ba\u015flayabiliriz.<\/p>\n<p>Solution \u00fczerine\u00a0<strong>ExampleClient<\/strong> isminde bir console application projesi ekleyelim. Ard\u0131ndan i\u00e7erisinde <strong>Sockets<\/strong> isimli bir klas\u00f6r olu\u015fturup a\u015fa\u011f\u0131daki gibi bu klas\u00f6r i\u00e7erisinde <strong>ExampleSocket <\/strong>isimli bir class tan\u0131mlayal\u0131m.<\/p>\n<pre class=\"lang:c# decode:true\">using ExampleDataTransferObjects;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Net;\r\nusing System.Net.Sockets;\r\nusing System.Runtime.Serialization.Formatters.Binary;\r\n\r\nnamespace ExampleClient.Sockets\r\n{\r\n    public class ExampleSocket\r\n    {\r\n        #region Variables\r\n        Socket _Socket;\r\n        IPEndPoint _IPEndPoint;\r\n\r\n        \/\/ Socket i\u015flemleri s\u0131ras\u0131nda olu\u015fabilecek errorlar\u0131 bu enum ile handle edebiliriz.\r\n        SocketError socketError;\r\n        byte[] tempBuffer = new byte[1024];\r\n        #endregion\r\n\r\n        #region Constructor\r\n        public ExampleSocket(IPEndPoint ipEndPoint)\r\n        {\r\n            _IPEndPoint = ipEndPoint;\r\n\r\n            \/\/ Socket'i tan\u0131ml\u0131yoruz IPv4, socket tipimiz stream olacak ve TCP Protokol\u00fc ile haberle\u015fece\u011fiz. \r\n            \/\/ TCP Protokol\u00fcnde server belirlenen portu dinler ve gelen istekleri kar\u015f\u0131lar oysaki UDP Protokol\u00fcnde tek bir socket \u00fczerinden birden \u00e7ok client'a ula\u015fmak m\u00fcmk\u00fcnd\u00fcr.\r\n            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\r\n        }\r\n        #endregion\r\n\r\n        #region Public Methods\r\n        public void Start()\r\n        {\r\n            \/\/ BeginConnect ile asenkron olarak bir ba\u011flant\u0131 ba\u015flat\u0131yoruz.\r\n            _Socket.BeginConnect(_IPEndPoint, OnBeginConnect, null);\r\n        }\r\n\r\n        public void SendData(ExampleDTO exampleDTO)\r\n        {\r\n            using (var ms = new MemoryStream())\r\n            {\r\n                \/\/ \u0130lgili object'imizi binary'e serialize ediyoruz.\r\n                new BinaryFormatter().Serialize(ms, exampleDTO);\r\n                IList&lt;ArraySegment&lt;byte&gt;&gt; data = new List&lt;ArraySegment&lt;byte&gt;&gt;();\r\n\r\n                data.Add(new ArraySegment&lt;byte&gt;(ms.ToArray()));\r\n\r\n                \/\/ G\u00f6nderme i\u015flemine ba\u015fl\u0131yoruz.\r\n                _Socket.BeginSend(data, SocketFlags.None, out socketError, (asyncResult) =&gt;\r\n                {\r\n                    \/\/ G\u00f6nderme i\u015flemini bitiriyoruz.\r\n                    int length = _Socket.EndSend(asyncResult, out socketError);\r\n\r\n                    if (length &lt;= 0 || socketError != SocketError.Success)\r\n                    {\r\n                        Console.WriteLine(\"Server ba\u011flant\u0131s\u0131 koptu!\");\r\n                        return;\r\n                    }\r\n                }, null);\r\n\r\n                if (socketError != SocketError.Success)\r\n                    Console.WriteLine(\"Server ba\u011flant\u0131s\u0131 koptu!\");\r\n            }\r\n        }\r\n        #endregion\r\n\r\n        #region Private Methods\r\n        void OnBeginConnect(IAsyncResult asyncResult)\r\n        {\r\n            try\r\n            {\r\n                \/\/ Ba\u011flanma i\u015flemini bitiriyoruz.\r\n                _Socket.EndConnect(asyncResult);\r\n\r\n                \/\/ Ba\u011fland\u0131\u011f\u0131m\u0131z socket \u00fczerinden datay\u0131 dinlemeye ba\u015fl\u0131yoruz.\r\n                _Socket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, OnBeginReceive, null);\r\n            }\r\n            catch (SocketException)\r\n            {\r\n                \/\/ Servera ba\u011flanamama durumlar\u0131nda bize SocketException f\u0131rlat\u0131cakt\u0131r. Hatalar\u0131 burada handle edebilirsiniz.\r\n                Console.WriteLine(\"Servera ba\u011flan\u0131lam\u0131yor!\");\r\n            }\r\n        }\r\n\r\n        void OnBeginReceive(IAsyncResult asyncResult)\r\n        {\r\n            \/\/ Almay\u0131 bitiriyoruz ve geriye gelen byte array'in boyutunu vermektedir.\r\n            int receivedDataLength = _Socket.EndReceive(asyncResult, out socketError);\r\n\r\n            if (receivedDataLength &lt;= 0 || socketError != SocketError.Success)\r\n            {\r\n                \/\/ Gelen byte array verisi bo\u015f ise ba\u011flant\u0131 kopmu\u015f demektir. Buray\u0131 istedi\u011finiz gibi handle edebilirsiniz.\r\n                Console.WriteLine(\"Server ba\u011flant\u0131s\u0131 koptu!\");\r\n                return;\r\n            }\r\n\r\n            \/\/ Tekrardan socket \u00fczerinden datay\u0131 dinlemeye ba\u015fl\u0131yoruz.\r\n            _Socket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, OnBeginReceive, null);\r\n        }\r\n        #endregion\r\n    }\r\n}<\/pre>\n<p>\u0130stemcimiz i\u00e7in soketi haz\u0131rlad\u0131\u011f\u0131m\u0131za g\u00f6re, \u015fimdi console uygulamam\u0131z\u0131n <strong>Program <\/strong>class&#8217;\u0131n\u0131n <strong>Main<\/strong> method&#8217;u i\u00e7erisinde soket&#8217;e ba\u011flanma i\u015flemlerini ger\u00e7ekle\u015ftirebiliriz.<\/p>\n<pre class=\"lang:c# decode:true\">using ExampleClient.Sockets;\r\nusing ExampleDataTransferObjects;\r\nusing System;\r\nusing System.Net;\r\nusing System.Linq;\r\n\r\nnamespace ExampleClient\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            int port = 5555;\r\n            Console.WriteLine(string.Format(\"Client Ba\u015flat\u0131ld\u0131. Port: {0}\", port));\r\n            Console.WriteLine(\"-----------------------------\");\r\n\r\n            ExampleSocket exampleSocket = new ExampleSocket(new IPEndPoint(IPAddress.Parse(\"127.0.0.1\"), port));\r\n            exampleSocket.Start();\r\n\r\n            Console.WriteLine(\"G\u00f6ndermek i\u00e7in \\\"G\\\", bas\u0131n\u0131z...\");\r\n\r\n            int count = 1;\r\n            while (Console.ReadLine().ToUpper() == \"G\")\r\n            {\r\n                ExampleDTO exampleDTO = new ExampleDTO()\r\n                {\r\n                    Status = string.Format(\"{0}. Al\u0131nd\u0131\", count),\r\n                    Message = string.Format(\"{0} ip numaral\u0131 client \u00fczerinden geliyorum!\", GetLocalIPAddress())\r\n                };\r\n\r\n                exampleSocket.SendData(exampleDTO);\r\n                count++;\r\n            }\r\n\r\n            Console.ReadLine();\r\n        }\r\n\r\n        static string GetLocalIPAddress()\r\n        {\r\n            string localIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(a =&gt; a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault().ToString();\r\n\r\n            return localIP;\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u0130stemcimizde sunucuya ba\u011flanmak i\u00e7in haz\u0131r durumda.Tek yapmam\u0131z gereken g\u00f6ndermek i\u00e7in G tu\u015funa basmak.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2015\/06\/socket-test.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-224 lazyload\" data-src=\"\/wp-content\/uploads\/2015\/06\/socket-test.jpg\" alt=\"socket-test\" width=\"876\" height=\"526\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2015\/06\/socket-test.jpg 876w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2015\/06\/socket-test-300x180.jpg 300w\" data-sizes=\"(max-width: 876px) 100vw, 876px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 876px; --smush-placeholder-aspect-ratio: 876\/526;\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<blockquote><p><strong>ExampleDataTransferObjects<\/strong> k\u00fct\u00fcphanesini istemci ve sunucu taraf\u0131nda referans olarak eklemeyi unutmay\u0131n\u0131z.<\/p><\/blockquote>\n<p>Kaynak kodu ekte bulabilirsiniz.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2015\/06\/SocketProgrammingExample.rar\">SocketProgrammingExample<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kimine g\u00f6re eskide kalm\u0131\u015f olsa da, hala bir\u00e7ok yerde aktif olarak kullan\u0131lan soket programlamadan bahsedip \u00fczerinde bir \u00f6rnek ger\u00e7ekle\u015ftirmeye \u00e7al\u0131\u015faca\u011f\u0131m. \u00d6ncelikle soket\u00a0programlama nedir? Soketler i\u00e7in istemci (Client) ve sunucu (Server) aras\u0131ndaki ba\u011flant\u0131n\u0131n sa\u011flanmas\u0131 i\u00e7in olan bir ileti\u015fim kanal\u0131 diyebiliriz. Ya\u015fam d\u00f6ng\u00fcs\u00fcn\u00fc basit\u00e7e tarif etmek gerekirse,&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/gokhan-gokalp.com\/tr\/c-ile-asenkron-socket-programlama\/\">Devam\u0131n\u0131 okuyun<span class=\"screen-reader-text\">C# ile Asenkron Socket Programlama<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[43,44,45,42,41],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-net","tag-c-asenkron-socket-programlama","tag-c-client","tag-c-server","tag-c-socket-programlama","tag-soket-programlama","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"tr","enabled_languages":["en","tr"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"tr":{"title":false,"content":false,"excerpt":false}}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C# ile Asenkron Socket Programlama - G\u00f6khan G\u00f6kalp<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# ile Asenkron Socket Programlama - G\u00f6khan G\u00f6kalp\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/\" \/>\n<meta property=\"og:site_name\" content=\"G\u00f6khan G\u00f6kalp\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-04T20:29:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-27T10:25:24+00:00\" \/>\n<meta name=\"author\" content=\"G\u00f6khan G\u00f6kalp\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Yazan:\" \/>\n\t<meta name=\"twitter:data1\" content=\"G\u00f6khan G\u00f6kalp\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/\"},\"author\":{\"name\":\"G\u00f6khan G\u00f6kalp\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\"},\"headline\":\"C# ile Asenkron Socket Programlama\",\"datePublished\":\"2015-06-04T20:29:28+00:00\",\"dateModified\":\"2020-03-27T10:25:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/\"},\"wordCount\":666,\"commentCount\":29,\"publisher\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\"},\"keywords\":[\"C# Asenkron Socket Programlama\",\"C# Client\",\"C# Server\",\"C# Socket Programlama\",\"soket programlama\"],\"articleSection\":[\".NET\"],\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/\",\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/\",\"name\":\"C# ile Asenkron Socket Programlama - G\u00f6khan G\u00f6kalp\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#website\"},\"datePublished\":\"2015-06-04T20:29:28+00:00\",\"dateModified\":\"2020-03-27T10:25:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/c-ile-asenkron-socket-programlama\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gokhan-gokalp.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# ile Asenkron Socket Programlama\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#website\",\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/\",\"name\":\"G\u00f6khan G\u00f6kalp\",\"description\":\"C# &amp; Python lover\",\"publisher\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gokhan-gokalp.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"tr\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\",\"name\":\"G\u00f6khan G\u00f6kalp\",\"pronouns\":\"he\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"tr\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659\",\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659\",\"contentUrl\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659\",\"caption\":\"G\u00f6khan G\u00f6kalp\"},\"logo\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659\"},\"sameAs\":[\"https:\\\/\\\/gokhan-gokalp.com\"],\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/tr\\\/author\\\/gok-gokalp\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# ile Asenkron Socket Programlama - G\u00f6khan G\u00f6kalp","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/","og_locale":"tr_TR","og_type":"article","og_title":"C# ile Asenkron Socket Programlama - G\u00f6khan G\u00f6kalp","og_url":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/","og_site_name":"G\u00f6khan G\u00f6kalp","article_published_time":"2015-06-04T20:29:28+00:00","article_modified_time":"2020-03-27T10:25:24+00:00","author":"G\u00f6khan G\u00f6kalp","twitter_card":"summary_large_image","twitter_misc":{"Yazan:":"G\u00f6khan G\u00f6kalp","Tahmini okuma s\u00fcresi":"9 dakika"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/#article","isPartOf":{"@id":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/"},"author":{"name":"G\u00f6khan G\u00f6kalp","@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe"},"headline":"C# ile Asenkron Socket Programlama","datePublished":"2015-06-04T20:29:28+00:00","dateModified":"2020-03-27T10:25:24+00:00","mainEntityOfPage":{"@id":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/"},"wordCount":666,"commentCount":29,"publisher":{"@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe"},"keywords":["C# Asenkron Socket Programlama","C# Client","C# Server","C# Socket Programlama","soket programlama"],"articleSection":[".NET"],"inLanguage":"tr","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/","url":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/","name":"C# ile Asenkron Socket Programlama - G\u00f6khan G\u00f6kalp","isPartOf":{"@id":"https:\/\/gokhan-gokalp.com\/#website"},"datePublished":"2015-06-04T20:29:28+00:00","dateModified":"2020-03-27T10:25:24+00:00","breadcrumb":{"@id":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/#breadcrumb"},"inLanguage":"tr","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gokhan-gokalp.com\/c-ile-asenkron-socket-programlama\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gokhan-gokalp.com\/"},{"@type":"ListItem","position":2,"name":"C# ile Asenkron Socket Programlama"}]},{"@type":"WebSite","@id":"https:\/\/gokhan-gokalp.com\/#website","url":"https:\/\/gokhan-gokalp.com\/","name":"G\u00f6khan G\u00f6kalp","description":"C# &amp; Python lover","publisher":{"@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gokhan-gokalp.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"tr"},{"@type":["Person","Organization"],"@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe","name":"G\u00f6khan G\u00f6kalp","pronouns":"he","image":{"@type":"ImageObject","inLanguage":"tr","@id":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659","url":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659","contentUrl":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659","caption":"G\u00f6khan G\u00f6kalp"},"logo":{"@id":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1776170659"},"sameAs":["https:\/\/gokhan-gokalp.com"],"url":"https:\/\/gokhan-gokalp.com\/tr\/author\/gok-gokalp\/"}]}},"_links":{"self":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":5,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":3350,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts\/209\/revisions\/3350"}],"wp:attachment":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}