// files.cs // You will usually need for input output operations using System.IO; using System; //using System.Text; using System.Net.Sockets; using System.Net; // IPEndPoint class debug { public static void abc(string str) { // FileMode - Append, Create, CreateNew, Open, OpenOrCreate, or Truncate // FileAccess - Read, ReadWrite, or Write // Read the whole file contents in FileStream file = new FileStream("abc.txt", FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write); // Create a new stream to read from a file StreamWriter f = new StreamWriter(file); f.Write(str); // Close StreamReader f.Close(); // Close file file.Close(); }// End of abc(..) }// End of class abc // Blocking sockets entry point of program class Program { static void Main() { // google.com -> 216.239.53.99:80 debug.abc("Setting Up Socket:\r\n"); // +-- an enum, which defines which networking we want to // | use, InterNetwork implies IP v 4 // | // | +- Socket type - reliable or // | | unreliable // | | // | | +- Protocol // | | | Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); // Connect to our server debug.abc("Connecting to Server:\r\n"); int port = 80; System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse("216.239.53.99"); IPEndPoint cc = new IPEndPoint( ipAdd, port ); //IPHostEntry IPHost = Dns.Resolve("www.google.com"); //IPAddress[] addr = IPHost.AddressList; //IPEndPoint cc = new IPEndPoint( addr[0], port ); s.Connect(cc); if( !s.Connected ) debug.abc("unable to connect" ); // Send the request for the html debug.abc("Sending:\r\n"); try { string szData = "GET / HTTP/1.0\r\n\r\n"; byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData); s.Send(byData, byData.Length, 0); } catch (SocketException se) { System.Windows.Forms.MessageBox.Show ( se.Message ); } // Receiving default html page debug.abc("Receive:\r\n"); int iRxBytes = 1; while( iRxBytes > 0 ) { byte [] buffer = new byte[1024]; iRxBytes = s.Receive (buffer, buffer.Length, 0); string received = System.Text.Encoding.ASCII.GetString(buffer, 0, iRxBytes); debug.abc(received); }// End while loop s.Close(); } } /* void Listen( int port ) { wait4socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp ); IPEndPoint localEp = new IPEndPoint( IPAddress.Any, port ); wait4socket.Bind( localEp ); // server mode; start listening wait4socket.Listen(1); // begin accepting connections socket = wait4socket.Accept(); remoteHost = IPAddress.Parse(((IPEndPoint)socket.RemoteEndPoint).Address.ToString()) + ":" + ((IPEndPoint)socket.RemoteEndPoint).Port.ToString(); // on return from Accept(), readSock is a new, // connected socket // dump the wait socket now wait4socket.Close(); } */