window.ENTITIES={'/api/snippets/cs/c%23%20example%20client%20socket%20class':[{"result":true,"message":null,"batch":{"type":"cs","name":"c# example client socket class","items":[{"id":"DKLZ8p","type":"cs","name":"C# example client socket class","content":"// Program.cs file:\n\nusing System;\nusing System.Text;\n\npublic class Program\n{\n public static void Main()\n {\n string host = \"some-domain.com\";\n int port = 80;\n\n using (ClientSocket clientSocket = new ClientSocket(host, port))\n {\n // request:\n\n byte[] requestBytes = new byte[] {1, 2, 3, /* ... */};\n\n clientSocket.Send(requestBytes);\n\n // response:\n\n byte[] responseBuffer = new byte[1024];\n int responseSize = clientSocket.Receive(responseBuffer);\n\n // ...\n \n // cleaning:\n\n clientSocket.Close();\n }\n }\n}\n\n\n// ClientSocket.cs file:\n\nusing System;\nusing System.Net;\nusing System.Net.Sockets;\n\npublic class ClientSocket: IDisposable\n{\n private bool disposed;\n\n private Socket socket;\n\n public ClientSocket(string host, int port)\n {\n IPHostEntry entry = Dns.GetHostEntry(host);\n\n this.socket = new Socket(SocketType.Stream, ProtocolType.Tcp);\n\n try\n {\n this.socket.Connect(entry.AddressList, port);\n }\n catch (SocketException ex)\n {\n this.socket.Dispose();\n\n throw ex;\n }\n }\n\n ~ClientSocket()\n {\n this.Dispose(false);\n }\n\n public int Send(byte[] buffer)\n {\n return this.socket.Send(buffer, SocketFlags.None);\n }\n\n public int Send(byte[] buffer, int offset, int size)\n {\n return this.socket.Send(buffer, offset, size, SocketFlags.None);\n }\n\n public int Receive(byte[] buffer)\n {\n return this.socket.Receive(buffer, SocketFlags.None);\n }\n\n public int Receive(byte[] buffer, int offset, int size)\n {\n return this.socket.Receive(buffer, offset, size, SocketFlags.None);\n }\n\n public void Close()\n {\n this.socket.Shutdown(SocketShutdown.Both);\n this.socket.Close();\n }\n\n protected virtual void Dispose(bool disposing)\n {\n if (!this.disposed)\n {\n if (disposing)\n this.socket.Dispose();\n\n this.disposed= true;\n }\n }\n\n public void Dispose()\n {\n this.Dispose(disposing: true);\n\n GC.SuppressFinalize(this);\n }\n}","source":"","author":{"id":"eaBBla","name":"FryerTuck","avatar":"1629139808762__eaBBla__w40px_h40px.jpg","points":649,"role":"BASIC"},"creationTime":1648158046000,"updateTime":1649007610000,"removalTime":null},{"id":"1yKzdp","type":"cs","name":"C# example client socket class","content":"// Program.cs file:\n\nusing System;\nusing System.Text;\n\npublic class Program\n{\n public static void Main()\n {\n string host = \"some-domain.com\";\n int port = 80;\n\n using (ClientSocket clientSocket = new ClientSocket(host, port))\n {\n // request:\n\n string requestText = \"Message to sent ...\";\n byte[] requestBytes = Encoding.UTF8.GetBytes(requestText);\n\n clientSocket.Send(requestBytes);\n\n // response:\n\n byte[] responseBuffer = new byte[1024];\n int responseSize = clientSocket.Receive(responseBuffer);\n\n string responseText = Encoding.UTF8.GetString(responseBuffer, 0, responseSize); // received message\n\n // ...\n\n // cleaning:\n\n clientSocket.Close();\n }\n }\n}\n\n\n// ClientSocket.cs file:\n\nusing System;\nusing System.Net;\nusing System.Net.Sockets;\n\npublic class ClientSocket: IDisposable\n{\n private bool disposed;\n\n private Socket socket;\n\n public ClientSocket(string host, int port)\n {\n IPHostEntry entry = Dns.GetHostEntry(host);\n\n this.socket = new Socket(SocketType.Stream, ProtocolType.Tcp);\n\n try\n {\n this.socket.Connect(entry.AddressList, port);\n }\n catch (SocketException ex)\n {\n this.socket.Dispose();\n\n throw ex;\n }\n }\n\n ~ClientSocket()\n {\n this.Dispose(false);\n }\n\n public int Send(byte[] buffer)\n {\n return this.socket.Send(buffer, SocketFlags.None);\n }\n\n public int Send(byte[] buffer, int offset, int size)\n {\n return this.socket.Send(buffer, offset, size, SocketFlags.None);\n }\n\n public int Receive(byte[] buffer)\n {\n return this.socket.Receive(buffer, SocketFlags.None);\n }\n\n public int Receive(byte[] buffer, int offset, int size)\n {\n return this.socket.Receive(buffer, offset, size, SocketFlags.None);\n }\n\n public void Close()\n {\n this.socket.Shutdown(SocketShutdown.Both);\n this.socket.Close();\n }\n\n protected virtual void Dispose(bool disposing)\n {\n if (!this.disposed)\n {\n if (disposing)\n this.socket.Dispose();\n\n this.disposed= true;\n }\n }\n\n public void Dispose()\n {\n this.Dispose(true);\n\n GC.SuppressFinalize(this);\n }\n}","source":"","author":{"id":"QaNKlo","name":"Nataniel-Barclay","avatar":"1629131030281__QaNKlo__w40px_h40px.jpg","points":499,"role":"BASIC"},"creationTime":1648158237000,"updateTime":1652487402000,"removalTime":null}]}}]};