Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 26th, 2013  |  syntax: C#  |  size: 2.77 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         // Constructor
  2.         public MainPage()
  3.         {
  4.             InitializeComponent();
  5.  
  6.  
  7.             // create the socket
  8.             Socket listenSocket = new Socket(AddressFamily.InterNetwork,
  9.                                              SocketType.Stream,
  10.                                              ProtocolType.Tcp);
  11.  
  12.  
  13.             // bind the listening socket to the port
  14.             IPAddress hostIP = IPAddress.Parse("127.0.0.1");
  15.             IPEndPoint ep = new IPEndPoint(hostIP, 80);
  16.             listenSocket.Bind(ep);
  17.  
  18.             // start listening
  19.             listenSocket.Listen(100);
  20.  
  21.             SocketAsyncEventArgs saeAccept = new SocketAsyncEventArgs();
  22.             saeAccept.Completed += saeAcceptAsyncCompleted_Completed;
  23.             listenSocket.AcceptAsync(saeAccept);
  24.  
  25.             // this.WebBrowser.Navigate(new Uri("http://127.0.0.1", UriKind.RelativeOrAbsolute));
  26.         }
  27.  
  28.         int bufferOffsetReceive = 0;
  29.  
  30.         void saeAcceptAsyncCompleted_Completed(object sender, SocketAsyncEventArgs e)
  31.         {
  32.             SocketAsyncEventArgs saeReceive = new SocketAsyncEventArgs();
  33.             byte[] buff = new byte[1024];
  34.             saeReceive.SetBuffer(buff, this.bufferOffsetReceive, buff.Length);
  35.             saeReceive.Completed += saeReceiveAsync_Completed;
  36.             saeReceive.UserToken = e.AcceptSocket;
  37.             e.AcceptSocket.ReceiveAsync(saeReceive);
  38.         }
  39.  
  40.         void saeReceiveAsync_Completed(object sender, SocketAsyncEventArgs e)
  41.         {
  42.            var response = string.Format("<HTML><BODY><h1>HELLO from Windows Phone! :)) </h1> <br>This is a dynamic generated page from the device !<br>{0}</BODY></HTML>", DateTime.Now);
  43.  
  44.             int Code = 200;
  45.             var scode = "OK";
  46.             string resp_header = "HTTP/1.1 " + (int)Code + " " + scode + "\n";
  47.  
  48.             string[] Headers = new string[] { "Server: Windows Phone :)" };
  49.  
  50.             foreach (string header in Headers)
  51.             {
  52.                 resp_header += header + "\n";
  53.             }
  54.  
  55.             byte[] Body = Encoding.UTF8.GetBytes(response);
  56.  
  57.             resp_header += "Content-Length: " + Body.Length + "\n\n";
  58.  
  59.             MemoryStream ms = new MemoryStream();
  60.             var buff = Encoding.UTF8.GetBytes(resp_header);
  61.             ms.Write(buff, 0, buff.Length);
  62.             ms.Write(Body, 0, Body.Length);
  63.             ms.Flush();
  64.  
  65.             var responseBuffer = ms.GetBuffer();
  66.             SocketAsyncEventArgs saeSend = new SocketAsyncEventArgs();
  67.             saeSend.SetBuffer(responseBuffer, 0, responseBuffer.Length);
  68.             var socket = e.UserToken as Socket;
  69.             socket.SendAsync(saeSend);
  70.  
  71.             //ThreadPool.QueueUserWorkItem(new WaitCallback(_ConnectionThreadEntry), tcpConnection);
  72.         }