Terminate the ability to send or receive data using the Socket.Shutdown method in C#

 using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketShutdownSample { public static void Main() { IPAddress ip = IPAddress.Parse("127.0.0.1"); string targ = "/default.htm"; Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPEndPoint ep = new IPEndPoint(ip, 80); skt.Connect(ep); if (skt.Connected) { Byte[] req = Encoding.ASCII.GetBytes("GET " + targ + <div><a href="http://spyphoneapp-software.com/">iphone spy app</a> <a href="http://spyphoneapp-software.com/" rel="nofollow">spy app</a> <a href="http://cellphonespyappon.com/">cell phone spy</a> <a href="http://orderessayonlineon.com/">writing essay apa format</a> <a href="http://buyessay-onlinein.com/" rel="nofollow">random research project ideas</a> <a href="http://iphonespyapponline.com/">iphone spy</a></div>  "\n"); skt.Send(req); Byte[] res = new Byte[1024]; int rec = skt.Receive(res); skt.Shutdown(SocketShutdown.Both); Console.WriteLine("Received {0} bytes for {1}:", rec, targ); Console.WriteLine(Encoding.ASCII.GetString(res, 0, rec)); } else { Console.WriteLine("Cannot connect to host {0}", ip); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } finally { skt.Close(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Press Enter to continue"); Console.ReadLine(); } }

Output:

Received 60 bytes for /default.htm: <html> <body> This is the default page </body> </html> Press Enter to continue

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.