All pastes #407014 Raw Edit

Something

public text v1 · immutable
#407014 ·published 2007-03-22 21:09 UTC
rendered paste body
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Collections;

namespace GrabPageServer
{
	class GrabPageServer
	{
		static void Send(string message, Socket sock)
		{
			byte[] theMessage = new Byte[1];
			for ( int i = 0; i < message.ToCharArray().Length; i++ )
			{
				theMessage[0] = (byte) message.ToCharArray()[i];
				sock.Send(theMessage);
			}
		}

		static void Main()
		{
			try
			{                               
				TcpListener server = null;
				int port = 80;
				server = new TcpListener( port );
                                
				server.Start();
				Console.WriteLine("Waiting for connection...\r\n");

				while ( true )
				{
					Socket connection = null;
					connection = server.AcceptSocket();
                
					IPEndPoint ipend = (IPEndPoint) connection.RemoteEndPoint;

					Console.WriteLine( "Connection from " + ipend.Address + " via its port " + ipend.Port + " with the following request: ");
					
					byte[] c = new byte[1];
					while (connection.Available > 0)
					{
						connection.Receive(c);
						Console.Write( (char)(int) c[0] );
					}

					Send("HTTP/1.1 200 OK\r\n", connection);
					Send("Date: " + System.DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT\r\n", connection);
					Send("Server: CrazyHTTP/1.0\r\n", connection);
					Send("Connection: close\r\n", connection);
					Send("Content-Type: text/html\r\n\r\n", connection);
					Send("<html><h1>Greetings from the Server!</h1></html>\r\n", connection);

					connection.Close();
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		}
	}
}