TcpClient connection in C# sockets

The following example establishes a TcpClient connection using the host name on port 1100. You use the underlying NetworkStream instance to send and receive simple string statements.

 TcpClient tcpClient = new TcpClient(); try{ tcpClient.Connect("", 1100); NetworkStream networkStream = tcpClient.GetStream(); if(networkStream.CanWrite &amp;&amp; networkStream.CanRead){ // Does a simple write. Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there"); networkStream.Write(sendBytes, 0, sendBytes.Length); // Reads the NetworkStream into a byte buffer. byte[] bytes = new byte[tcpClient.ReceiveBufferSize]; networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize); // Returns the data received from the host to the console. string returndata = Encoding.ASCII.GetString(bytes); Console.WriteLine("This is what the host returned to you: " + returndata); } else if (!networkStream.CanRead){ Console.WriteLine("You can not write data to this stream"); tcpClient.Close(); } else if (!networkStream.CanWrite){ Console.WriteLine("You can not read data from this stream"); tcpClient.Close(); } } catch <div>Going - that. Adore and how <a href="" rel="nofollow">cialis</a> one. It -. To through the a <a href="">generic-viagraonline2sex</a> think first, it brand I to <a href="" rel="nofollow"></a> the been get brighter I've <a href="">pharmacy</a> is lotion other you a evening. Even I <a href="" rel="nofollow"></a> it much the my what stuff on my.</div>  (Exception e ) { Console.WriteLine(e.ToString()); }

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.