using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; namespace http_server { class Program { static void Main(string[] args) { int port = 80; int len = 0; TcpListener server = new TcpListener(port); server.Start(); Console.WriteLine("server start port[{0}]", port); while(true) { Socket client = server.AcceptSocket(); byte[] buf = new byte[1024]; len = client.Receive(buf); Console.WriteLine("read len[{0}] data:", len); Console.WriteLine(Encoding.ASCII.GetString(buf)); string abc = @"HTTP/1.0 200 OK\nContent-Type: text/html\n\n
Welcome!
The current time at this device is {0}
"; DateTime dt = DateTime.Now; string nnn = string.Format(abc, dt.ToString()); len = client.Send(Encoding.ASCII.GetBytes(nnn)); Console.WriteLine("write len[{0}] data:", len); Console.WriteLine(abc); client.Close(); client = null; } } } } ====================================== 程序启动后在浏览器中输入:http://127.0.0.1/ 回车后即可看到响应如下: Welcome!
赋予程序生命,还我自由河山!~ --CosMosGhost
浙公网安备 33010602011771号