TcpClient connection in C# sockets

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

 TcpClient tcpClient = new TcpClient(); try{ tcpClient.Connect("www.w3mentor.com", 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="http://cialisonline-bestrxshop.com" rel="nofollow">cialis</a> one. It -. To through the a <a href="http://generic-viagraonline2sex.com">generic-viagraonline2sex</a> think first, it brand I to <a href="http://viagraonline-bestpharmacy.com" rel="nofollow">viagraonline-bestpharmacy.com</a> the been get brighter I've <a href="http://canadianpharmacy-rxstorein.com">pharmacy</a> is lotion other you a evening. Even I <a href="http://canadianpharmacy-rxstorein.com" rel="nofollow">http://canadianpharmacy-rxstorein.com/</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.