Advertisement
mkv

C# webserver with comments

mkv
May 8th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7.  
  8. namespace wire
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             // this is the message we'll show
  15.             string message = "C# says hello!";
  16.  
  17.             // then we get the message in binary, in the form of bytes
  18.             byte[] data = Encoding.UTF8.GetBytes(message);
  19.  
  20.             // now we'll start a new web server
  21.             TcpListener server = new TcpListener(80);
  22.             server.Start();
  23.            
  24.             // and tell ourselves that we're all good so far
  25.             Console.WriteLine("Waiting for the first connection...");
  26.  
  27.             // we'll keep doing the stuff between the brackets forever
  28.             while (true)
  29.             {
  30.                 // everything between the brackets has access to the incoming connection from a browser
  31.                 using (TcpClient connection = server.AcceptTcpClient())
  32.                 {
  33.                     // get the data stream from the connection, and send the message
  34.                     Console.WriteLine("Oh! Hello!");
  35.                     connection.GetStream().Write(data, 0, data.Length);
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement